570

As the title says, I would like to know the difference between a hard link and a soft link created by the command ln. The command man ln does provide information, but does not sufficiently answer my question.

Also, it would be nice if someone could provide a setting where hard link might be preferable over a symbolic link.

5
  • 20
    one of differences... you have some file, for example file test. If you make ln test hardlink, make ln -s test symlink and then move file test to other dir ( or rename ), symlink wont work. Hardlink will work. Now try deleting file test. Hardlink will still work, in fact you will be still able to acces file until number of hardlinks to file isnt 0. Thats because of inodes, it is written in manual...
    – Denwerko
    May 18, 2011 at 9:35
  • 6
    I've reopened this because it deserves a good generic answer on this issue (unlike the previous question that was an obscure C example).
    – Oli
    May 18, 2011 at 14:50
  • 4
    Best answer: youtube.com/watch?v=aO0OkNxDJ3c Jan 30, 2016 at 15:08
  • 1
    Also quite a complete answer : stackoverflow.com/questions/185899/… Mar 9, 2016 at 7:20
  • @AbhishekBhatia the video is not available
    – Ooker
    Jan 19, 2018 at 12:18

9 Answers 9

408

"A picture is worth a thousand words." Pictorial representation


And, "An example is worth a hundred paragraphs..."

Create two files:

$ touch blah1   
$ touch blah2

Enter some data into them:

$ echo "Cat" > blah1
$ echo "Dog" > blah2

And as expected:

$ cat blah1; cat blah2
Cat
Dog

Let's create hard and soft links:

$ ln blah1 blah1-hard
$ ln -s blah2 blah2-soft

Let's see what just happened:

$ ls -l

blah1
blah1-hard
blah2
blah2-soft -> blah2

Changing the name of blah1 does not matter:

$ mv blah1 blah1-new
$ cat blah1-hard
Cat

blah1-hard points to the inode, the contents, of the file - that wasn't changed.

$ mv blah2 blah2-new
$ ls blah2-soft
blah2-soft
$ cat blah2-soft  
cat: blah2-soft: No such file or directory

The contents of the file could not be found because the soft link points to the name, that was changed, and not to the contents.
Similarly, If blah1 is deleted, blah1-hard still holds the contents; if blah2 is deleted, blah2-soft is just a link to a non-existing file.


source: blatantly copying it from StackOverflow!

8
  • 19
    To be fair to you - you did add the lovely image at the top... ah you copied that too! Combining the two answers is actually very helpful :)
    – icc97
    Jan 22, 2017 at 13:28
  • 3
    better explained, nowhere!
    – dennisbot
    Jun 22, 2017 at 19:49
  • 3
    I kept staring at the image for 20 seconds and then, suddenly, I figured it out. This is actually brilliant.
    – M J
    Jul 28, 2017 at 7:05
  • 2
    btw: Using hardlinks with git is a bad idea, just in case somebody (frustrated over soft links) wonders... could apply to other versioning systems as well.
    – Frank N
    Sep 18, 2017 at 12:43
  • 1
    Is a inode to its hardlinks similar to a file stored in cloud to any device accesses it?
    – Ooker
    Jan 19, 2018 at 12:24
335

A hardlink isn't a pointer to a file, it's a directory entry (a file) pointing to the same inode. Even if you change the name of the other file, a hardlink still points to the file. If you replace the other file with a new version (by copying it), a hardlink will not point to the new file. You can only have hardlinks within the same filesystem. With hardlinks you don't have concept of the original files and links, all are equal (think of it as a reference to an object). It's a very low level concept.

On the other hand, a symlink is actually pointing to another path (a file name); it resolves the name of the file each time you access it through the symlink. If you move the file, the symlink will not follow. If you replace the file with another one, keeping the name, the symlink will point to the new file. Symlinks can span filesystems. With symlinks you have very clear distinction between the actual file and symlink, which stores no info beside the path about the file it points to.

1
  • 1
    A thing that (by own words) "points to the file" may be called a pointer (it is trivially nearly tautologically true). If we are nitpicking then (in general) the notion of hardlinks may exist even if a filesystem doesn't use inodes.
    – jfs
    Jun 18, 2016 at 18:56
92

In Linux/Unix, Shortcuts are known as Links


Links are of two types: soft links (symbolic links) or hard links.

  1. Soft Links (symbolic links)

    You can make links to files and directories, and you can create links (shortcuts) on different partitions and with a different inode number than the original.

    If the real copy is deleted, the link will not work.

  2. Hard Links

    Hard links are for files only; you cannot link to a file on a different partition with a different inode number.

    If the real copy is deleted, the link will work, because it accesses the underlying data which the real copy was accessing.


Question: How do I make soft link?

Answer: A soft link can be made with ln -s; first you need to define the source and then you need to define the destination. (Keep in mind you need to define the full paths of both source and destination; otherwise it will not work.)

 sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib32/libGL.so.1
             (----------Source-------)             ( Destination )

