Linux Hardlinks and Softlinks
- Linux Tips for Beginners
- Beginners guide to Reading and Finding Files in Linux
- Using Grep to Search for Text in Linux
- Understanding Linux File Permissions
- How to Archive, Compress and Extract files in Linux
- Linux Piping and Redirection
- Linux Hardlinks and Softlinks
- Basic Data Recovery in Linux
- Essential Guide to Working with Files in Linux
- Apache Administration on Linux
- MySql Administration on 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 hello cat f2 hello
From this, we can see that the files f1 and f2 are exactly the same, f2 just points to f1.
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 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.
Last updated on: Saturday 17th June 2017