What Are Hard Links And Why Would You Use Them?

Linux Hard Links
Linux Hard Links.

Definition Of A Hard Link

There are 2 types of links that you can create within Linux:

A symbolic link is much like a desktop shortcut within Windows. The symbolic link merely points to the location of a file. 

Deleting a symbolic link has no effect on the physical file that the link is pointing to. 

A symbolic link can point to any file on the current file system or indeed other file systems.

This makes it more flexible than a hard link.

A hard link is actually the same file that it links to but with a different name. The easiest way to think of it is as follows:

Imagine you were born with the first name Robert. Other people might know you as Robbie, Bob, Bobby or Rob. Each person would be talking about the same person. 

Each link adds 1 to a counter of links which means to delete the physical file you have to delete each and every one of the links.

Why Use Hard Links?

Hard links provide an efficient way to organise files. I think the easiest way to describe this is with an old Sesame Street episode I watched as a child.

Bert told Ernie to tidy away all his things and so Ernie set about his task. First of all he decided to tidy away all the red things. "The fire engine is red". So Ernie puts the fire engine away.

Next Ernie decides to put away all the toys with wheels. The fire engine has wheels.

So Ernie tidied the fire engine away.

Needless to say Bert comes home to find exactly the same mess as before but Ernie had tidied the fire engine away half a dozen times.

Imagine that the fire engine was just a picture of a fire engine. You could have different folders on your machine as follows:

  • Photos of red things
  • Photos of vehicles
  • Photos of emergency services

Now you could create a copy of the photo and place it in each of the folders. This means you have three copies of the same file taking up three times the space.

Categorising photos by making copies of them might not take up too much space but if you tried the same thing with videos you would significantly reduce your disk space.

A hard link takes up no space at all. You could therefore store the same video in various different categories (i.e. by year, genre, cast, directors) without reducing your disk space.

How To Create A Hard Link

You can create a hard link using the following syntax:

ln path/to/file /path/to/hard/link

For example in the image above I have an Alice Cooper music folder called Trash in the path /home/gary/Music/Alice Cooper/Trash. In that folder there are 10 songs one of which is the classic Poison.

Now Poison is a rock track so I created a folder called Rock under the music folder and created a hard link to Poison by typing the following file:

ln "01 - Poison.mp3" "~/Music/rock/Poison.mp3"

This is a good way to organise music.

How To Tell The Difference Between A Hard Link And A Symbolic Link

You can tell if a file has a hard link by using the ls command:

ls -lt

A standard file without links will look as follows

-rw-r--r-- 1 gary gary 1000 Dec 18 21:52 poison.mp3

The columns are as follows:

  • -rw-r--r-- = permissions
  • 1 = number of links
  • gary = group
  • gary = owner
  • 1000 = file size
  • Dec 18 = date
  • 21:52 = time
  • poison.mp3 = filename

If this was a hard link the output would look as follows:

-rw-r--r-- 2 gary gary 1000 Dec 18 21:52 poison.mp3

Notice that the number of links column shows 2. Every time a hard link is created that number will increase.

A symbolic link will look as follows:

-rw-r--r-- 1 gary gary 1000 Dec 18 21:52 poison.mp3 -> poison.mp3

You can clearly see that one file is pointing to another.

How To FInd All Hard Links To A File

All files in your Linux system contain an inode number which uniquely identifies the file. A file and its hard link will contain the same inode.

To see the inode number for a file type the following command:

ls -i

The output for a single file will be as follows:

1234567 filename

To find the hard links for a file you just need to do a file search for all the files with the same inode (i.e. 1234567).

You can do that with the following command:

find ~/ -xdev -inum 1234567


More From Us