Tim TrottTim TrottListen to your heart

Linux Hardlinks and Softlinks

By , Monday 27th August 2012 in Linux

Linux has the capability to create links to files, similar to shortcuts in Windows, except that they work at the filesystem level. These links are come in both hard and soft versions, each referencing different addresses.

A hard link is a reference directly to the location in the file system through an inode entry. A soft link however is a file which has the information to point to another file, the linked file itself contains the reference to the inode. An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4. Inode number also called as index number.

Hardlinks

In this example I'm going to create a file called f1 and put "hello" into it as it's contents. I'm then going to create a hardlink to that file with the name f2.

echo hello > f1
ln f1 f2
ls -li f1 f2

As we said above, a hardlink is a link directly to the inode entry. We can see this by comparing the entries on the ls command.

9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f1
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f2

We can also have a look at the file contents and see that the contents are the same.

cat f1
hello
cat f2
hello

From this we can see that the files f1 and f2 are exactly the same, f2 just points to f1.

Softlink Examples

In this example I'm going to create a softlink to f1 called f3. I'll then list the inode entries.

ln -s f1 f3

ls -li f1 f2 f3
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f1
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f2
9514546 lrw-rw-r--. 1 root root 6 Dec 27 20:48 f3 -> f1

We can see that the new softlink, f3, has a different inode number and it has a file type of link. It is a new, distinct file and points to f1. This can be seen from the f3 -> f1 indicator in the listing.

If we look at the file contents of f3, we can see that the softlink performs the same as the hardlink.

cat f3
hello

Differences between Hardlinks and Softlinks

We can see that hardlinks and softlinks both create links to files withing the file system, and the links behave in the same way, but when should you use a hardlink and when should you use a softlink?

Softlinks can be split across file systems (partitions) whereas hard links are same file system only. This is because inode 10000 on one partition references a completely different sector to that of inode 10000 on another partition.

If the original file or hardlink is removed, or changes locations, softlinks will no longer work (the referenced file cannot be found)

Only Softlinks can link to a directory.

Hardlinks do not take up storage space, while the softlink will use up the minimal block size (typically 4k)

Hard Link Count and Sub Directory Count

The listing command, ls, shows the number of hard links next to the permissions block when viewing directories. This number represents the number of hard links below that directory. In this example it shows that there are 110 "names" linked to this inode in metadata. Metadata is the extra information the operating system holds on a file.

ls -ld /etc
drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc

Using the -i parameter on with the ls command, we can see the inode number for an entry.

ls -ldi /etc
9388737 drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc

Every directory has a file with the name simply a dot. The . file represents the current directory, all directories have a . file and it will always point to the same inode enrty.

ls -ldi /etc/.
9388737 drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc/.

Every directory also contains a double dot file (..) This file is linked to the parent directory, that is the directory in which the current directory resides in.

These dot files are included in the inode count, so from the initial inode count of 110, the actual number of sub directories is 108. From this we can say that the /etc/ directory has 108 sub directories.

My website and its content are free to use without the clutter of adverts, tracking cookies, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

About the Author

Tim Trott

Tim is a professional software engineer, designer, photographer and astronomer from the United Kingdom. You can follow him on Twitter to get the latest updates.

Leave a Reply

Your email address will not be published.