- How to: Linux / UNIX create soft link with ln command
- Two types of links
- How do I create soft link / symbolic link under Unix and Linux?
- How to use the ln command
- Creating Symlink to a directory
- How to overwrite symlinks/Soft link
- How to delete or remove symlinks/soft links
- Getting help about the ln command
- Conclusion
- What is a hard and symbolic (soft) link in Linux or Unix?
- What is a Symbolic or soft link in Linux / Unix?
- What is a hard link in Linux or Unix?
- Hard link vs. Soft link in Linux or UNIX
- How do I create symbolic link in Linux?
- Symbolic link creation and deletion
- Conclusion
How to: Linux / UNIX create soft link with ln command
Symbolic links can be made to directories as well as to files on different filesystems or different partitions.
Two types of links
- symbolic links (also known as “soft links” or “symlinks”): Refer to a symbolic path indicating the abstract location of another file.
- hard links : Refer to the specific location of physical data.
How do I create soft link / symbolic link under Unix and Linux?
Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
$ ln -s file1 link1
To verify new soft link run:
$ ls -l file1 link1
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
From the above outputs it is clear that a symbolic link named ‘link1’ contains the name of the file named ‘file1’ to which it is linked.
How to use the ln command
So the syntax is as follows to create a symbolic link in Unix or Linux, at the shell prompt:
$ ln -s < source-filename >< symbolic-filename >
For example create a softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:
$ ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php
$ ls -l
Sample outputs:
You can now edit the soft link named /home/vivek/index.php and /webroot/home/httpd/test.com/index.php will get updated:
$ vi /home/vivek/index.php
Your actual file /webroot/home/httpd/test.com/index.php remains on disk even if you deleted the soft link /home/vivek/index.php using the rm command:
$ rm /home/vivek/index.php ## ##
## But original/actual file remains as it is ##
$ ls -l /webroot/home/httpd/test.com/index.php
Creating Symlink to a directory
The syntax remains same:
$ ln -s
For example, create a symbolic link from the /home/lighttpd/http/users/vivek/php/app/ directory to the /app/ directory you would run:
$ ln -s /home/lighttpd/http/users/vivek/php/app/ /app/
Now I can edit files using /app/
$ cd /app/
$ ls -l
$ vi config.php
How to overwrite symlinks/Soft link
Pass the -f to the ln command to overwrite links:
ln -f -s /path/to/my-cool-file.txt link.txt
How to delete or remove symlinks/soft links
Use the rm command to delete a file including symlinks:
rm my-link-name
unlink /app/
rm /home/vivek/index.php
Getting help about the ln command
Type the following ln command:
$ man ln
$ ln —help
ln command option | Description |
---|---|
—backup | make a backup of each existing destination file |
-b | like —backup but does not accept an argument |
-d | allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) |
-f | remove existing destination files |
-i | prompt whether to remove destinations |
-L | dereference TARGETs that are symbolic links |
-n | treat LINK_NAME as a normal file if it is a symbolic link to a directory |
-P | make hard links directly to symbolic links |
-r | create symbolic links relative to link location |
-s | make symbolic links instead of hard links |
-S | override the usual backup suffix |
-t | specify the DIRECTORY in which to create the links |
-T | treat LINK_NAME as a normal file always |
-v | print name of each linked file |
—help | display this help and exit |
—version | output version information and exit |
Conclusion
You learned how to create a symbolic link in Linux using the ln command by passing the -s option. See ln command man page here for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
What is a hard and symbolic (soft) link in Linux or Unix?
I nodes are associated with precisely one directory entry at a time. However, with hard links, it is possible to associate multiple directory entries with a single inode. Each file on Linux or Unix comes with an inode containing metadata about the file. To create a hard link use the ln command as follows:
ls /path/to/source /path/to/link
ln old_dir new_link_dir
ls [options] /path/to/source /path/to/link
## create a hard link ##
ln /home/vivek/my-awesome-long-file.txt /home/vivek/file
## create a symbolic link ##
ln -s /etc/resolv.conf /tmp/test
ls -l /tmp/test /home/vivek/file
Above commands create a link to my-awesome-long-file.txt and /etc/resolv.conf as /home/vivek/file and /tmp/test respectively.
What is a Symbolic or soft link in Linux / Unix?
Symbolic links refer to:
A symbolic path indicating the abstract location of another file. It is like a shortcut in Microsoft Windows operating system.
What is a hard link in Linux or Unix?
Hard links refer to:
The specific location of physical data. It an essentially a label or name assigned to a file.
Hard link vs. Soft link in Linux or UNIX
- Hard links cannot link directories.
- Cannot cross file system boundaries.
Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:
- To create links between directories.
- Can cross file system boundaries.
These links behave differently when the source of the link is moved or removed.
- Symbolic links are not updated.
- Hard links always refer to the source, even if moved or removed.
How do I create symbolic link in Linux?
You can create symbolic link with ln command:
ln -s source_file my_link
ln -s /path/to/file1.txt /path/to/file2.txt
ls -s /etc/hosts /tmp/file
ls -ali /tmp/file /etc/hosts
Above command will create a symbolic link to /etc/hosts as /tmp/file.
Symbolic link creation and deletion
Let us create a directory called foo using mkdir command $ mkdir foo
$ cd foo
Copy /etc/resolv.conf file using the cp command, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali
Sample outputs:
The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali
If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.
Conclusion
To create a symbolic link in Linux and Unix, at the shell prompt, enter:
ln -s source new_soft_link
ls -ali source new_soft_link
And to create a hard link in Linux and Unix, at the shell prompt, enter:
ln source new_hard_link
ls -ali source new_hard_link
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Thank u Vivek Gite Sir,
for posting very nice article about linux filesystem…
Hard links cannot “links” directories?
Yes. Here is output from my desktop:
Vivek, what kaos meant was: the second word “links” should be written without the “s” in that sentence. It should be: “Hard links cannot link directories”.
Hardlink or softlink is only for files and not for folders.
/etc is a folder name. Choose any file name under it
like
$ln /etc/test.log /tmp/test_file
@Robertson
That is not really correct. Hard links cannot link directories however soft links can
soft links are often used to link to directories, e.g.
cdrom -> /media/cdrom/
floppy -> /media/floppy/
Ya the links cannot be used for directories and also you should not change the default file permissions of symbolic links file by using chmod command lest they will grant the user all the rights to the file. The sys admins therefore disable access to the /etc directory because if you had access to the directory a simple symbolic link to /etc/passwd could get you all the access you want to the system.
see this link
thank u vivek very good infomation
and nice explain.
I have created the softlink…….but not working
please let us know the output and how are you trying to access the softlink?
if you open nautilus and try to type the path of the soft link, it will appear as a short-cut (like in windows).
Try to browse it in X windows.
when i right click on a file then how the os gets it’s properties file. i.e in os where those files are stored
I knew that hard links cannot be linked to folders,However unable to find reason behind this? I did lot of googling but still unable to get correct answer Can somebody give me reason for this in simple english?
Hard links to directories and not supported, because you would be able to create “loops”.
Imagine you would go into a folder “a” and create a hard link “b” that would link to the folder “a”. This would create and endless loop. Imagine you would tar the directory a now.
Tar would go into directory a and find a directory b in it. Then it will try to go into b, but because it is a hard link to a, it will be in folder a again. There, it would again see the folder “b” and when it does into this folder, because of the hard link, end up in a again. And this would never stop, because you created a circle.
In this example it’s easy to figure out. But the situation might be more complex. A folder a might include – somewhere deep inside – a link to a different folder b and inside b there might be a link to a folder c and somewhere within c there would be a link to folder a. Now you have a circle again.
It’s not that it’s not possible to prevent programs from hanging etc., but they hard links to directories are a problem and it was decided to not allow them to make things easier.
Though, Apple uses them in Mac OS X for Time Machine, but does not allow the user to create them.
how the soft links and hard links will be helpful?
hi,
i have one file and one link file with the same filename as resolv.conf
lrwxrwxrwx 1 vivek vivek 11 2008-12-09 20:24 alink.conf -> resolv.conf
and
-rwxr-xr-x 1 vivek vivek 129 2008-12-09 20:19 resolv.conf
How can i open the content of the file resolv.conf which has a link file i.e. 1st one.
@Chaitanya
If you try to open either file with an editor or any thing for that matter they both will open resolv.conf.
resolv.conf is your real file and alink.conf is a symbolic link pointing to resolv.conf.
Though I’m sure you probably figured this out by now.
Softlinks and hardlink can be used for various things (organizing files is one of them).
Let me explain what you might do with links with the following example:
Let’s assume you have pictures in “
/Pictures” and music in “
/Music”, there is a folder for every artist you like and within that folder there is a folder for every album, which contains the songs of the artist; so you might have a folder “
/Music/Josh Woodward” and in that folder are the albums of Josh Woodward (that gives you “
/Music/Josh Woodward/Ashes” etc.).
You also have some Pictures of Josh Woodward in
You like to have all your pictures in
/Pictures and all your music in
/Music, but you don’t like that those folders are not close together. The pictures and the music of one artist are in completely different folders and you would also like to have all the music and all the pictures of an artist in one folder.
So you might want to create the folder “
/Artists” and in that folder you create a folder for every artist and within that you have a folder “Music” and a folder “Pictures” that contains the music and pictures of that artist. In this example you would have the folders
/Artist/Josh Woodward/Music and
/Artist/Josh Woodward/Pictures.
Now you have want you wanted and all the files of one artist are packed together in one folder.
The first idea would be to copy all the files into the new folders, but that leads to three main problems:
1. every file is saved twice on the disk, so you need twice the disk space, because every song will be in
/Artists//Music and so on.
2. if you would change the tags of a Picture in
/Pictures/… the same Picture in
/Artists would still have the old tags, you if you change a Picture, you need to copy it to the other locations to update the version there.
3. every new file needs to be saved to two places, which adds more work.
Problem two and three would be solved by creating scripts, but synchronizing the content could take some time. And problem one would still remain.
And now think you might want to go even further and for every artist you want to organize your albums in several ways: in “
/Music/by-artist” you have all your music organized by artists; in “
/Music/by-year” you have all albums organized by year and in
/Music/by-title” you just have all the albums without any further organization. To maintain such a structure can only be done with scripts and every time you add a new song somewhere, you need to run a synchronization script.
THIS is where links come into play.
/Artist/” folder, you do not create a folder “Music” and a folder “Pictures” and copy all the files; you just create a symbolic link “Music” to the folder “
/Music/” and a link “Pictures” to the folder “
Now, you can access the music and pictures through “
/Artists/” folder. But the files only need to be stored once on the hard disk AND if you put a new album in “
/Music//” it is immediately accessible in “
/Artists//Music”. And if you change the tags of a Picture, it does not matter if you go to “
/Pictures/…”, the changes will be accessible in both places.
You can’t create hard links in this case, because we are dealing with directories. But you can use hard links for the same thing. Hard links are just an additional name for the same file. So you in this example, you could create the folder “
/Artists///” and then in that folder create hard links for all the audio files.
In this case, adding new albums or new files to an album will not be visible in the other place until you create hard links for all new files as well. Hard links do have some advantages, however, because sometimes soft links will create problems in applications whereas hard links are “real files”. And the most important thing: If you create 100 symlinks to a single file and you delete that single file, all symlinks will stop working, because the symlinks are just links (like on the internet, remember?) and if you remove a site from the internet, all the links to it will stop working.
With hard links that will not happen; but hard links only work in the same file system.
I hope this helped a bit.
Nice explanation i need little more explanation about hard link with examples..
Thanks in advance.
Innova
Hi,
sorry for taking so long.
Hard links are essentially just additional names for the same file. A file in linux can have more than one name and be in more than one directory at the same time.
This might be a weird concept at first, because most people do not really use it, but it has some advantages (and disadvantages, depending on what you want to achieve).
Let’s do this as an exercise. Create a file named “test.txt” in your homefolder, open it with a text editor and add some text to it.
Now open a terminal and go to your home directory (should not be necessary, because a new terminal should start at your homedirectory, but still:
Now we can create a hard link and a soft link to this file and name them test_hard.txt and text_soft.txt, respectively:
ln test.txt test_hard.txt
ln -s text.txt test_soft.txt
If you look at your home folder now, you will see 3 files: test.txt, test_hard.txt and test_soft.txt.
If you open any one of them with a text editor, you should always get the same content.
Now rename test.txt to test_new.txt.
If you try to open test_hard.txt and test_soft.txt now, you will notice that test_hard.txt works just fine, but test_soft.txt does not. The reason why the soft link does not work anymore is because a softlink is basically a file that only contains the name of another file. When you created the soft link, you created a soft link to a file named “test.txt”.
If you now open the test_soft.txt, linux will know that it is soft link and then try to load the file the soft link is pointing to, which is test.txt. But test.txt is not there anymore, because you renamed it, so the file can not be loaded.
test_hard.txt works differently. A hard link does not just remember the path and name of the file where it links to, a hard link points directly to the data on the disk (well, the inode, to be exact, but that’s not important here), so the two files test_new.txt and test_hard.txt are pointing to the exact same data on the disk.
test_new.txt and test_hard.txt are now merely two different names for the exact SAME file.
You can rename each file without affecting the other (because they are both pointing to the data) and can also delete either one and the other one will still work.
That’s because Linux will remember how many names a file has. When you created the hard link, Linux noticed that there are now two names pointing to the file on the disk. When you delete either one, Linux knows that the other one is still being used and will therefore not delete the data on the disk, but only the filename.
And it does not matter which one you delete. You can delete test_new.txt and test_hard.txt will work just fine. Linux will only delete the data on the disk once there is no filename pointing to it anymore.
You can test that it is the same file, by editing it. No matter which one you edit, you will notice that the other does reflect the changes, because it’s just ONE file and test_new.txt and test_hard.txt are just two names for the same file. You can also move one of them to a different folder and it will still work.
My advice would be to play around with soft and hard links a little bit. Create a simple text file, created soft and hard links to it in different folders and see what happens.
Just one note: You can only create a hard link to a file on the same filesystem/partition.
I hope this could help you a little. If not, please feel free to ask more questions. I will try to answer them more quickly.
thanks a lot tordeu, putting efforts to explain the concepts.. now i m understand soft & hard links working manner.. currently i m working with linux BMR. so i need to analyze the different file system, file types behavior etc.. can u guide me what are things we need to concentrate linux while we take backup and recovered..
thanks in advance
innova
Thanks alot guys, the post and all the conversation was informative..;)
Hello,
Is it possible to create a link to another server?
For example ln -s /www/htdocs/link root@192.168.1.41:/www/htdocs/ ?
That command should be:
ln -s root@192.168.1.41:/www/htdocs /www/htdocs/link
For as far as I know, that won’t work.
But you can mount the remote directory instead (e.g. with sshfs). For example:
sshfs root@192.168.1.41:/www/htdocs/ /www/htdocs/dir/
The remote server needs to have SSH setup for that to work.
Thanks for your answer, SOLVED
You are right.
This is not possible from symbolic link, i had to install sshfs and then is possible
If some one is need this goto:
http://www.linuxnix.com/2011/03/mount-directory-locally-linux-sshfs.html
Another possibility (besides the already mentioned sshfs) would be nfs, which I would say is the better option if you want to be have access to the files on the server constantly.
Also, after mounting the server on your computer (no matter which method you use), you can then create symbolic links to those mounted files.
If your server has a folder foo and inside this folder is a file bar.jpg then you might mount the foo folder to /media/server. After it is mounted, the file bar.jpg will be available under /media/server/bar.jpg.
Now its possible to create a link to that file with
ln -s /media/server/bar.jpg bar
The reason you can’t directly create a soft link is because softlinks just store the path of the file you want to point to, so the file needs to be accessible locally somehow. It’s not possible to use IP addresses to access different systems.
You need to use something else (like sshfs, nfs, ftpfs, smb etc.) to first mount the files of the server locally. Then the files of the server have a local path through which they can be accessed (usually through /media/… or /mnt/…) and now that they have a local path, you can create a soft link to them.
This is very close to what I want to do. I have a NAS server running zfs. I access the zfs filesystem at it’s root with an NFS mount on my linux workstation as well with CIFS mounts on other users Windows boxes.
When I built the server I knew it would approach 80% capacity within 2-3 years. My original plan was to swap existing 2TB drives for 4TB ones, but I read in forums that resilvering even a 500MB disk could take 1-2 days, leaving one with a degraded file system during that time.
So, now I’m looking at putting another disk farm on the existing server via an eSATA connection and a port multiplier, then creating another zpool. I’d really like to keep the same directory structure (namewise) on the new file system as on the existing filesystem and make the fact that the users are accessing different zfs file systems transparent.
Would it be as simple as creating a soft link on the server (BSD) between the original file system and the new one?
Ex: on BSD, with the original file system at /mnt/nas/data, put the new file system at /mnt/nas/data1 then at root or at /mnt on tthenhe BSD server create a symbolic link: ln -s /mnt/nas/data1 /mnt/nas/data and point the NFS and CIFS mount points at /mnt/nas/data? Below the /mnt/nas/data would be directory one, two, three, four… and some of these would have further subdirectories before getting to files to be accessed.
So if I change the contents of a file I have a hard link for, what happens next ? The link works or not ?
I mean, does it work just like it did, with the changes visible just normally.
What is the performance difference between hard and soft ?
While creating soft link to hard drive, is it necessary to use -F option, like
System is implemented with CI framework.
But symbolik link is not working satisfactory.
I want to point from abc.com to dps.com’s order controller.
Actual code is on dps.com domain contoller order.
Then I want to point that controller as follows from abc.com
But when I open this url in browser then I am getting error page not found [404 error]
And When I open it as follows it’s working :
I do not understand whts going on.
Please help me.
In advance Thanks
I want to create symbolic link .
I want to point dps.com/order
order is controller file from abc.com
so that I can access abc.com/order/[param]
then I tried to create sym link for this but not working.
please give me solution.
your extensive use of quoting does not clarify
this article is the opposite of useful: jumbled
Hi,
1st I created both hardlink (hlink) and softlink (slink) to a single file (a.dat).
As I know, slink has its own inode, and this inode will point to the inode of a.dat. But hlink does not have its own inode. when we create a hardlink it will directly point to the inode of the a.dat.
So my qs here is when we are removing Original File (a.dat), because of the existence of a hardlink (hlink), the inode of a.dat still be there in memory.But why we can not access this content by the slink, though slink’s inode points to the inode which is still exist in the memory, whereas hlink can access though it is pointing to the same inode.
Please help me understand this and correct me if i am wrong..
Источник