Linux Command Line · Lesson 26 of 43
Hard links and symbolic links
Create hard links and symbolic links with ln, see how they differ, read ls -l output for links and avoid broken or circular links.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will create shortcuts to files and folders, understand why some links survive moving a file and others break, and read the arrow notation in ls -l. Links are everywhere on Linux: /bin is one, many configuration files are one, and installers use them to switch between versions of a program.
Names and data
On Linux, a file's name is not the file itself. The name is an entry in a directory that points to the file's data, which is described by an inode, a record holding the size, owner, permissions and the location of the data. That separation makes links possible: you can have several names, or a name that just contains a path.
Symbolic links
A symbolic link (symlink or soft link) is a tiny special file containing the path of another file. Create one with ln -s TARGET LINKNAME, in that order:
cd ~/project
ln -s docs/notes.txt shortcut.txt
stat -c '%A %N' shortcut.txt
cat shortcut.txt | head -n 1lrwxrwxrwx 'shortcut.txt' -> 'docs/notes.txt'
Linux is a family of operating systems.ls -l would show the same thing as shortcut.txt -> docs/notes.txt together with the date; we use stat here because it leaves the date out. The l at the start of the mode marks a link, and the arrow shows its target. Reading the link follows it automatically. The permission bits lrwxrwxrwx are not meaningful for a link; access depends on the target. readlink prints just the target, and readlink -f prints the full absolute path of the final destination:
readlink shortcut.txt
readlink -f shortcut.txtdocs/notes.txt
/home/learner/project/docs/notes.txtA relative target, like the one above, is resolved relative to the folder containing the link, not to where you run the command. Use an absolute target when the link will be used from many places.
Links to folders
A symlink can point to a folder, and cd follows it. People use this to shorten long paths or to give a stable name to a changing folder:
ln -s ~/project/docs mydocs
ls mydocs
cd mydocs
pwd
pwd -Pnotes.txt
tasks.txt
/home/learner/project/mydocs
/home/learner/project/docspwd shows the path you used, and pwd -P shows the real physical path.
Broken links
If the target is moved or deleted, the symbolic link remains but points to nothing. It is called dangling or broken:
cd ~/project
mv docs/notes.txt docs/notes-old.txt
cat shortcut.txt
stat -c '%A %N' shortcut.txtcat: shortcut.txt: No such file or directory
lrwxrwxrwx 'shortcut.txt' -> 'docs/notes.txt'The link kept its old path, and the error says "No such file or directory" even though the link itself exists. find . -xtype l lists broken symlinks, which is useful for cleaning up.
Hard links
A hard link is a second directory entry for the same inode. Both names are completely equal; neither is "the original". Create one with ln TARGET LINKNAME, without -s:
cd ~/project
ln docs/tasks.txt tasks-link.txt
stat -c '%h links, inode shared: %n' docs/tasks.txt tasks-link.txt | cut -d, -f1
ls -i docs/tasks.txt tasks-link.txt | awk '{print ($1 == prev ? "same inode" : "different inode"); prev = $1}' | tail -n 12 links
2 links
same inodeBoth names now report 2 links and share one inode. Edit the file through either name and you see the change through both, because there is only one piece of data. The data is removed only when the last name is deleted:
rm docs/tasks.txt
cat tasks-link.txt | head -n 2Buy milk
Call the bankThe data survived because a second name still pointed at it.
Comparing the two
| Symbolic link | Hard link | |
|---|---|---|
| Contains | A path (text) | Nothing: another name for the data |
| Can point to a folder | Yes | No |
| Can cross filesystems | Yes | No |
| After the target is moved or removed | Breaks | Still works |
Shows in ls -l as |
link -> target |
An ordinary file with link count above 1 |
Use symbolic links for almost everything: shortcuts, version switches, shared configuration. Hard links are useful for backups that share unchanged data, as some backup tools do.
Deleting links
rm shortcut.txt removes the link and never the target. For a symlink to a folder, remove it without a trailing slash: rm mydocs, not rm mydocs/. A trailing slash makes the name refer to the folder the link points to, not to the link itself, so rm mydocs/ does not remove the link.
Common mistakes
Swapping the arguments. It is ln -s TARGET LINKNAME. Reversing them creates a link named after your real file.
Using a relative target from another folder. The target is relative to the link's location. Use an absolute path if unsure.
Expecting a hard link to work across disks. Use a symbolic link.
Deleting with a trailing slash. A trailing slash makes the name mean the target folder, not the link, so rm link/ does not remove the link. Use rm link.
Exercise
Create a symbolic link to ~/project/logs/app.log named latest.log in your home folder, and read it through the link. Move the log and watch the link break, then fix it with ln -sf. Create a hard link to a file, change one of the names' contents with echo "x" >> name, and check that both show the change.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.