Linux file systems offer various ways to manage files and directories efficiently. Among these methods, soft and hard links play a crucial role in establishing connections between files. Understanding the differences between them is essential for any Linux user or administrator. In this article, we will learn What are Links in Linux – soft and hard links and their uses.
In Linux we have two types of links:
In Linux, soft and hard links serve as pointers to files or directories, facilitating access and organization. Soft links, also known as symbolic links, are references to the file’s pathname. They act as shortcuts, redirecting to the target file’s location. On the other hand, hard links create multiple directory entries pointing to the same inode, essentially sharing the same data blocks.
Like we have shortcuts in Windows Operating system, we have links in Linux. When we create a link, then whatever change we make in the source file, that will be done in the link also.
Computer doesn’t understand names, it understands numbers. When we create a file, a number is assigned to that file on HDD. That number is called inode. We have inode “index node” which is a pointer on number of a file on HDD. The inode acts as a pointer to the actual data blocks of a file or directory and contains essential information about that file or directory.
It is a data structure on a filesystem that stores information about a file or a directory, except its name and actual data.
When you interact with files on a Linux system, you are often interacting with their corresponding inodes indirectly.
Note: We can’t create a link (soft or hard) within same directory with same name. Therefore go to another directory like /tmp.
Soft links are created using the ln -s
command, followed by the target file and the name of the link. Unlike hard links, soft links can span across different file systems. They provide flexibility and convenience, allowing users to create symbolic references to files or directories located elsewhere. However, they are more vulnerable to link breakage if the target file is moved or deleted.
Syntax:
ln -s /path/to/target/file link_name
Example:
touch sourcefile
cd /tmp
/tmp]$ ln -s /tmp/sourcefile
echo “This is source file” > sourcefile // > overrides and >> appends
This will make same change in both sourcefile and the link /tmp/sourcefile as both are linked.
Inodes of source file and its link are different and hence both are stored differently on the HDD.
~]$ ls -lrti /tmp/sourcefile
68133625 -rw-rw-r–. 1 vagrant vagrant 9 Nov 9 11:30 /tmp/sourcefile
~]$ ls -lrti sourcefile
83 lrwxrwxrwx. 1 vagrant vagrant 15 Nov 9 11:27 sourcefile -> /tmp/sourcefile
If source file is removed, it shows it as red which means broken.
Hard links, created with the ln
command, establish direct connections between directory entries and inodes. They share the same inode number and data blocks, making them indistinguishable from the original file. Hard links cannot span different file systems and are limited to the same partition. While they offer efficiency in storage and performance, any changes made to one hard link affect all others, as they point to the same data blocks.
Hard link works within the same partition. If /home dir not on same partition as /tmp, we can get error: invalid cross-device link. An “invalid cross-device link” error occurs in Linux when attempting to create a hard link between files on different storage devices or partitions. This error indicates that hard links cannot span across devices due to differences in file system structures and limitations.
Syntax:
ln /path/to/target/file link_name
If the source file is removed/renamed, the soft link will be removed and the destination link file will also be removed.
If source file is removed/renamed, the hard link and will not be removed and the destination link file will stay there.
From the above diagram, we see that my-soft-link goes through the myfile.txt to inode but my-hard-link goes to inode directly. Therefore, if myfile.txt deleted, softlink also broken but hardlink remains intact.
Soft and hard links differ significantly in structure and functionality. Soft links provide flexibility across file systems and are easily recognizable as symbolic references. However, they incur a slight performance overhead due to additional indirection. Hard links, while restricted to the same partition, offer efficiency in storage and access speed. Understanding the specific use cases and requirements is essential for choosing the appropriate link type.
A Linux hard link establishes a direct connection between the directory entry and the inode of the target file. When a hard link is created, a new directory entry is generated with the same inode number as the original file. Thus, both directory entries point to the same data blocks on the disk, enabling seamless access to the file’s contents.
In Linux, a shortcut link, typically referred to as a symbolic link or soft link, serves as a pointer to the target file’s pathname. When a soft link is created, it contains the pathname of the target file rather than its inode. This allows for flexible referencing across different file systems, providing convenience in file management and organization.
When working with soft and hard links in Linux, it’s essential to adhere to best practices to ensure efficient management and data integrity. Avoiding circular references, maintaining consistent naming conventions, and regularly checking link integrity are crucial steps. Additionally, understanding the limitations and considerations of each link type helps prevent errors and optimize file system performance.
Soft and hard links are fundamental components of Linux file systems, offering versatile methods for file organization and access. While soft links provide flexibility and convenience across file systems, hard links offer efficiency in storage and access speed within the same partition. By understanding the differences and best practices for working with each link type, Linux users can optimize file management and enhance system performance.
You may also like: TCP SACK (Selective Acknowledgement)
lrwxr-xr-x 1 mohit.singh 347102967 10 Feb 3 13:19 softlink -> myfilesoft
You often ask yourself, “Why is the server so slow today?” Whether it's your application…
The Google Pixel 9 Pro Fold is the latest foldable phone in the Google Pixel…
Looking to develop locally with a database? Vagrant offers a powerful solution for creating isolated…
Are you a Devops Engineer or an IT engineer who wants to spin up a…
Using Kubernetes and Docker offers several advantages, particularly for scaling, managing, and orchestrating containerised applications.…
Apple’s iPhone 16 series is set to launch in September 2024, with significant updates and…
View Comments