In the world of Linux file systems, hard links are a powerful tool for managing files and directories. However, their use is closely tied to the concept of inodes. In this blog post, we'll explore the relationship between hard links and inodes in Linux, and how they work together to enable powerful file system management. We'll cover what hard links are, how they differ from soft links, and how they use inodes to reference files. By the end of this post, you'll have a deeper understanding of these concepts and be able to use them more effectively in your Linux file system management.
What is inode?
Inode (short for "index node") is a data structure that stores information about a file, such as its permissions, owner, timestamps, and most importantly, its data blocks. When a file is created, an inode is also created, which serves as a reference to the file's data blocks on the disk.
How to check inode number of file?
To check inode number of file, you can use stat
or ls -i
command.
What is hard link?
A hard link is a file system object that points to the same underlying data as another file or directory. In other words, it's a way to create multiple references to the same file or directory without duplicating the actual data. Hard links are created using the ln
command, and can only be created for files or directories that reside on the same file system.
One important thing to note about hard links is that they share the same inode as the original file or directory. This means that changes made to the original file or directory will be reflected in all of its hard links, and vice versa.
You can notice, that after creating a hard link the Links
property has increased and Inode number is the same in both cases.
Because hard links share the same inode as the original file or directory, they can be used to save disk space and improve performance in certain situations. For instance hard links can be used to share files between users. Example: if two users need to access the same file, you can create a hard link to the original file in each of their home directories. This will allow both users to access the file, without duplicating the data.
What is soft link?
A soft link (or symbolic link) is a special type of file that acts as a pointer to another file or directory. Unlike a hard link, a soft link is a separate file that simply contains the path to the original file or directory, rather than sharing the same inode as the original file or directory.
You can create a soft link by using the ln -s
command, followed by the path to the original file or directory, and the name of the new soft link. For example, ln -s this/is/very/complicated/path/data.pdf softlink
Soft links can be used to create shortcuts to files or directories, or to reference files or directories that have been moved to a different location. They can also be used to create more human-readable paths to files or directories, or to simplify complex directory structures.
You can notice on above screenshot, that stat
command shows different output while executed on soft link. Also while listing files in directory with ls -l
command, you can see, that our softlink have l
letter before permissions - this gives you information, that you are dealing with soft link.
One important thing to note about soft links is that if the original file or directory is moved or deleted, the soft link will become broken and no longer function properly. This is because the soft link only contains a path to the original file or directory, and does not share the same inode as the original file or directory.