Hardlinks and Softlinks in Linux Explained With Examples

Linux has the capability to create Softlinks and Hardlinks to files, similar to shortcuts in Windows, only they work on filesystem level.

By Tim Trott | Linux Tips and Tutorials | August 27, 2012
Introduction to Linux

This article is part of a series of articles. Please use the links below to navigate between the articles.

  1. How to Download and Installing Linux Step by Step For Beginners
  2. Essential Guide to Working with Files in Linux
  3. Understanding Linux File Permissions and Permission Calculator
  4. How to Archive, Compress and Extract Files in Linux
  5. Linux Piping and Redirection Explained
  6. Hardlinks and Softlinks in Linux Explained With Examples
  7. How to Create and Use Bash Scripts in Linux
  8. Data Recovery in Linux - How To Recover Your Data after Drive Failures
  9. Apache Web Server Administration Cheat Sheet for Linux
  10. Essential MariaDB and MySql Administration Tips on Linux
  11. How to Switching from Windows to Linux - A Complete Guide

These links 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 the 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 is also called an index number.

What are Linux Hardlinks

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
hello
cat f2
hello

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

Linux 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
hello

Differences between Hardlinks and Softlinks

We can see that hard links and soft links both create links to files within 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 the 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 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, which is the directory in which the current directory resides.

These dotfiles 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.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.