← All lessons
Files and permissions · Lesson 6 of 7

Links

A symbolic link (symlink) is a small file that points to another path, like a shortcut. Open the link and you get the file it points to. ln -s makes one: target first, then the link's name.

$ ln -s /var/www/duck-site/logs logs
$ ls -l logs
lrwxrwxrwx 1 sam sam 23 Oct 21 10:02 logs -> /var/www/duck-site/logs

The l at the start and the arrow show it's a link and where it goes. Its own permissions are always shown as rwxrwxrwx and don't matter; the target's permissions are the ones that count.

If the target is moved or deleted, the symlink breaks: it still exists but points at nothing, and opening it says No such file or directory. Deleting a link with rm logs removes only the link, never the target.

A hard link (ln without -s) is a second name for the very same file data. The data stays until the last name is deleted, so deleting the original doesn't break a hard link. Hard links only work within one disk and can't point to folders, so symlinks are what you'll use almost every time.

Linux uses symlinks a lot: python3 might be a link to python3.12, and on many systems /bin links to /usr/bin. readlink -f follows a chain of links to the real file.

Practice

Make a symbolic link called docs that points to /srv/shared/docs.

More info

Quick reference

ln -s target nameMake a symbolic link.
ln target nameMake a hard link.
name -> targetHow ls -l shows a symlink.
readlink -f nameThe real file at the end of the links.
rm nameRemoves the link, not the target.

The other Linux courses