First we Need To Know About Inodes
Hard links and soft link is an important concept in the Linux file system, which relates to the index node in the file system (inode). Inode is one of the four basic concepts in Linux virtual file system (VFS). Through the analysis of the relation and the difference between hard links and soft links, we can better understand Linux VFS.
We know that the file has file name and data, which is divided into two parts: user data and metadata. The user data, namely the file data blocks (data block), data block is a place that store the real content. metadata is additional properties of file, such as: file size, created time of file, owner information of file. In Linux, the inode in the metadata, (inode is a part of the file metadata but which do not contain a file name, inode, namely the inode number) is uniquely identifies of the file rather than the file name. The file name is only for memory and is convenient for people to use. through the inode number, system can find the correct file data block. The below figure shows how the program obtain the contents of the file by file name.
In order to solve the problem of file sharing, Linux system introduce two links: hard link (hard link) and soft link (also called a symbolic link). A link will resolve file sharing for the Linux system. If a inode is correspond to multiple file name, said these files as hard links. In other words, a hard link is the same file with multiple aliases. Hard links can be created by the command link or ln.
Soft link is a common file, there is only a little special for the content of data block. A soft link has its own inode number and the user data block , pls see the below figure. So soft link is created without many restrictions similar with hard links:
- A soft link has its own file attributes and authority
- You can create a soft link to the file or directory does not exist
- A soft link can cross file system
- Soft links can be created to the file or directory
- Create a soft link, link count i_nlink does not increase
- Removing the soft link does not affect the pointed file, but the original file if it is to be deleted, then the soft links is called the dead link
Q. What is the one line answer to the question “What is the main difference between hard links & soft links” ?
A. A softlink will have a different Inode number than the source file, which will be having a pointer to the source file but hardlink will be using the same Inode number as the source file.
Q. How can I find all the Soft Links in my system ?
A. Use this command for the same “find /etc -type l -exec ls -li {} \;”
Q. How can I find all the files having Hard Links in my system ?
A. Use this command for the same “find / -links +2 -type f -exec ls -li {} \;”
Q. How to find whether a file is a softlink ?
A. Simply using this command “ls -l” will tell you whether a file is pointing to some other file or not.
Q. How to check whether a file have any softlink pointing to it ?
A. Till now, I am not aware of any way to do that. If I will find any, I will surely update my post.
Q. How can I find out the source file of a hard link ?
A. No, you can’t find out the source file of a hard link. Once hard link is created, there is no way to tell which was the first file created.
Q. Can I make a Soft link to a Hard link and Vice Versa ?
A. Yes, both soft links and hard links acts as normal files of the file system, so you can do both.
How file is deleted having hard links:
So, as it’s pretty clear from the above article that hard links are just the reference to the main file location, and even if you delete one link, the data will still be intact. So, to remove a hard link, you need to remove all the links, which are referring to the file. Once the “link count” goes to “0”, then the inode is removed by the filesystem, and file is deleted.
When to use Soft Link:
- Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.
- Links to directory: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.
When to use Hard Link:
- Storage Space: Hard links takes very negligible amount of space, as there are no new inodes created while creating hard links. In soft links we create a file which consumes space (usually 4KB, depending upon the filesystem)
- Performance: Performance will be slightly better while accessing a hard link, as you are directly accessing the disk pointer instead of going through another file.
- Moving file location: If you move the source file to some other location on the same filesystem, the hard link will still work, but soft link will fail.
- Redundancy: If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.
PRACTICAL:
NOTE: The only information not included in an inode, is the file name and directory. These are stored in the special directory files.
Symbolic links are created with the “ln” command in linux. The syntax of the command is:
$ ln -s
-s = This flag tells to create a symlink (if you don’t use this it will create a hard link, which we will talk about soon).
For Example, if you want to create a soft link of one fo your favorite application, like gedit, on your desktop, use the command like this:
$ ln -s /usr/bin/gedit ~/Desktop/gedit
I hope now the concept of Soft Links should be clear.
You can create a hard link with the same command “ln” like this
# ln
So, to create a hard link of gedit program on your desktop, you will use the command like this:
# ln /usr/bin/gedit ~/Desktop/gedit
Now, the bigger question is, who will decide what is better and when to use soft link or hard link
|
0 comments:
Post a Comment