Posted: . At: 9:04 AM. This was 6 years ago. Post ID: 12535
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


What a file hardlink means on Linux. And why symlinks are better.


Linux supports the creation of file hardlinks. This is a copy of a file that has the same inode as the original. If the copy is edited, the original is too.

4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ ln missile.jpg missilenew.jpg
4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ ls -hula -i -1
total 264K
14885009 drwxr-xrwx+  2 jason jason 4.0K Oct  4 07:48 .
14811151 drwxr-xr-x+ 33 jason jason 4.0K Oct  4 07:47 ..
14821480 -rwxr-----   2 jason jason 128K Sep 20 18:23 missile.jpg
14821480 -rwxr-----   2 jason jason 128K Sep 20 18:23 missilenew.jpg

Both of these files have the same inode number. This means they are the same file, I edited the copy in the Gimp and the original changed too. They are both the same file and it is hard to find out which is the original. That is the problem with a hardlink. They have the same creation date as well. A Symlink on the other had is a symbolic link to a file.

Create one like this.

4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ ln -s missile.jpg missileLink.jpg

Then we can see that it is a link to a file, not an exact copy.

4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ ls -hula
total 304K
drwxr-xrwx+  2 jason jason 4.0K Oct  4 08:40 .
drwxr-xr-x+ 33 jason jason 4.0K Oct  4 07:47 ..
-rwxr-----   2 jason jason 146K Oct  4 08:06 missile.jpg
lrwxrwxrwx   1 jason jason   11 Oct  4 08:40 missileLink.jpg -> missile.jpg
-rwxr-----   2 jason jason 146K Oct  4 08:06 missilenew.jpg

The link may be deleted without affecting the parent file. This is a great way to have a file in more than one directory, without wasting disk space.

In this example, I renamed the original file and the symlink is now broken.

4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ mv missile.jpg missile2.jpg
4.4 Thu Oct 04 jason@Yog-Sothoth 0: $ ls -lLi
ls: cannot access 'missileLink.jpg': No such file or directory
total 296
14821480 -rwxr----- 2 jason jason 149445 Oct  4 08:06 missile2.jpg
       ? l????????? ? ?     ?          ?            ? missileLink.jpg
14821480 -rwxr----- 2 jason jason 149445 Oct  4 08:06 missilenew.jpg
Broken symlink properties.
Broken symlink properties.

Just rename it back again, and it is all good. So symlinks are the way to go to duplicate a file without wasting space, this is used a lot in the Linux filesystem. I renamed the original file and the hardlinked file still shows up as normal. So to summarize, it is a unique file that shares the same inode number as the original file. But it is better to use symlinks to duplicate a file in another place.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.