enter image description here

As you can see it has a different inode and can be made on a different partition.


Question: How do I make Hard link?

Answer: A Hard link can be made with ln; first you need to define the source and then you need to define the destination. (Keep it mind you need to define the full path of both source and destination; otherwise it will not work.)

Let's say I have a script in the /script directory named firefox.

 ls -i # Shows you the inode
 5898242 firefox

 ln /scripts/firefox /scripts/on-fire
       ( Source )    ( Destination )

enter image description here

As you can see, it has the same inode. If I delete the original file, the link will still work, and it will act as the original.

enter image description here

Above, I check that the link is working, and then delete the original firefox script.


Question: It would be nice if someone could provide a setting where a hard link might be preferable over a symbolic link.

Answer: Depending on the disk partition layout, hard links have the limitation that they must be on same partition (-1 point) and can only link to files (-1 point), but if the original is deleted, the link will work and it acts like the original (+1 point).

On the other hand, a soft link can point to directories or files (+1 point) and there is no partition limitation (+1 point), but if the source is deleted, the link will not work (-1 point).

1
  • Can I create a hard link and provide the inode as the source?
    – TMOTTM
    Sep 30, 2018 at 9:09
89

Both are pointers to files; the difference is the kind of pointer. A symbolic link points to another file by name. It has a special mode bit that identifies it as a symbolic link, and its contents are the name of the real file. Because it just contains a name, that name does not actually have to exist, or may exist on a different filesystem. If you replace the named file (change its contents without affecting its name), then the link still contains the same name, and so now it points to the new file. You can easily identify a symbolic link and see the name of the file it points to.

A hard link points to the file by inode number. As such, hard links are no different than the first name of a file. There is no "real" name vs. hard link name; all hard links are equally valid names for the file. Because of this, the file you link to must actually exist and be in the same filesystem where you are trying to create the link. If you delete the original name, then the hard link still points to the same file. Because all hard links are equally valid name(s) for the file, you can not look at one and see the other names for the file; to find this, you have to go looking at every file and compare their inode number to find the other name(s) that have the same inode number.

You can tell how many names a file has from the output of ls -l. The first number after the file mode is the link count. A file with more than 1 link has other name(s) somewhere, and conversely, a file with a link count of only 1 has no (other) hard links.

4
  • If you replace the named file, then the link still contains the same name, and so now it points to the new file - I think this is not well explained. Do you mean if I replace the file where i got a symbolic link to, then the links containing name stays untouched. But it would only point to the replaced file when its filename (i.e. new file which replaced the old one) is the same as the replaced(i.e. old file which got replaced by the new one) one, correct?
    – Mike
    Apr 17, 2015 at 22:29
  • @Mike, yes: the symbolic link points to the original file name, so replacing that file means the link now points to the new file.
    – psusi
    Apr 18, 2015 at 1:32
  • Bbut only if it got the same name correct? Would symlink point to banana and I replace the file with orange then sy link can not find the file banana anymore i.e. it will not work
    – Mike
    Apr 18, 2015 at 8:08
  • 1
    @Mike, the world replace means it has the same name, otherwise you're just deleting one file and adding another ;)
    – psusi
    Apr 18, 2015 at 16:28
60

A hardlink can only work on the same filesystem, it is simply a different name for the same inode (files are internally referenced by inodes). A file will only be deleted from disk when the last link to its inode is gone (you rmd or unlinkd the last link). Hardlinks usually only work for files, not directories.

A symlink (symbolic link) is a special file containing a path to another file. This path can be absolute or relative. symlinks can work across file systems, and can even point to different files, if you for example unplug an external hard drive and replace it with another one, which has a different file at the same path. A symlink can point to either files or directories.

6
  • Thanks, this tells me how they work, but what exactly does the hard link do? And why doesn't it work for directories?
    – ste_kwr
    Feb 29, 2012 at 18:41
  • @knittl: you sure? It seems on some file systems hardlinks to directories are allowed but only root can create them. See the -d, -F, --directory switches. And yes, I have seen the note in the ln(1) page :) Feb 29, 2012 at 19:08
  • 1
    @kniwor: the easiest way to describe hardlinks is "just another name for the same file (that is, data on disk)". And – at least on my system(s) – ln cannot be used to make hardlinks to directories. There exist hardlinks to directories though, the most prominent example being . and ... I didn't want to include that in my original answer, since that would only complicate things.
    – knittl
    Mar 1, 2012 at 10:26
  • 2
    @STATUS_ACCESS_DENIED: well okay… but it's usually not a good idea. That's why I wrote »usually« in my original answer. Also see my previous comment for examples.
    – knittl
    Mar 1, 2012 at 10:29
  • so a hard link can point to the same folder/file with different names to it i.e. having different names that link to the same inode? Apr 10, 2017 at 17:38
23

