Learning never exhausts the mind
Home >  Technology > Linux > Linux Hardlinks and Softlinks

Published 27th August 2012 by

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.
Introduction to Linux Series
  1. Installing Linux Step by Step
  2. Linux Tips for Beginners
  3. Beginners guide to Reading and Finding Files in Linux
  4. Using Grep to Search for Text in Linux
  5. Understanding Linux File Permissions
  6. How to Archive, Compress and Extract files in Linux
  7. Linux Piping and Redirection
  8. Linux Hardlinks and Softlinks
  9. How to Create and Use Bash Scripts
  10. Basic Data Recovery in Linux
  11. Apache Administration on Linux
  12. MySql Administration on Linux
  13. Switching from Windows to Linux

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 an index number.


In this example, I'm going to create a file called f1 and put "Hello" into it as its contents. I'm then going to create a hard link to that file with the name f2.

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

As we said above, a hard link 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
cat f2

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 soft link 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 soft link, 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 soft link performs the same as the hard link.

cat f3

Differences between Hardlinks and Softlinks

We can see that hard links and soft links both create links to files withing the file system, and the links behave in the same way, but when should you use a hard link and when should you use a soft link?

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 hard link is removed or changes locations, soft links will no longer work (the referenced file cannot be found)

Only Softlinks can link to a directory.

Hard links do not take up storage space, while the soft link 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 the 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 entry.

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 subdirectories is 108. From this, we can say that the /etc/ directory has 108 sub directories.

Tutorial Series

This post is part of the series Introduction to Linux. Use the links below to advance to the next tutorial in the couse, or go back and see the previous in the tutorial series.

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.