Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


Linux - Soft and Hard Links

It is essential to understand the concepts of soft and hard links for file management in Linux. Links provide a way to reference and access files, allowing for efficient organization and resource sharing. In this guide, we will explore soft links (symbolic links) and hard links, including their differences and how to create them.

Soft links, also known as symbolic links, create separate entries that point to the original file or directory. They act as shortcuts or aliases to the target file or directory. Soft links are represented as separate files with their own names and locations.

Soft links can cross filesystem boundaries, meaning they can reference files or directories located on different file systems or partitions.

To create a soft link, use the -s option with the ln command, followed by the source file and the desired name and location of the soft link.

Let's assume we have a file named file1.txt located at /home/user/files/file1.txt. We want to create a soft link called link1 that points to file1.txt. Here is the command:

ln -s /home/user/files/file1.txt /home/user/links/link1

This command will create a soft link named link1 at /home/user/links that points to /home/user/files/file1.txt.

When running the ls command in the /home/user/links directory, you would see:

$ ls -l /home/user/links
lrwxrwxrwx 1 user user  24 May 7 09:30 link1 -> /home/user/files/file1.txt

The output shows that link1 is a symbolic link (l in the first column) with the arrow (->) indicating its target location.

Hard links, unlike soft links, directly reference the data blocks of the original file or directory. They create additional names that point to the same physical file or directory. All hard links share the same file attributes and metadata.

Hard links cannot cross filesystem boundaries. They can only reference files or directories located within the same file system or partition.

When listing the contents of a directory containing a hard link, the link appears as a separate entry with its own name, but it points to the same physical file or directory.

It is important to note that deleting a hard link does not delete the original file or directory as long as there are other hard links pointing to it. The data remains accessible until all hard links are removed.

To create a hard link, use the ln command followed by the source file and the desired name and location of the hard link.

Assuming we have the same file1.txt located at /home/user/files/file1.txt, we want to create a hard link called link2 that points to file1.txt. Here is the command:

ln /home/user/files/file1.txt /home/user/links/link2

This command will create a hard link named link2 at /home/user/links that points to /home/user/files/file1.txt.

When running the ls command in the /home/user/links directory, you would see:

$ ls -l /home/user/links
-rw-r--r-- 2 user user   0 May 7 09:30 link2
lrwxrwxrwx 1 user user  24 May 7 09:30 link1 -> /home/user/files/file1.txt

The output shows that link2 is a hard link (- in the first column & a number greater than 1 before the user owner field) with the same file attributes as the original file.

Conclusion

By understanding the differences between soft and hard links, you gain flexibility in organizing and accessing files or directories in your Linux system. Soft links provide separate entries that point to the original file or directory, while hard links create additional names that directly reference the same physical file or directory.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.