LESSON · 1 OF 5

What are links

What are links

A link is an alternate name for a file. Linux has two flavors, and they behave very differently.

Hard links

Every file on a filesystem is stored at an inode, a numeric ID. The "name" of a file is a directory entry that points at that inode. A hard link is just another directory entry pointing at the same inode.

bash
echo hi > original.txt          # a file to link to
ln original.txt copy.txt        # creates a hard link
ls -li                          # -i shows the inode number

Both original.txt and copy.txt show the same inode. They're two names for one file. Delete one, the file is still there under the other name. Edit one, both "see" the change because there's only one file underneath.

Limitations:

  • Hard links can't cross filesystems (an inode is only meaningful within one filesystem).
  • Hard links can't point at directories (with rare exceptions).

Hard links are useful for deduplication and backup tools. In daily DevOps work, you'll rarely create one on purpose.

Symbolic links (symlinks)

A symlink is a small file that contains a path pointing at another file or folder. Following a symlink is like following a shortcut.

bash
ln -s /var/www/releases/v2 /var/www/current

Now /var/www/current is a symlink pointing at /var/www/releases/v2. Reading /var/www/current/index.html transparently reads /var/www/releases/v2/index.html.

Telling them apart in a listing

bash
ls -l /var/www/current

A symlink shows an l as the first character and an arrow to its target:

lrwxrwxrwx 1 root root 23 Jul 14 10:30 current -> /var/www/releases/v2

Regular files show -, directories show d, and hard links look exactly like regular files (because they ARE regular files, just under a different name).

Spin up a fresh environment and practice live.
linux-devops-basic · fresh machine · ready in under a minute