Skip to content

Hard links and symbolic links

Module
Working with Files and Directories
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21, GNU Coreutils 9.4
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.

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.

Terminal window
cd ~/lab/b06
mkdir -p links
cd links
cp ../../Documents/notes.txt notes.txt
pwd
Captured output
/home/learner/lab/b06/links

The copy keeps the source notes intact. Every relative path below starts in links, unless a command changes directory.

Inspect the fresh copy:

Terminal window
ls -li notes.txt
stat notes.txt
Captured output
38025 -rw-r--r-- 1 learner learner 40 Nov 11 09:22 notes.txt
Captured output
File: notes.txt
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38025 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 09:22:18.251754817 +0000
Modify: 2026-11-11 09:22:18.253058860 +0000
Change: 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.

Guess first: will this create another inode, like a copy, or another name for the existing inode?

Terminal window
ln notes.txt saved.txt
ls -li notes.txt saved.txt
stat saved.txt
Run it, then compare with your guess
Captured output
38025 -rw-r--r-- 2 learner learner 40 Nov 11 09:22 notes.txt
38025 -rw-r--r-- 2 learner learner 40 Nov 11 09:22 saved.txt
Captured output
File: saved.txt
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38025 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 09:22:18.251754817 +0000
Modify: 2026-11-11 09:22:18.253058860 +0000
Change: 2026-11-11 09:22:20.263843163 +0000
Birth: 2026-11-11 09:22:18.251754817 +0000

Both 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:

Terminal window
printf 'Review links\n' >> saved.txt
cat notes.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Review 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.

Add -s to request a symbolic link:

Terminal window
ln -s notes.txt shortcut
ls -li notes.txt saved.txt shortcut
readlink shortcut
cat shortcut
Captured output
38025 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 notes.txt
38025 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 saved.txt
38029 lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txt
Captured output
notes.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Review links

Notice 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.

Inspect the three names before removal:

Terminal window
ls -l notes.txt saved.txt shortcut
Captured output
-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.txt
lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txt

Predict which will still read the notes: saved.txt, shortcut, both, or neither.

Terminal window
rm notes.txt
cat saved.txt
stat saved.txt
ls -l shortcut
cat shortcut
Run it, then compare with your guess
Captured output
Buy printer paper
Renew lab VM snapshot
Review links
Captured output
File: saved.txt
Size: 53 Blocks: 8 IO Block: 4096 regular file
Device: 0,91 Inode: 38025 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ learner) Gid: ( 1001/ learner)
Access: 2026-11-11 09:22:25.800083201 +0000
Modify: 2026-11-11 09:22:21.771908551 +0000
Change: 2026-11-11 09:22:25.296061350 +0000
Birth: 2026-11-11 09:22:18.251754817 +0000
Captured output
lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 shortcut -> notes.txt
Captured output
cat: shortcut: No such file or directory

saved.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.

One name removed, shared data retained Before rm, notes.txt and saved.txt point to one inode with Links: 2 and the same data. The separate shortcut symlink stores notes.txt. After rm, a dashed notes.txt box marked removed shows the missing name; the symlink's dashed connector ends at a break mark on its edge, labelled path no longer resolves. saved.txt still points to the inode with Links: 1 and the data remains. before rm shortcut (own inode) stores: notes.txt notes.txt saved.txt Same file inode Links: 2 File data after rm shortcut (own inode) stores: notes.txt notes.txt removed saved.txt Same file inode Links: 1 Data still here path no longer resolves
One name removed, shared data retained Before rm, notes.txt and saved.txt point to one inode with Links: 2 and the same data. The separate shortcut symlink stores notes.txt. After rm, a dashed notes.txt box marked removed shows the missing name; the symlink's dashed connector ends at a break mark on its edge, labelled path no longer resolves. saved.txt still points to the inode with Links: 1 and the data remains. before rm shortcut (own inode) stores: notes.txt notes.txt saved.txt Same file inode Links: 2 File data Names → inode → data after rm shortcut (own inode) stores: notes.txt notes.txt removed saved.txt Same file inode Links: 1 Data still here path no longer resolves Names → inode → data
Simplified model. Before rm, two names share one inode; after rm, saved.txt retains the data while shortcut's stored path is broken.

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.

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.

Make a working symlink to the surviving name and inspect it:

Terminal window
ln -s saved.txt live
ls -l live saved.txt
Captured output
lrwxrwxrwx 1 learner learner 9 Nov 11 09:22 live -> saved.txt
-rw-r--r-- 1 learner learner 53 Nov 11 09:22 saved.txt
Terminal window
rm live
cat saved.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Review links

rm 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.

Resolving a relative target from where you ran ln.

From links, try placing a symlink inside another directory:

Terminal window
mkdir shelf
ln -s saved.txt shelf/wrong
readlink shelf/wrong
cat shelf/wrong
Captured output
saved.txt
Captured output
cat: shelf/wrong: No such file or directory

ln -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:

Terminal window
ln -s ../saved.txt shelf/right
cat shelf/right
Captured output
Buy printer paper
Renew lab VM snapshot
Review links

The 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.

Terminal window
ln shelf shelf-hard
Captured output
ln: shelf: hard link not allowed for directory

ln 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.

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
Terminal window
mkdir -p delivery/store delivery/view
cp saved.txt delivery/store/brief.txt
ln -s ../store/brief.txt delivery/view/current
ln delivery/store/brief.txt delivery/retained.txt
ls -li delivery/store/brief.txt delivery/retained.txt
readlink delivery/view/current
cat delivery/view/current
ls -l delivery/store/brief.txt
Captured output
38038 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/retained.txt
38038 -rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/store/brief.txt
Captured output
../store/brief.txt
Captured output
Buy printer paper
Renew lab VM snapshot
Review links
Captured output
-rw-r--r-- 2 learner learner 53 Nov 11 09:22 delivery/store/brief.txt

The shared inode and count 2 identify the hard links. The stored path goes up from view to delivery, then into store.

Terminal window
rm delivery/store/brief.txt
cat delivery/retained.txt
cat delivery/view/current
Captured output
Buy printer paper
Renew lab VM snapshot
Review links
Captured output
cat: delivery/view/current: No such file or directory

The retained name reaches the same file; the symlink still asks for the removed pathname. This also explains the different outcomes in the opening.

After finishing, or after stopping partway through, return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b06
ls -AR links

-A includes hidden entries; -R includes subdirectories. After the solution:

Captured output
links:
delivery saved.txt shelf shortcut
links/delivery:
retained.txt store view
links/delivery/store:
links/delivery/view:
current
links/shelf:
right wrong

If you stopped earlier, the listing reflects that point instead.

Terminal window
rm -r ~/lab/b06/links

It 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.
  • ln adds a hard-link name for the same file. Contents are shared, and Links counts those names.
  • ln -s creates a separate symlink storing a target path. readlink prints 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.

  • On your system: man ln and man 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.