Tag Archives: how to identify hard link

Difference between hard link and soft link

Learn the difference between hard links and soft links. Also discover what are they, how to create them, and how to identify them on the system.

Differences between hard link and soft link

One of the frequently asked Linux or Unix interview questions is what is the difference between hard links and soft links? In this post, we will touch base: what is the hard link & soft link,  main differences between hard and soft link, how to create a soft link and hard link, a table showing the difference between a hard and soft link, and how to identify the hard link and soft link.

Without much of distraction, lets get started :

What is hard link?

A hard link is a mirror copy of a file in the Linux or Unix system. Having said that, the original file and link file both have the same inodes. Since both share the same inode, hard links can not cross file system boundaries i.e. you can not create a hard link of a file residing in another mount point. Whenever you delete hard links, the original file and its other hard links still exist since they are all mirror copies. It just reduces the link count! Hard links have actual file content.

What is soft link?

A soft link is just a link to a file in Linux or Unix system. For understanding, you can visualize soft link as a “desktop shortcuts” in windows. Since its a link, its inode is different from the file it’s linking to. Soft links can cross file systems. You can create soft links across file systems. If you delete the original file all linked soft links fail. Since it will point to a non-existent file.

Differences between hard link and soft link :

Hard link
Soft link
Its mirror copy of original file Its link to original file
Link and original file have same inode Links has different inode than original file
Can not cross file systems Can be created across file systems
Show data even if original file deleted Fails if original file deleted
Has full content of original file Its just points to source file hence contains no data of source
It can not link directories It can link to directory
Saves your inodes in kernel since it shared same inode as source One inode is occupied hence decreasing available inodes
Takes up storage space since its a mirror copy Takes almost no storage since it contains only path of source

How to create hard link?

To create a hard link you need to use command ln followed by source (original filename) and then link name. In the below example, we are creating two hard links link1 and link2 to file testdata.

# cat testdata
This is test file with test data!
# ln testdata link1
# ln testdata link2
# ls -li
total 12
3921 -rw-r--r--. 3 root root 50 May 16 01:16 link1
3921 -rw-r--r--. 3 root root 50 May 16 01:16 link2
3921 -rw-r--r--. 3 root root 50 May 16 01:16 testdata

You can see above we used ln command to create hard links. Following which we listed their inodes with -i option of command ls. You can see, both links are having the same inode (3921) as the original file (see the first column). Also, the size of hard links is the same as the original file since they contain the same data as the source file.

Now we will delete the original file and see if we can still have data of it from link files.

# rm testdata
rm: remove regular file `testdata'? y

# ls -li
total 8
3921 -rw-r--r--. 2 root root 50 May 16 01:16 link1
3921 -rw-r--r--. 2 root root 50 May 16 01:16 link2

# cat link1
This is test file with test data!

Yes. Data still can be fetched from hard links even after deleting the original file since those are mirror copies!

How to create soft link?

For creating soft links, the same ln command can be used but need to specify -s option (soft link). The rest of the command format remains the same.

# ln -s testdata link1
# ln -s testdata link2
# ls -li
total 4
3921 lrwxrwxrwx. 1 root root  8 May 16 01:26 link1 -> testdata
3925 lrwxrwxrwx. 1 root root  8 May 16 01:26 link2 -> testdata
3923 -rw-r--r--. 1 root root 34 May 16 01:25 testdata

In the above example, after creating soft links if you observe inode numbers of soft links are different from the original file. Also, link size is pretty small since they have only path details of the source, not data. Another observation is soft links shows which file they are pointing to in ls output at last column which was not the case in hard links.

Now, we will delete original file and try to access links.

# cat link2
This is test file with test data!
# rm -f testdata
# cat link2
cat: link2: No such file or directory

You can see in the above output, previously we can use link2 properly. After deleting the original file, links are broken and throwing errors when we try to access them!

How to identify hard link and soft link?

From the above examples, you can figure out soft links are easy to identify. Soft links are marked as link files lxxxxxxxxx in special file bit (first column of ll output). They are even displaying pointers and source file names in the last column of ll output (link1 -> testdata).

Hard links are not that pretty straight forward to identify. You need to use inode option -i in ls command and then you need to check for duplicate inodes. This is a manual method. You can even use find command with -same file option. It will then scan inodes and list files with the same inodes (i.e. hard links!)

# find /path -xdev -samefile testdata

The above command will scan /path directory and will list all files having the same inode as testdata file. Which means it will list all hard links to testdata file!