Hardlink & Softlink in Linux

Hardlink & Softlink in Linux

In UNIX, a link is a file pointer. Links in UNIX, like pointers in other programming languages, are pointers leading to a file or directory. Making links is a type of shortcut for accessing a file. Links allow several file names to refer to the same file in different locations.

Links are classified into two types:

  1. Symbolic or soft link

  2. Hardlink

When the source of the connection (what is being linked to) is moved or withdrawn, these links act differently.
Symbolic links are not updated (they simply include a string containing the target's path name); hard links always point to the source, even if moved or destroyed.
Assume we have the file abc.txt. If we make a hard link to a file and subsequently remove it, we can still access it through the hard link. However, if we make a soft link to a file and then remove it, we cannot access the file via the soft link, and the soft link becomes dangling. In general, hard links raise a location's reference count, but soft links function as shortcuts (as in Windows).

Hard Links:

  1. Each hard-linked file has the same inode value as the original, they correspond to the same physical file location.

  2. Although hard links are more flexible and remain linked even if the original or linked files are relocated around the file system, they cannot cross multiple file systems.

  3. The ls -l command displays all links, with the link column displaying the number of links.

  4. The contents of links are the actual file contents.

  5. Removing any link reduces the link count but does not affect other links.

  6. Even if we modify the filename of the original file, the hard links continue to function effectively.

  7. To avoid recursive cycles, we cannot build a hard link for a directory.

  8. If the original file is deleted, the link will still display the file's content.
    The size of any hard link file is the same as the original file, and if the content of any hard link is changed, the size of all hard link files is updated.

  9. Hard links have the disadvantage of not being able to be made for files on various file systems or particular files or directories.

  10. The command to make a hard link is:

#$ ln  <OriginalFile> <LinkFile>
$ ln Pranav.txt Pranav_hardlink

Soft Links:

  1. A soft link is analogous to the file shortcut capability found in Windows operating systems.

  2. Each soft-linked file has its own inode value, which points to the original file. Any changes to the data in either file are reflected in the other, much like hard links.

  3. Soft links can be linked between file systems, but if the source file is removed or transferred, the soft-linked file will stop working.

  4. The ls -l command displays all links that have the first column value l? and points to the original file.

  5. Soft Link only retains the path to the original file, not its contents.

  6. The soft link's size is equal to the length of the original file's path that we provided. For example, if we link a file with ln -s /tmp/hello.txt /tmp/link.txt, the file size will be 14 bytes, which is equal to the length of "/tmp/hello.txt".

  7. If we change the name of the original file, any soft links to that file become dangling, which means they are no longer useful.

  8. The command to make a soft link is:

#$ ln -s <OriginalFile> <LinkFile>
$ ln -s Pranav.txt Pranav_softlink

Types of files :

"-" --> Regular File
"d" --> Directory
"l" --> Link File
"b" --> Block File
"s" --> Socket File
"c" --> Character File
"p" --> Pipe File

[demouser@linux-system q1]$ ls -l
-rwxr-xr-x 1 root testing 1357 May 14 18:48 /etc/passwd

Where,
- --> Type of file
rwx --> Permission for the owner of the file
r-x --> Permission for group owner of the file
r-x --> Permission for other users
1 --> Link Count
root
--> Owner of the file
testing --> Group owner of the file
27M --> Size of the file
May 14 18:48 --> Last modification date
/etc/passwd
--> File name