Hard links and symbolic links
On this page
You want another name for your working notes without making a separate copy. Later, you remove the first name. One kind of link still opens the notes; another reports a missing file.
By the end, you can create both kinds, read the metadata that distinguishes them, and explain which names survive a removal.
Before you start
Section titled “Before you start”Finish Filenames and metadata (B06.3). Keep your existing ~/lab from B04.1’s setup. Leave Documents and the earlier B06 project, archive, handoff, removal, and inspection directories intact.
Use only the disposable ~/lab/b06/links directory. Run this setup once; use Reset this lesson before repeating it.
cd ~/lab/b06mkdir -p linkscd linkscp ../../Documents/notes.txt notes.txtpwd/home/learner/lab/b06/linksThe copy keeps the source notes intact. Every relative path below starts in links, unless a command changes directory.
Find the record behind a name
Section titled “Find the record behind a name”Inspect the fresh copy:
ls -li notes.txtstat notes.txt38025 -rw-r--r-- 1 learner learner 40 Nov 11 09:22 notes.txt File: notes.txt Size: 40 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38025 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 09:22:18.251754817 +0000Modify: 2026-11-11 09:22:18.253058860 +0000Change: 2026-11-11 09:22:18.253058860 +0000 Birth: 2026-11-11 09:22:18.251754817 +0000-li combines the long listing, -l, with -i, which adds the inode number at the left. It matches Inode in stat.
A directory entry associates a name with an inode number. The inode is the file’s metadata record from B06.3; it also identifies where the contents are stored. The name lives in the directory, rather than inside that record.
Links: 1 counts one hard-link name for this regular file: notes.txt. In the long listing, the link count follows the type and permission string.
Device identifies the device containing the filesystem. Inode numbers identify files within one filesystem; another filesystem can use the same number. Compare Device and Inode together when identifying a file across filesystems.
Your inode numbers, device numbers, times, and allocation details will differ. This lab capture uses numeric user/group IDs 1001; the course VM’s IDs are usually 1000. These are observations, not values to reproduce.
Give the same file a second name
Section titled “Give the same file a second name”Guess first: will this create another inode, like a copy, or another name for the existing inode?
ln notes.txt saved.txtls -li notes.txt saved.txtstat saved.txtRun it, then compare with your guess
38025 -rw-r--r-- 2 learner learner 40 Nov 11 09:22 notes.txt38025 -rw-r--r-- 2 learner learner 40 Nov 11 09:22 saved.txt File: saved.txt Size: 40 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38025 Links: 2Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 09:22:18.251754817 +0000Modify: 2026-11-11 09:22:18.253058860 +0000Change: 2026-11-11 09:22:20.263843163 +0000 Birth: 2026-11-11 09:22:18.251754817 +0000Both names have the same inode number. Links has risen from 1 to 2. ln made a hard link: a second name for the same file. Neither name has a special status as the original once both exist.
Change the contents through saved.txt, then read through notes.txt:
printf 'Review links\n' >> saved.txtcat notes.txtBuy printer paperRenew lab VM snapshotReview links>> appends the output to the named file instead of replacing its contents; B09 covers redirection in detail. Here printf adds one line, with \n ending that line.
Both names reach the same contents. A hard link is therefore not an independent backup: changing the file through either name changes what the other reads.
Store a path in a separate link
Section titled “Store a path in a separate link”Add -s to request a symbolic link:
ln -s notes.txt shortcutls -li notes.txt saved.txt shortcutreadlink shortcutcat shortcut38025 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 notes.txt38025 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 saved.txt38029 lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txtnotes.txtBuy printer paperRenew lab VM snapshotReview linksNotice the lowercase l at the start of shortcut’s type string, its different inode, and shortcut -> notes.txt. A symbolic link, also called a symlink, is a separate small file containing a target path. readlink prints that stored path; cat follows it and reads the target’s contents.
Creating the symlink did not raise the notes’ hard-link count beyond 2. It points to the name notes.txt, rather than becoming another name for the notes’ inode.
Remove one name, then follow the others
Section titled “Remove one name, then follow the others”Inspect the three names before removal:
ls -l notes.txt saved.txt shortcut-rw-r--r-- 2 learner learner 53 Nov 11 09:22 notes.txt-rw-r--r-- 2 learner learner 53 Nov 11 09:22 saved.txtlrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txtPredict which will still read the notes: saved.txt, shortcut, both, or neither.
rm notes.txtcat saved.txtstat saved.txtls -l shortcutcat shortcutRun it, then compare with your guess
Buy printer paperRenew lab VM snapshotReview links File: saved.txt Size: 53 Blocks: 8 IO Block: 4096 regular fileDevice: 0,91 Inode: 38025 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)Access: 2026-11-11 09:22:25.800083201 +0000Modify: 2026-11-11 09:22:21.771908551 +0000Change: 2026-11-11 09:22:25.296061350 +0000 Birth: 2026-11-11 09:22:18.251754817 +0000lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txtcat: shortcut: No such file or directorysaved.txt still works. Its inode is unchanged, and Links has dropped to 1. The symlink still exists and still stores notes.txt, but that name is gone. This is a dangling link: its target path cannot be resolved.
The error names shortcut, the path you asked cat to open, rather than printing the missing target notes.txt. Use readlink or the ls -l arrow to inspect the target.
In words: before rm, notes.txt and saved.txt lead to one inode and its data. The separate shortcut symlink stores notes.txt. After rm, only saved.txt leads to that inode, with Links 1; the data remains, while the symlink’s path fails. This model omits filesystem allocation details and open programs.
Removing one name does not wipe the data. Storage becomes available for reuse only after the last hard-link name is gone and no program still has the file open. Removing the last name is not a claim that the stored bytes have been erased.
learner@rootvolt:~/lab$ cd b06/linkslearner@rootvolt:~/lab/b06/links$ rm notes.txtlearner@rootvolt:~/lab/b06/links$ cat saved.txtBuy printer paper
Renew lab VM snapshot
Review linkslearner@rootvolt:~/lab/b06/links$ cat shortcutcat: shortcut: No such file or directorylearner@rootvolt:~/lab/b06/links$ Files ~/lab/b06/links
- ~
- lab/b06/links/cwd
- saved.txt-rw-r--r--
- shortcutargumentlrwxrwxrwx
Steps (10)
- Type
cd b06/links - Press Enter
- Type
rm notes.txt - Press Enter
- Type
cat saved.txt - Press Enter
- Output:
Buy printer paper Renew lab VM snapshot Review links - Type
cat shortcut - Press Enter
- Output:
cat: shortcut: No such file or directory
The Files panel marks notes.txt removed for that step. It shows names, not shared inodes or target paths; use the diagram for those relationships. The static transcript preserves both reads and the failure without playback.
Remove the link while keeping its target
Section titled “Remove the link while keeping its target”Make a working symlink to the surviving name and inspect it:
ln -s saved.txt livels -l live saved.txtlrwxrwxrwx 1 learner learner 9 Nov 11 09:22 live -> saved.txt-rw-r--r-- 1 learner learner 53 Nov 11 09:22 saved.txtrm livecat saved.txtBuy printer paperRenew lab VM snapshotReview linksrm live succeeds silently and removes the link itself. The target remains readable. Naming a symlink directly for removal does not remove its target, whether the link works or is dangling.
Mistakes to recognize
Section titled “Mistakes to recognize”Resolving a relative target from where you ran ln.
From links, try placing a symlink inside another directory:
mkdir shelfln -s saved.txt shelf/wrongreadlink shelf/wrongcat shelf/wrongsaved.txtcat: shelf/wrong: No such file or directoryln -s accepted the path without checking that it exists. A relative target is resolved from the link’s own directory, shelf, not the directory where you ran ln. This link asks for shelf/saved.txt, which does not exist.
Create a different link whose target goes up one directory:
ln -s ../saved.txt shelf/rightcat shelf/rightBuy printer paperRenew lab VM snapshotReview linksThe stored ../saved.txt starts in shelf and reaches the surviving file in links. You did not need to move there with cd.
Trying to hard-link a directory.
ln shelf shelf-hardln: shelf: hard link not allowed for directoryln names shelf and explains the refusal: Linux does not allow hard links to directories. Hard links also cannot cross filesystem boundaries. Symbolic links can refer to directories or paths on another filesystem, because they store paths instead of sharing the target’s inode.
Try it yourself
Section titled “Try it yourself”Goal: Prepare a delivery with a readable relative shortcut and a retained hard-link name in another directory.
Inside links, create delivery/store and delivery/view. Copy saved.txt to delivery/store/brief.txt. Make delivery/view/current a relative symlink to that copy, and make delivery/retained.txt a hard link to it. Verify both relationships before removing only delivery/store/brief.txt.
Expected: The retained name still reads the notes; the shortcut fails. Explain why your relative target begins with ...
Hint
Resolve the symlink target from delivery/view. Its sibling directory is ../store. A hard link takes the existing file path first and the additional name second.
Solution
mkdir -p delivery/store delivery/viewcp saved.txt delivery/store/brief.txtln -s ../store/brief.txt delivery/view/currentln delivery/store/brief.txt delivery/retained.txtls -li delivery/store/brief.txt delivery/retained.txtreadlink delivery/view/currentcat delivery/view/currentls -l delivery/store/brief.txt38038 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/retained.txt38038 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/store/brief.txt../store/brief.txtBuy printer paperRenew lab VM snapshotReview links-rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/store/brief.txtThe shared inode and count 2 identify the hard links. The stored path goes up from view to delivery, then into store.
rm delivery/store/brief.txtcat delivery/retained.txtcat delivery/view/currentBuy printer paperRenew lab VM snapshotReview linkscat: delivery/view/current: No such file or directoryThe retained name reaches the same file; the symlink still asks for the removed pathname. This also explains the different outcomes in the opening.
Reset this lesson
Section titled “Reset this lesson”After finishing, or after stopping partway through, return to the parent and inspect only this lesson’s directory:
cd ~/lab/b06ls -AR links-A includes hidden entries; -R includes subdirectories. After the solution:
links:delivery saved.txt shelf shortcut
links/delivery:retained.txt store view
links/delivery/store:
links/delivery/view:current
links/shelf:right wrongIf you stopped earlier, the listing reflects that point instead.
rm -r ~/lab/b06/linksIt succeeds silently. Run the setup under Before you start again. If links is already absent, skip removal and run setup. The setup restores the initial note copy; its inode and times need not match the previous run.
- A directory entry maps a name to an inode; Device and Inode identify the file within its filesystem context.
lnadds a hard-link name for the same file. Contents are shared, and Links counts those names.ln -screates a separate symlink storing a target path.readlinkprints it; relative targets start in the link’s directory.- Removing one hard-link name leaves the others usable. Storage is released for reuse after the last name and open reference are gone.
- A symlink can dangle after its target name disappears. Removing the symlink itself leaves its target intact.
Next, Practice: Working with Files and Directories puts all four lessons to work on a classmate’s handoff.
Further reading
Section titled “Further reading”- On your system:
man lnandman readlink. Ubuntu 24.04 manuals: ln(1) and readlink(1). - Ubuntu 24.04 symlink(7) explains link targets and dangling links; inode(7) explains Device, Inode, and link counts.
- Ubuntu 24.04 unlink(2), DESCRIPTION, explains removal while a file remains open. This documents the operation; you do not need to call it yourself.