Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

Hard links vs symbolic links

Compare hard links and symbolic links in Linux: how each works, what happens on delete or move, cross-filesystem limits, directories, and when to choose which.

Linux can give one file more than one name in two very different ways. A hard link is another directory entry that points at the same inode, so both names are equally real. A symbolic link (symlink) is a tiny separate file whose content is a path; the system follows that path whenever you open it.

The difference shows up the moment something moves or disappears. Understanding it tells you which link to use for backups, version switches, deduplication and /usr/bin shortcuts.

Terminal
ln target hardname      # hard link
ln -s target softname   # symbolic link

Try it

Bash Delete the original and see what survives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Writes, renames and link counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Side by side

Property Hard link Symbolic link
What it stores A name pointing at an inode A text path
Own inode No, shares the target's Yes
Survives deleting the original name Yes, data lives until the last name goes No, becomes dangling
Survives moving the original Yes (same filesystem) No, unless the path is still valid
Cross filesystems No Yes
Link to a directory No (only the kernel's . and ..) Yes
Link to a path that does not exist No Yes
Relative or absolute Not applicable Either; relative resolves from the link's directory
Permissions Same as the file (it is the file) Link's own mode is ignored; target's applies
Size shown by ls -l File size Length of the stored path

Why the rules look like this

A hard link is only a name. Directory entries map names to inode numbers, and the inode keeps a link count. rm removes one name and decrements the count; the data is freed when the count reaches zero and no process still has the file open. Because inode numbers are meaningful only inside one filesystem, a hard link cannot cross one. Directories are excluded so the tree cannot form loops. See Inodes.

A symlink is a file whose content is a path. Resolution happens each time you use it, so it follows the thing at that path today, not the thing that was there when you created it. That makes it flexible and fragile.

Which one to use

  • Symlink: version switching (current -> releases/2024-05), shortcuts to directories, config files kept in one repository, linking across disks.
  • Hard link: snapshot-style backups that share unchanged files (rsync --link-dest, cp -al), keeping a file reachable under two names atomically, protecting a file from accidental rm.
  • Neither: if you need an independent copy, use cp.

Usage notes

  • Editors can break hard links. Many save by writing a new file and renaming it over the old one, so the two names stop sharing content. cp-style in-place writes keep the link.
  • Relative symlinks are created relative to the link's location, not your current directory. ln -s ../lib/x.so /usr/bin/x is correct; ln -s lib/x.so /usr/bin/x usually is not.
  • ls -l and readlink show where a symlink points; ls -i or stat -c %i shows the inode so you can spot hard links; find -samefile finds every name.
  • rm on a symlink removes the link, never the target. rm link/ with a trailing slash on a symlink to a directory behaves differently, so avoid the slash.
  • Chains and loops: symlinks may point at symlinks; the kernel stops after about 40 and reports "Too many levels of symbolic links".
  • Tools differ in following links: cp follows symlinks on the command line only with -L, find needs -L or -follow, tar stores the link unless told otherwise.
  • Security: symlink races in shared directories like /tmp are a classic attack. Use mktemp and keep protected links enabled (fs.protected_symlinks).