What is the difference between a soft (symbolic) link and a hard link?

Solution 1:

A hard link traditionally shares the same file system structures (inode in unixspeak), while a soft-link is a pathname redirect.

  • Hardlinks must be on the same filesystem, softlinks can cross filesystems.
  • Hardlinked files stay linked even if you move either of them (unless you move one to another file system triggering the copy-and-delete mechanism). Softlinked files break if you move the target (original), and sometimes when you move the link (Did you use an absolute or relative path? Is it still valid?).
  • Hardlinked files are co-equal, while the original is special in softlinks, and deleting the original deletes the data. The data does not go away until all hardlinks are deleted.
  • Softlinks can point at any target, but most OS/filesystems disallow hardlinking directories to prevent cycles in the filesystem graph (with the exception of the . and .. entries in unix directories which are hard links).
  • Softlinks can require special support from filesystem walking tools. Read up on readlink (2).

(Some details brought back to mind by mat1t. Thanks.)

Solution 2:

The summary is that a symbolic / short link acts as a shortcut to the first file's location, whereas a hardlink is a shortcut to the file on the disk.

If you delete the target of a soft link then the soft link will cease to work, but if you delete one copy of a hard link, the file will remain on the disk until all hard links to it are removed. In effect all filenames are hardlinks to the file on the disk.

There are also certain restrictions, eg I don't think you can create hard links of folders, but you can create soft links of them. Soft links can also point to files/folders on different drives and partitions whereas hard links can't.