Soft and Hard links on UN*X System.
Hard and symbolic links offer different ways to access files and directories in Linux. Hard links reference the same data on disk, while symbolic links serve as shortcuts to file paths. Both have unique benefits, making them useful tools for efficient file management and navigation.
Hard links and symbolic links are two distinct methods of referencing files in Linux and Unix-like operating systems. These links help organize files within the filesystem. A hard link is an additional name for an existing file, pointing directly to the file's inode (the data structure storing file information). Essentially, it's a synced carbon copy that shares the same data.
In contrast, a symbolic link acts like a shortcut, pointing to the file itself rather than its inode. This makes it a special file that indirectly links to the data.
To better understand how symbolic and hard links work, it's important to first grasp the concept of inodes, the core component of how Linux manages file storage.
What is an inode?
An inode is a data structure that stores essential information about files and directories, such as metadata and their physical location on the hard drive. It's like the numerical equivalent of a full address, enabling the operating system to access details about a file, including permissions and data location. When a file is moved to a different folder, its inode is updated automatically to reflect its new position on the hard drive, ensuring the system can still retrieve the file accurately.
Let's create a hard link to an existing file using the command ln
than commandls -i
to verify the inode number.
1. Create a hard link to an existing file using the ln command:
$ touch SNUB.txt
$ ln SNUB.txt SNUB1.txt
$ ls -li
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey 38 Feb 10 12:19 ln.txt
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey 38 Feb 10 12:19 1ln.txt
2. List the files along with their inode numbers using the ls -i
command:
ls -li
Output
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey 38 Feb 10 12:19 SNUB.txt
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey 38 Feb 10 12:19 SNUB1.txt
This will display the inode numbers of both the original file (existingfile) and the hard link (hardlinkfile). Since hard links share the same inode, you’ll see both files with identical inode numbers, here: #1053413.
What is a hard link?
A hard link is a direct reference to a file's inode, meaning it points directly to the file's data on disk. Hard links can only be created for files, not directories, and must reside on the same file system. If the original file’s contents or location changes, the hard link remains valid because it references the same inode. There is no dependency on the original file's path. To create a hard link, you use the ln
command, specifying the source file and the name of the hard link. For cross-volume linking, symbolic links are required instead.
$ ln SNUB.txt SNUB1.txt
What are symbolic links?
Symbolic (soft) links are special files that act as shortcuts, pointing to another file or directory in the file system. Unlike hard links, symbolic links don't contain the target file's data but reference its location. This allows symbolic links to point to directories or files across different file systems, including remote systems over NFS. Modifying the symbolic link affects the original file because it directly references the file's path, not its inode. However, if the original file is moved or replaced, the symbolic link will break, as it no longer points to the correct path.
Let's create a directory monkeyGroup and make a soft link to the home directory of user Batman.
$ mkdir monkeyGroup
$ ln -s /home/snubmonkey/monkeyGroup /home/batman
Let's list their inode numbers:
$ ls -i
Output
524302 drwxrwx— 2 snubmonkey snubmonkey 4096 Feb 10 09:36 monkeyGroup
now, let's su
(substitute user) to user: batman
$ su batman
Password:
$ cd
$ ln -li
Output
1053420 lrwxrwxrwx 1 root root 16 Feb 10 09:52 monkeyGroup -> /home/batman/monkeyGroup
As you can see, the inode numbers (524302 vs 1053420) and file permissions (drwxrwx vs lrwxrwxrwx—) differ, even though the symbolic link contains the same content as the source file.
While both hard and symbolic links offer ways to reference files, they differ in structure and behavior. Hard links share the same inode and file contents, while symbolic links act as shortcuts pointing to the file's path. Understanding these differences is key to effectively managing files and directories in Linux.
We hope this was of great use!