Understanding UNIX / Linux symbolic (soft) and hard links

by on January 30, 2006 · 34 comments· LAST UPDATED May 6, 2012

in , ,

Inodes 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. To create a hard link use ln command as follows:
# ln /root/file1 /root/file2
# ls -l

Above commands create a link to file1. Symbolic links refer to:

A symbolic path indicating the abstract location of another file.

Hard links refer to:

The specific location of physical data.

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?

You can create symbolic link with ln command:
$ ln -s /path/to/file1.txt /path/to/file2.txt
$ ls -ali

Above command will create a symbolic link to file1.txt.

Task: Symbolic link creation and deletion

Let us create a directory called foo, enter:
$ mkdir foo
$ cd foo

Copy /etc/resolv.conf file, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:19 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali

Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:24 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048602 lrwxrwxrwx   1 vivek vivek     11 2008-12-09 20:24 alink.conf -> resolv.conf
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

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.
Continue reading rest of the Understanding Linux file system series (this is part VI):

  • Part I - Understanding Linux superblock
  • Part II - Understanding Linux superblock
  • Part III - An example of Surviving a Linux Filesystem Failures
  • Part IV - Understanding filesystem Inodes
  • Part V - Understanding filesystem directories
  • Part VI - Understanding UNIX/Linux symbolic (soft) and hard links
  • Part VII - Why isn't it possible to create hard links across file system boundaries?
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 34 comments… read them below or add one }

1 Balakumar June 26, 2008 at 7:01 am

Thank u Vivek Gite Sir,
for posting very nice article about linux filesystem…

Reply

2 kaos December 9, 2008 at 2:34 pm

Hard links cannot “links” directories?

Reply

3 nixCraft December 9, 2008 at 2:37 pm

@ kaos,

Yes. Here is output from my desktop:

ln /etc /tmp/test
ln: `xy': hard link not allowed for directory

Reply

4 Serrano May 6, 2012 at 6:28 pm

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”.

Reply

5 ROBERTSON April 13, 2009 at 6:35 am

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

Reply

6 lv June 2, 2009 at 3:48 am

@Robertson
That is not really correct. Hard links cannot link directories however soft links can

Reply

7 hw June 4, 2009 at 9:13 pm

soft links are often used to link to directories, e.g.
cdrom -> /media/cdrom/
floppy -> /media/floppy/

Reply

8 Morph July 14, 2009 at 5:40 am

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

Reply

9 ramesh February 10, 2011 at 8:28 am

thank u vivek very good infomation
and nice explain.

Reply

10 Anjali March 14, 2011 at 1:06 pm

Hi

I have created the softlink…….but not working

Reply

11 Aman Bedi July 20, 2011 at 4:42 pm

@ Anjali

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.

Reply

12 mallick August 29, 2011 at 10:07 am

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

Reply

13 Srinivas Kotaru October 5, 2011 at 6:49 pm

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?

Srinivas

Reply

14 tordeu January 12, 2012 at 3:27 pm

@Srinivas Kotaru:

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.

Reply

15 mahesh February 3, 2012 at 5:10 am

how the soft links and hard links will be helpful?

Reply

16 Chaitanya February 24, 2012 at 10:17 am

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.

Reply

17 cordel March 20, 2012 at 12:08 am

@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.

Reply

18 tordeu March 21, 2012 at 9:27 am

@mahesh:

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”. 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 ~/Pictures/People/Josh Woodward.

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 ~/Music/ and 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.

In the “~/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 “~/Pictures/People/”.

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 “~/Artists/…” or 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.

Reply

19 connect@me June 1, 2012 at 10:00 am

Hi tordeu,

Nice explanation i need little more explanation about hard link with examples..

Thanks in advance.
Innova

Reply

20 tordeu June 12, 2012 at 1:16 am

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:

cd

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.

Reply

21 connect@me June 12, 2012 at 5:48 am

Hi tordeu,

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

Reply

22 thiyagi August 31, 2012 at 11:14 pm

Thanks alot guys, the post and all the conversation was informative..;)

Reply

23 dimitrios September 14, 2012 at 6:59 pm

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/ ?

Reply

24 Serrano September 16, 2012 at 1:42 pm

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.

Reply

25 dimitrios September 17, 2012 at 8:45 am

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

Thanks again

Reply

26 tordeu September 18, 2012 at 11:14 am

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.

Reply

27 SmilingInSeattle August 11, 2013 at 12:13 am

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.

Reply

28 Jouni "rautamiekka" Järvinen February 2, 2013 at 7:24 pm

So if I change the contents of a file I have a hard link for, what happens next ? The link works or not ?

Reply

29 Jouni "rautamiekka" Järvinen February 2, 2013 at 7:26 pm

I mean, does it work just like it did, with the changes visible just normally.

Reply

30 Jouni "rautamiekka" Järvinen February 2, 2013 at 7:30 pm

What is the performance difference between hard and soft ?

Reply

31 ramesh April 9, 2013 at 1:44 pm

While creating soft link to hard drive, is it necessary to use -F option, like

ln -sF

Reply

32 Dipti May 4, 2013 at 3:15 pm

Hello

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

https://www.abc.com/order/1

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 :

https://www.abc.com/order/order/1

I do not understand whts going on.
Please help me.
In advance Thanks

Reply

33 Dipti May 6, 2013 at 11:03 am

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.

Reply

34 not dave March 3, 2014 at 8:59 am

your extensive use of quoting does not clarify

this article is the opposite of useful: jumbled

rewrite required

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous post:

Next post: