The Hard Link

root's picture

Every file is a hard link. It is a label or name assigned to a set of information which resides on the hard disk. In most of the cases there is a single name assigned to a file (a single hard link).
I it possible however to create different names which all refer to the same content.
Why you would do that? There are various reasons.
Some quick examples:
- backups that keep various versions of your data can use hard links for files with the content unchanged.
- Photos in MacOS uses hard links when migrating from Aperture or iPhoto. When the migration is done, if you check the size of both databases, the sum of them can exceed the total size of your disk. That is because the master photos are hard linked, not copied to the new location (they reference to the same location on the disk).

When you create a hard link to an existing file, that content will have two references. For a better understanding, check the example below:

This is an ordinary file. The number 1 (in green) tells you there is only one reference to this data.

florian@gate:/tmp$ ls -la original 
-rw-r--r-- 1 florian florian 0 Aug 20 20:28 original

Now we create a hard link from it. You will see on the second column that 1 is 2 now. This tells you there are 2 hard links that points to the same data.

florian@gate:/tmp$ ln original originallink
florian@gate:/tmp$ ls -la original originallink 
-rw-r--r-- 2 florian florian 0 Aug 20 20:28 original
-rw-r--r-- 2 florian florian 0 Aug 20 20:28 originallink

This is one way to see if a file has more than one references. Another way is to search for the inode number of the file. For that, you must use the ls -i command.
After that, with find you can search for that inode number and see where the other hard link is located. In our example, both are in the same directory but in your case they can be far away one from another.

Example below:

florian@gate:/tmp$ ls -i original
12574 original
florian@gate:/tmp$ find . -inum 12574 -ls
 12574    0 -rw-r--r--   2 florian  florian         0 Aug 20 20:28 ./original
 12574    0 -rw-r--r--   2 florian  florian         0 Aug 20 20:28 ./originallink

If you use the standard UNIX command to delete a link, as long as is one name referencing to the file, the content is not deleted. If you do not have multiple links to the same file, the content is lost. It is lost for the normal "eye", because you can still recover it but this detail is for another article.

Getting back to the hard links, there are few rules when creating one:
- they cannot refer to files located on different computers (via NFS share for example);
- they cannot refer to files located on another partition;
- they cannot refer to directories.

At the end, I should advise you to use hard links with caution. They are not dangerous but you can loose the track of your files or make it much more difficult.
Interesting, not?

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.