How to create symbolic and hard links

Overview

A symbolic link stores a path to another item, much like a shortcut. A hard link is another directory entry for the same file data.

Symbolic links can cross filesystems and point to directories; hard links usually cannot.

Examples

ln -s ~/Documents/notes.txt notes-link
ln original.txt second-name.txt
ls -li original.txt second-name.txt
readlink notes-link

Options and behavior

  • ln -s target link-name creates a symbolic link
  • ln target link-name creates a hard link
  • -f replaces a conflicting destination and should be used carefully
  • A symbolic link becomes broken if its target moves
  • Hard-linked file names share an inode until all names are removed

Tips and safety

Use absolute targets when a symlink must work regardless of its location; use relative targets when moving the whole directory tree together.

Run ls -l to inspect link destinations before deleting or editing through a link.