Linux Soft Links and Hard Links

Linux Soft Links and Hard Links with Practical Examples In Linux, there are two types of links: soft links (symbolic links) and hard links. Both are used to create shortcuts or references to files and directories, but they behave differently. 1.Soft Link (Symbolic Link) A soft link, also called a symbolic link, is like a shortcut in Windows. It points to the location of a file or directory. If the original file is deleted, the soft link becomes broken and unusable. Key Features: Can link to files and directories. If the target is removed or renamed, the symbolic link becomes broken. Does not occupy extra space on the disk except for the link itself. Creating a Soft Link: ln -s /path/to/target /path/to/link Example of Soft Link: 1.Create a file: echo "This is a test file" > original_file.txt 2.Create a symbolic link to this file: ln -s original_file.txt soft_link.txt 3.Check the symbolic link: ls -l Output: lrwxrwxrwx 1 user user 15 Aug 22 14:12 soft_link.txt -> original_file.txt -rw-rw-r-- 1 user user 20 Aug 22 14:11 original_file.txt 4.Access the content through the soft link: cat soft_link.txt Breaking the Soft Link: 1.Delete the original file: rm original_file.txt 2.Try accessing the soft link: cat soft_link.txt Output: cat: soft_link.txt: No such file or directory The link is broken because the target file is gone. 2.Hard Link A hard link is a reference to the same inode (data) on disk as the original file. Deleting the original file does not affect the hard link because both links point to the same data. Key Features: Can only link to files, not directories. Both the original file and the hard link share the same inode number. Even if the original file is deleted, the hard link continues to access the file's content. Occupies no extra space, both the file and hard link share the same data. Creating a Hard Link: ln /path/to/target /path/to/link Example of Hard Link: 1.Create a file: echo "This is a test file" > original_file.txt 2.Create a hard link to this file: ln original_file.txt hard_link.txt 3.Check the hard link: ls -li Output: 123456 -rw-rw-r-- 2 user user 20 Aug 22 14:11 hard_link.txt 123456 -rw-rw-r-- 2 user user 20 Aug 22 14:11 original_file.txt Notice both hard_link.txt and original_file.txt have the same inode number (123456), meaning they are pointing to the same data. 4.Access the content through the hard link: cat hard_link.txt Deleting the Original File: 1.Remove the original file: rm original_file.txt 2.Access the file through the hard link: cat hard_link.txt Output: This is a test file The hard link still works even though the original file was deleted, because the data remains on the disk until all hard links are removed. 4.Conclusion Use soft links when you need shortcuts or references across different filesystems, or when you need to link to directories. Use hard links when you want to create multiple names for the same file that can still work even if one of the links is deleted.

Apr 13, 2025 - 12:07
 0
Linux Soft Links and Hard Links

Linux Soft Links and Hard Links with Practical Examples

In Linux, there are two types of links: soft links (symbolic links) and hard links. Both are used to create shortcuts or references to files and directories, but they behave differently.

1.Soft Link (Symbolic Link)

A soft link, also called a symbolic link, is like a shortcut in Windows. It points to the location of a file or directory. If the original file is deleted, the soft link becomes broken and unusable.

Key Features:

  • Can link to files and directories.
  • If the target is removed or renamed, the symbolic link becomes broken.
  • Does not occupy extra space on the disk except for the link itself.

Creating a Soft Link:

ln -s /path/to/target /path/to/link

Example of Soft Link:
1.Create a file:

echo "This is a test file" > original_file.txt

2.Create a symbolic link to this file:

ln -s original_file.txt soft_link.txt

3.Check the symbolic link:

ls -l

Output:

lrwxrwxrwx 1 user user   15 Aug 22 14:12 soft_link.txt -> original_file.txt
-rw-rw-r-- 1 user user   20 Aug 22 14:11 original_file.txt

4.Access the content through the soft link:

cat soft_link.txt

Breaking the Soft Link:
1.Delete the original file:

rm original_file.txt

2.Try accessing the soft link:

cat soft_link.txt

Output:

cat: soft_link.txt: No such file or directory

The link is broken because the target file is gone.

2.Hard Link

A hard link is a reference to the same inode (data) on disk as the original file. Deleting the original file does not affect the hard link because both links point to the same data.

Key Features:

  • Can only link to files, not directories.
  • Both the original file and the hard link share the same inode number.
  • Even if the original file is deleted, the hard link continues to access the file's content.
  • Occupies no extra space, both the file and hard link share the same data.

Creating a Hard Link:

ln /path/to/target /path/to/link

Example of Hard Link:
1.Create a file:

echo "This is a test file" > original_file.txt

2.Create a hard link to this file:

ln original_file.txt hard_link.txt

3.Check the hard link:

ls -li

Output:

123456 -rw-rw-r-- 2 user user   20 Aug 22 14:11 hard_link.txt
123456 -rw-rw-r-- 2 user user   20 Aug 22 14:11 original_file.txt

Notice both hard_link.txt and original_file.txt have the same inode number (123456), meaning they are pointing to the same data.

4.Access the content through the hard link:

cat hard_link.txt

Deleting the Original File:
1.Remove the original file:

rm original_file.txt

2.Access the file through the hard link:

cat hard_link.txt

Output:

This is a test file

The hard link still works even though the original file was deleted, because the data remains on the disk until all hard links are removed.

4.Conclusion

Use soft links when you need shortcuts or references across different filesystems, or when you need to link to directories.
Use hard links when you want to create multiple names for the same file that can still work even if one of the links is deleted.