One of the answers from the other thread (now linked from the top of your post) mentions this page which I think is a fairly good medium-level explanation. If you're getting lost in the ascii art, here's the tl;dr version:

  • Standard files are a pointer from the filesystem to an inode which in turn point to physical data. The file component stores its link to the filesystem (essentially its path) and a link to the inode.
  • Hard-links, are just like files. They're just an additional pointer directly to an inode.
  • Symbolic-links are separate files (including separate inode and data) that store a filesystem path to a file.

The kernel and filesystems involved translate everything transparently.

So based on that:

  • Hard-links only allow same-filesystem linking. Symlinks can point at any path.
  • Hard-links (essentially) point to absolute data. Symlinks can point to relative paths (eg ../parent.file)
  • By extension, if you move the target pointer of a hard-link (which, remember, itself is essentially just a hard-link pointing to an inode), the hard-link still works. Moving the target of a symlink would usually break the symlink.
  • Resolving a hard-link would be faster but immeasurably so. That insignificant portion of speed comes at the cost of a inflexible filesystem.

I might have confused myself a little but reading through various things, I'm struggling to find the difference between a standard file and a hardlink. The way I'm reading it is every file consists of a hardlink (storing the filename), linking to an inode that points at physical data.

Adding a hardlink just provides an inode with an additional filesystem-based pointer. Is that right?

6
  • 5
    I think you are right, every file is a pathname for an inode, and a hard link is an additional pathname for the same inode. So a hard link is no different from a normal file.
    – enzotib
    May 18, 2011 at 15:20
  • I'm trying to understand this... but you say: > "Symbolic-links are separate files (including separate inode and data) that store a filesystem path to a file." Does a symlink really have separate data? Then its just like a copy of the dir it links to, right? ... and every time something is written to the symlink, it has to be written twice to disk? Makes no sense.
    – MiniGod
    Dec 29, 2012 at 23:43
  • @MiniGod No a symlink is an inode to a block of data that stores a path to another inode (filename). Yeah, it's Matrix-like confusing but once you get it, you'll never forget :)
    – Oli
    Dec 29, 2012 at 23:48
  • @Oli I might be confused, but when you say: "including separate inode and data ", you mean that the symlink has separate data!?
    – MiniGod
    Dec 31, 2012 at 1:34
  • 1
    @MiniGod Yeah. The Symlink is an inode pointing to data (just like a normal file) and that data is a path. It's a little more clever than that — to allow transparent usage through symlinks — but that's essentially all they are.
    – Oli
    Dec 31, 2012 at 2:21
15

When to use Soft Link:

Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.

Links to directory: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.

When to use Hard Link:

Storage Space: Hard links takes very negligible amount of space, as there are no new inodes created while creating hard links. In soft links we create a file which consumes space (usually 4KB, depending upon the filesystem)

Performance: Performance will be slightly better while accessing a hard link, as you are directly accessing the disk pointer instead of going through another file. Moving file location: If you move the source file to some other location on the same filesystem, the hard link will still work, but soft link will fail.

Redundancy: If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.

2
  • Note that there is also a fast symbolic link for pathname size up to 64 bytes. It still takes up one inode, but doesn't consume the 4kb block space.
    – syockit
    Jan 21, 2015 at 10:57
  • Other use for hard links: There is a backup utility, rsnapshot, that uses extensively hard-links to mantain complete file-tree backups without duplicating files. The first backup tree has all files, but if the following backups have the same file, it is stored as a hard link, retaining this way only one copy of the file. The changed files, on the other hand, are newly copied in the next backup tree. So, you can have lots of "full" file trees but using only 1x+changed files space. This can save a lot of space in the backups and mantain direct access to all files of each backup point.
    – Fjor
    Aug 3, 2022 at 22:47
10

The confusion sets in when you try to find the difference between "the file name" and a hard link because there is none.

Every file you create consists of data on the disk and a hard link - which is a file name in a directory and a pointer to the data on the disk. End of story. When the last (or only) hard link is deleted, then the OS knows that the data is no longer needed.

From this you can see that the actual data is never deleted, only the hard link(s) are. And when it gets sufficiently crowded on the disk, the data might get overwritten by another file's data. Until then, the data from the deleted file might be recovered, but it's kinda hard to find without the hard link.

Symlinks, as previously explained, simply tell you "there's a file named <targetname> in a folder named <targetfolder>". They point to the hard link. They don't know where the data is. The hard link knows that.

0

It's very simple. Files (and directories!) are stored at addresses on the block device (HDD or whatever). Normally you have a single name mapped to an address, and that's how you get your file. A hard link is a second, third, etc name mapped to the same address. A symbolic link instead refers to the symbol - the name - and so is a second name mapped to the first name. As far as the kernel is concerned, once it reads the symbolic link target it stops and goes back to the beginning with the target value as the filename (more or less) so relative symlinks are possible but wildly unhelpful. The target name is not used above the filesystem level except if it's explicitly queried in userspace code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .