Links explain deployments with a 'current' symlink, why deleting a file may not free space, and many tool behaviours.
On a Linux filesystem, a file's data and metadata (owner, permissions, size, timestamps) live in an inode, identified by a number. A directory is just a table mapping names to inode numbers. ls -i shows the numbers and stat the full metadata.
A hard link (ln target name) adds another name for the same inode. Both names are equal; there is no "original". The inode keeps a link count, and the data is freed only when the count reaches zero and no process has the file open. Hard links cannot cross filesystems and cannot point to directories.
A symbolic link (ln -s target name) is a small special file that contains a path. Opening it follows the path. Symlinks can point anywhere, including directories and other filesystems, but if the target is moved or deleted the link dangles. A relative target is resolved relative to the link's own directory, not your current one.
Useful commands: readlink link prints the stored path, readlink -f the fully resolved one; ls -l shows name -> target; find -L follows links, find -type l finds them.
A common deployment pattern: install each release in its own directory and atomically switch a current symlink with ln -sfn /opt/app-2.1 /opt/app/current, making rollbacks instant.
Example
cd "$(mktemp -d)"
echo "hello" > original.txt
ln original.txt hard.txt # hard link: another name, same inode
ln -s original.txt soft.txt # symlink: a file containing a path
ls -li | sort -k10
# 1234 -rw-r--r-- 2 ada ada 6 ... hard.txt
# 1234 -rw-r--r-- 2 ada ada 6 ... original.txt
# 1240 lrwxrwxrwx 1 ada ada 12 ... soft.txt -> original.txt
rm original.txt
cat hard.txt # hello (data lives while a link remains)
cat soft.txt # cat: soft.txt: No such file or directory
readlink soft.txt # original.txt (the dangling path)
stat -c '%h links: %n' hard.txt # 1 links: hard.txt
mkdir app-2.0 app-2.1
ln -sfn app-2.1 current && readlink current # app-2.1 (switch a release)Remember
- An inode holds data and metadata; names are links to it
- Hard links share an inode; symlinks store a path and can dangle
- ln -sfn atomically repoints a 'current' symlink
Watch out for
- Creating a relative symlink from another directory and expecting the path to be relative to where you ran ln.