Within the Unix/Linux file system, linking lets you create file shortcuts to link one or more files. Linux provides two different linking methods (hard and soft links) which will further be explained in this tutorial.
To better understand how linking works, it may be easier to first understand the concept of inodes. Each file contains an index number called an inode. An inode (typically represented by a numerical value) is a data structure comprised of metadata information about the associated file. Linking utilizes inodes by pointing a file towards a specific inode value.
To view a file’s inode number, issue the command ls –i within the command prompt:
[Dillon@localhost Tutorial]$ ls -i
929447 example_file_1.txt
929448 example_file_2.txt
929449 example_file_3.txt
Hard links are beneficial as they are more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems. Lastly, hard links can only be assigned to files and not directories as to avoid recursive loops.
In the following example, the original file and hard linked file point to the same inode value of “101”.
[Dillon@localhost Tutorial]$ ln original_file new_hardlink_file
[Dillon@localhost Tutorial]$ ls –l
-rw-rw-r--. 2 Dillon Dillon 28 Dec 28 14:28 new_hardlink_file
-rw-rw-r--. 2 Dillon Dillon 28 Dec 28 14:28 original_file
A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other.
Soft links are beneficial as they can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).
In the following example, the softlink points to the location of the original file. Notice that both files contain different inode values.
[Dillon@localhost Tutorial]$ ln -s original_file new_softlink_file
[Dillon@localhost Tutorial]$ ls -l
lrwxrwxrwx. 1 Dillon Dillon 28 Dec 28 14:32 new_softlink_file -> original_file
-rw-rw-r--. 1 Dillon Dillon 28 Dec 28 14:32 original_file
To fix a hanging link, you must first determine the original file that the soft link pointed to. Issue the command ls -l command to verify this file – the broken link will typically be indicated by red colored text. In this example, the file “original_file” has been moved which resulted in the associated soft link to become broken.
[Dillon@localhost Tutorial]$ ls -l
lrwxrwxrwx. 1 Dillon Dillon 13 Dec 28 14:32 new_softlink_file - original_file
[Dillon@localhost Tutorial]$ find -name "original_file"
Folder 1/original_file
[Dillon@localhost Tutorial]$ unlink new_softlink_file
[Dillon@localhost Tutorial]$ ls -l
[Dillon@localhost Tutorial]$ ln -s Folder_1/original_file new_softlink_file
[Dillon@localhost Tutorial]$ ls -l
lrwxrwxrwx. 1 Dillon Dillon 25 Dec 28 15:07 new_softlink_file -> Folder_1/original_file
- Issue the ln [original filename] [link name] command to create a hard link
- Original File and Hard Linked file contain the same inode value
- Creates a mirror copy of the file
- Any changes to either original file or hard linked file are reflected in the other file
- Benefit – more flexible and remain linked if either the original or hard linked file is moved
- Negative – unable to cross different file systems
Soft Links:
- Issue the ln -s [original filename] [link name] command to create a soft link
- Similar to shortcut feature in Windows Operating system
- Original File and Hard Linked file contain different inode values
- Each soft link file points to the original file’s inode
- Any changes to either original file or soft linked file are reflected in the other file
- Benefit – soft linked file can cross different file systems
- Negative – if original file is deleted or moved, the soft link is broken (hanging link)