Wednesday, January 26, 2022

SOFT & HARD LINKS

Soft links

Commonly referred to as symbolic links, soft links link together non-regular and regular files. They can also span multiple filesystems. By definition, a soft link is not a standard file, but a special file that points to an existing file. Format: 

ln -s (file path [original] you want to point to) (the soft path pointing to the original)

Example:

ln -s  /home/simon/snap/jgrasp/bin/jgrasp   /usr/bin/jgrasp

Hard links

The concept of a hard link is the most basic. Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem. Creating an additional hard link to a file means a few different things. The syntax is:

ln (original file path) (new file path)

Example:

ln  TestFile  /tmp/link2TestF

First, you create a new filename pointing to the exact same data as the old filename. This means that the two filenames, though different, point to identical data. When changes are made to one filename, the other reflects those changes. The permissions, link count, ownership, timestamps, and file content are the exact same. If the original file is deleted, the data still exists under the secondary hard link. The data is only removed from your drive when all links to the data have been removed. If you find two files with identical properties but are unsure if they are hard-linked, use the "ls -i" command to view the inode number. Files that are hard-linked together share the same inode number. Example:

ls  -li  TestFile  /tmp/link2TestF

Conclusion

  • A hard link always points a filename to data on a storage device.
  • A soft link always points a filename to another filename, which then points to information on a storage device.



Reference: RedHat.

---