Sobes.tech
Middle

What is the difference between a soft link and a hard link?

sobes.tech AI

Answer from AI

Hard link and soft link (symbolic link) are two ways to create links to files in the Unix/Linux file system.

  • Hard link:

    • An additional name for an existing file.
    • Points directly to the file's inode.
    • Multiple hard links are equivalent — the file exists as long as there is at least one hard link.
    • Cannot create a hard link to a directory (usually) or to files on other file systems.
    • If the original file is deleted, the hard link will still work because it points to the same inode.
  • Soft link (symbolic link):

    • A separate file containing the path to another file or directory.
    • Can link to files and directories.
    • Can point to a non-existent file (broken link).
    • Works like a shortcut; when accessed, the OS follows the path specified in the link.
    • Links can be created between different file systems.

Example of creation:

ln original.txt hardlink.txt      # hard link
ln -s original.txt softlink.txt   # soft (symbolic) link

In summary, a hard link is another name for the same file, while a soft link is a separate file with a path to another file.