Tuesday, March 31, 2020

Soft links and Hard links


If you need to maintain two or more copies of the same file on the system, instead of having separate physical copies, you can use one physical copy and multiple virtual copies, called links. A link is a placeholder in a directory that points to the real location of the file. There are two types of links:
a. A symbolic or soft link
b. A hard link

The hard link creates a separate file that contains information about the original file and where to locate it. When you reference the hard link file, it is just as if you are referencing the original file.

The -l parameter creates a hard link to a file. The inode number does not change, indicating that they are referring to the same file. The link count for both files now shows 2.

You can only create a hard link between files on the same physical medium. Not across file systems.

The -s parameter creates a symbolic or soft link. The inode numbers will be different. It is treated as a separate file. The file size is different.

Be careful while copying linked files. If you use the cp command to copy a file that's linked to another source file, all you are doing is making another copy of the source file. Instead of copying the linked file, you can create another link to the original file. You can have as many links to the same file with no problems. However you also do not want to create soft links to other soft-linked files. This creates a chain of links that can not only be confusing but also be easily broken, causing all sorts of problems.

No comments:

Post a Comment