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.
"A picture is worth a thousand words."
And, "An example is worth a hundred paragraphs..."
Create two files:
Enter some data into them:
And as expected:
Let's create hard and soft links:
Let's see what just happened:
Changing the name of
blah1
does not matter:blah1-hard
points to the inode, the contents, of the file - that wasn't changed.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; ifblah2
is deleted,blah2-soft
is just a link to a non-existing file.source: blatantly copying it from StackOverflow!
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.
In Linux/Unix, Shortcuts are known as Links
Links are of two types: soft links (symbolic links) or hard links.
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.
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.)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 namedfirefox
.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.
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).
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.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
rm
d orunlink
d 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.
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:
The kernel and filesystems involved translate everything transparently.
So based on that:
../parent.file
)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?
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.
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.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.