Linux Foundations / Inspect Storage and Restore Owned Files

Compare symbolic and hard links

Lesson 25 of 37. Open to read. No account needed.

Two names can reach the same file, while a third name can store a path to one of those names. A copy starts with the same contents but is a separate file. These differences determine what happens when you edit through a name or remove that name.

You will compare one copy, one hard link and one symbolic link to a tiny owned route note. Then you will remove only the original name, inspect what survives, and repair a deliberately incorrect relative symbolic link. The exercise stays inside one fresh directory tree in the normal Ubuntu guest.

1. Separate a name, a file identity and a stored path

An inode is a filesystem object that holds file metadata and identifies the file within that filesystem. For this exercise, compare the device number and inode number together; an inode number alone is not an identity across different filesystems. Record the pair at each observed stage rather than treating a printed number as a permanent identifier for every future file.

A directory entry associates a name with a file. A hard link adds another name for the same file identity. Neither hard-link name is the special master name: both reach the same content and metadata. The file's hard-link count records how many hard-link names refer to it.

A symbolic link, or symlink, is a separate object containing a target path. When an operation follows it, path lookup uses that stored path. If the stored path is relative, resolution starts in the directory containing the symlink, not wherever your terminal happens to be. A symlink whose target cannot be resolved is dangling; its own directory entry can still exist.

RelationshipFile identityWhat a content change means
CopyA separate file from its source.Later changes to one file do not automatically edit the other.
Hard linkAnother name for the same device/inode pair.Writing through either name changes the shared file.
Symbolic linkA separate link object holding a path.Reading or writing through it follows the stored path when resolution succeeds.

A separate copy identity does not prove that every physical storage block is distinct. Storage can share blocks internally while preserving independent file contents. This lesson tests identity and content behavior, not allocation. Hard links to ordinary files must stay within the same filesystem; do not change mounts or privileges to bypass a cross-device refusal.

The route.txt and route-hard.txt names reach one device-and-inode file identity; route-copy.txt has a separate identity. The separate route-symbolic.txt object stores route.txt relative to notes. Removing only route.txt leaves route-hard.txt reaching the shared file, while the symbolic link's unchanged target path dangles.

Read green arrows as names reaching regular-file identities, and blue arrows as lookup through the symbolic link's stored path from notes/. The upper scene is after the shared-file append but before removing route.txt; the lower scene is after that name's removal. This expected model predicts the guided observations; it contains no measured inode numbers and does not give the independent repair answer.

2. Establish a fresh owned source

Complete Inspect capacity, mounts and directory usage first. Use the checked ordinary nonzero-UID Ubuntu guest account in Bash. Run one line at a time, inspect each result, and stop after unexpected errors. The task creates only named files, links and directories under ~/linux-practice/compare-file-links/; no system target, directory hard link, privilege elevation or forced replacement is needed.

Reuse the taught identity and command-origin checks:

id -u
id -un
ps -p $ -o comm=
type cd pwd printf set mkdir cat cp ln readlink stat ls rm rmdir

Require the recorded nonzero account and active Bash. cd, pwd, printf and set must be Bash builtins; the other commands must resolve to expected guest executables. For a listing alias, reuse the complete taught inspection:

type -a ls
type -P ls

Accept only the understood color-only alias, if present, and expected executable paths. Stop for a function, additional alias behavior or an unexpected origin. Inspect shell options without changing them:

set -o

Require errexit off because this lesson includes deliberate missing-target observations. Preserve and reconcile a different baseline rather than altering settings. Inspect the known parents and prospective task name without trailing slashes, so ls -ld checks each entry itself:

ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/compare-file-links

Require ordinary parent directories owned by the recorded account, with no links or unknown identity. The task name must be absent for a fresh start; an existing attempt needs the resume section. After acceptance:

mkdir ~/linux-practice/compare-file-links
ls -ld ~/linux-practice/compare-file-links

Require the recognized new ordinary owned directory, then enter it separately:

cd ~/linux-practice/compare-file-links
pwd
ls -a

Require the recorded shared-root path followed by /compare-file-links and only . and .. before creating children:

mkdir notes shortcuts
ls -ld -- notes shortcuts
ls -a -- notes
ls -a -- shortcuts

Require two ordinary owned empty directories. In particular, shortcuts must contain no preexisting subtree that could make the later wrong target resolve somewhere unintended.

Use the taught newline-terminating printf and fresh-output redirection only after confirming notes/route.txt absent:

printf '%s\n' 'Route: north gate.' 'Bring the paper map.' > notes/route.txt

Inspect the entry and every supplied line:

ls -ld -- notes/route.txt
cat notes/route.txt

Require a regular file owned by your account with one hard link, containing exactly two newline-terminated lines:

Route: north gate.
Bring the paper map.

The one-link starting count matters: you are about to add exactly one known hard-link name. Reject an unexpected starting identity or link count.

3. Observe identity before adding names

The taught stat -c selects a format and prints one line per operand. Add these fields to your metadata vocabulary before using the report:

Format itemMeaning
%dDecimal device number identifying the file's filesystem context.
%iInode number within that context. Compare it together with %d.
%hHard-link count for the reported object.
%FFile type, such as regular file or symbolic link; displayed wording can vary.
%nThe name supplied to the command, so each report stays attached to its operand.

The literal labels in the quoted format make the observations readable. The quotes group the format into one argument; they are not printed. Without -L, GNU stat reports a symbolic link's own metadata rather than following its final target. We will use that distinction shortly.

First compare directory filesystem context and record the source identity:

stat -c 'device=%d type=%F name=%n' -- notes shortcuts
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route.txt

Require the intended same device context for these two new directories and record the source's device/inode pair and count 1. The decimal device number is metadata, not a disk-capacity figure or an lsblk display name.

4. Make a copy, then a hard link

Inspect ls -a -- notes before each new destination and require its exact name absent. cp -iT combines the taught interactive overwrite check (-i) with -T, which treats the destination as the exact file entry instead of as a directory receiving a file. We still require a fresh absent destination; an unexpected prompt or collision is a reason to stop, not approve replacement.

cp -iT -- notes/route.txt notes/route-copy.txt

Inspect the new copy and its two-line content:

ls -ld -- notes/route-copy.txt
cat notes/route-copy.txt
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route.txt notes/route-copy.txt

Require an ordinary owned single-link copy with the two exact source lines. Both files should be in this same filesystem, but the copy must have a different device/inode identity from the source. Record the actual values; do not substitute illustrative inode numbers.

ln creates a hard link by default. In ln -T -- TARGET LINK_NAME, the first operand identifies the existing file and the second gives the new name. -T again treats the second operand as the exact destination entry. We use no force option. After checking notes/route-hard.txt absent:

ln -T -- notes/route.txt notes/route-hard.txt

Inspect the new name and both identities:

ls -ld -- notes/route.txt notes/route-hard.txt
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route.txt notes/route-hard.txt notes/route-copy.txt

Require route.txt and route-hard.txt to be regular owned entries with the same recorded device/inode pair and link count 2. The copy retains its separate identity and count 1. Equal text alone would not prove this relationship; the paired identity and count explain it.

5. Add a symbolic link and inspect its stored path

ln -s selects a symbolic link. The target operand is stored path text, and it need not exist for link creation to succeed. That makes target inspection essential. -sT combines symbolic creation with the exact destination-entry rule. Check notes/route-symbolic.txt absent before:

ln -sT -- route.txt notes/route-symbolic.txt

The stored relative path is route.txt. Because the link lives in notes/, following it looks for notes/route.txt, even though this command ran from the outer exercise directory.

readlink without canonicalizing options prints the stored target of a symbolic link. It does not prove that the target currently exists. Inspect the link itself and its exact text:

ls -ld -- notes/route-symbolic.txt
readlink -- notes/route-symbolic.txt
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route-symbolic.txt

Require the recognized symbolic-link entry, stored text route.txt, and its own device/inode identity, different from the regular shared file. Creating this symlink does not add a hard link to the target, whose count remains 2.

stat -L explicitly follows the final symbolic link and reports its target. Use it only after recognizing this link and its in-scope stored path:

stat -L -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route-symbolic.txt
cat notes/route-symbolic.txt

The followed metadata must match the shared regular file's device/inode pair and count, and cat must print the original two lines. The reported operand name can still be the symbolic-link pathname; inspect the identity and type to understand which object the selected stat mode describes.

6. Predict one edit and one removed name

Predict the content reached through all four names before editing. After rechecking the recorded shared identity and its two-line content, append one new line through the hard-link name using the taught >>:

printf '%s\n' 'Check the gate sign.' >> notes/route-hard.txt

Inspect each complete result:

cat notes/route.txt
cat notes/route-hard.txt
cat notes/route-symbolic.txt
cat notes/route-copy.txt

The first three reads must show the same three lines, with Check the gate sign. as the third. The copy must retain only its original two lines. The append changed the shared file, while the symlink followed the path reaching that file. It did not independently store a copy of the text.

Now predict what happens if only the notes/route.txt name is removed. Reinspect its entry and shared pair before this deliberate transition:

ls -ld -- notes/route.txt notes/route-hard.txt notes/route-symbolic.txt
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route.txt notes/route-hard.txt
readlink -- notes/route-symbolic.txt

Require the same two-name shared file and the known symbolic target. The shown y/n responses assume English confirmations. Establish the actual program's documented responses before removal; stop if the convention is unclear. Use only the recognized original name:

rm -i -- notes/route.txt

If you decline, the name remains: stop this transition instead of claiming the following post-removal result. After an accepted removal, verify the inventory, surviving identity and preserved content:

ls -a -- notes
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route-hard.txt
cat notes/route-hard.txt
cat notes/route-copy.txt
readlink -- notes/route-symbolic.txt

Require route.txt absent, the hard name still reaching the same recorded file with count 1 and three lines, and the copy still containing two lines. Readlink still prints route.txt: it reads the symlink, not the missing target. Observe the deliberate failed read and its immediate status:

cat notes/route-symbolic.txt
printf 'read status: %s\n' "$?"

Expect a missing-target diagnostic and a nonzero read status. The exact error wording may vary. The link's own entry survives, but its path no longer reaches a file. Do not recreate route.txt with the seed redirection to hide this result; that would create a new regular file identity while the original shared file still survives through route-hard.txt.

Removing one hard-link name does not remove the shared file while another name still reaches it. More generally, removing a name is not proof that all data has been erased or that all open uses have ended. This exercise makes no secure-erasure claim and has no course trash or undo command.

Diagnose the relationship

ObservationLikely distinctionSafe next check
Same text, different device/inode pairs.Separate files can have equal contents.Compare metadata and the controlled edit result.
Hard-link creation refuses a different filesystem.Hard links cannot span filesystems.Respect the refusal; return to the intended owned same-filesystem fixture.
ln succeeds but cat through the symlink fails.Creating stored path text does not validate target resolution.Readlink the recognized link and resolve from its parent directory.
Stat and stat -L report different identities.One inspects the link; the other follows its target.Read the selected mode, type and device/inode pair together.
The copy changed too.The starting relationship or edited operand differs from the intended fixture.Reinspect identities and complete contents; do not force a replacement.
A third appended line appears twice.A non-idempotent append was replayed.Identify the last accepted state before any deliberate repair.

Independent task: repair a sibling-directory link

Keep the surviving three-line hard-linked file and two-line copy unchanged. Recheck the exact outer working path, ordinary owned notes/shortcuts entries, the surviving file identity and the still-empty shortcuts directory:

pwd
ls -ld -- notes shortcuts
ls -a -- shortcuts
stat -c 'device=%d inode=%i links=%h type=%F name=%n' -- notes/route-hard.txt
cat notes/route-hard.txt

Require the recorded state and absence of shortcuts/route-view.txt. The following deliberate mistake creates a relative symlink using path text that looks plausible from the current outer directory:

ln -sT -- notes/route-hard.txt shortcuts/route-view.txt

Before the hints or answer:

  1. Inspect the new link entry and stored path. Explain which path that text actually attempts from the directory containing the link.
  2. Predict the read result, then make the bounded read observation. Explain why the existing hard-linked file is not reached by this particular path.
  3. Repair only the recognized new symbolic-link entry. Preserve the shared file, copy and earlier dangling link. Use a relative target, no force replacement, and inspect the new stored path before following it.
  4. Verify that the repaired link reaches the recorded shared device/inode identity and reads the three exact lines. Explain why changing the terminal's current directory is not the repair.

Success: the repaired relative link in shortcuts reaches the existing three-line file in notes; all other recorded identities and contents stay unchanged. The original route.txt name remains absent, and its earlier symbolic link remains dangling. The task is repairing one stored path.

Graduated hints

  1. The link's containing directory is shortcuts. Resolve the stored path from there, one component at a time.
  2. The target is in a sibling directory. A relative path can first reach the parent directory and then enter the sibling.
  3. Reuse the taught .. component. Positively identify the incorrect link with ls -ld and readlink, remove only that named link interactively, confirm its absence, then create a fresh link with the corrected text.

Answer and Reasoning

From the verified outer exercise directory, inspect the intentionally wrong link before following it:

ls -ld -- shortcuts/route-view.txt
readlink -- shortcuts/route-view.txt

Require the recognized symbolic link created in this task, storing exactly notes/route-hard.txt. Its directory is shortcuts, so the attempted path is shortcuts/notes/route-hard.txt. That subtree is absent in the checked fixture. The link was created successfully because ln stored text; it did not require that text to reach an existing target.

cat shortcuts/route-view.txt
printf 'read status: %s\n' "$?"

Expect the bounded missing-target failure and a nonzero read status. With the same identity and response-convention checks, remove only this link:

rm -i -- shortcuts/route-view.txt

A refusal leaves the incorrect link in place; stop and inspect rather than running a replacement over it. After accepted removal, inspect separately:

ls -a -- shortcuts
ls -ld -- shortcuts

Require the same ordinary owned directory and the link name absent, with no other entries. Only then create the corrected relative target:

ln -sT -- ../notes/route-hard.txt shortcuts/route-view.txt

Inspect the new link's own entry and stored path before target resolution:

ls -ld -- shortcuts/route-view.txt
readlink -- shortcuts/route-view.txt

Require the recognized new symlink storing exactly ../notes/route-hard.txt. From shortcuts, .. reaches the outer exercise directory and notes enters the sibling. Verify the target identity and content:

stat -L -c 'device=%d inode=%i links=%h type=%F name=%n' -- shortcuts/route-view.txt
cat shortcuts/route-view.txt
cat notes/route-hard.txt
cat notes/route-copy.txt
readlink -- notes/route-symbolic.txt

The followed pair must match the surviving regular file with hard-link count 1. Both target reads show the three exact lines; the copy still has two. The earlier symlink still stores route.txt, whose name remains absent. Creating or removing symbolic links did not change the target's hard-link count. The corrected path is anchored at the symlink's parent, so changing the shell's current directory was never the missing repair.

Cleanup after all link observations

Reinspect the shared and exercise parent entries without trailing slashes:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/compare-file-links

Require the known ordinary owned directories, then enter and verify separately:

cd ~/linux-practice/compare-file-links
pwd

Require the recorded exercise path before the remaining inventory checks:

ls -a
ls -ld -- notes shortcuts
ls -a -- notes
ls -a -- shortcuts
ls -ld -- notes/route-copy.txt notes/route-hard.txt notes/route-symbolic.txt shortcuts/route-view.txt
readlink -- notes/route-symbolic.txt
readlink -- shortcuts/route-view.txt

Require exactly the known three remaining notes entries and the corrected shortcut, with route.txt absent. Recognize the two regular owned files and the two intentional symlinks with their recorded stored paths. Recheck file identities and complete contents if uncertain; preserve unknown entries.

Remove the two recognized symbolic-link names, without trailing slashes:

rm -i -- notes/route-symbolic.txt shortcuts/route-view.txt

These operands remove the symlink entries, not their targets. Honor the established response convention. When stopping, decline the current and every remaining prompt; one refusal does not cancel later prompts. Reinspect both inventories and require those link names absent. Verify that the three-line hard name and two-line copy still exist with expected content. Only after those checks remove the two known regular-file names:

rm -i -- notes/route-hard.txt notes/route-copy.txt

Again, approve only recognized targets and honor every refusal. Reinspect both ordinary directory identities and inventories:

ls -ld -- notes shortcuts
ls -a -- notes
ls -a -- shortcuts

Only when both are empty:

rmdir -- notes shortcuts
ls -a

Require the outer exercise directory empty, then return and check separately:

cd ~/linux-practice
pwd

Require the recorded shared-root path. Inspect the exercise entry again:

ls -ld -- compare-file-links

Only for that same recognized owned empty directory:

rmdir -- compare-file-links

Retain the shared root and other lessons. No filesystem layout or system configuration was changed.

Resume by recognizing the stage

Recheck account, command origins, option baseline, directory identity and exact location before resuming. Inspect existing entries with ls/stat, stored paths with readlink, and complete known regular-file contents with cat.

  • Initial source: one two-line regular file, no copy or links yet.
  • Relationships created: shared file has two lines and two hard-link names; the independent copy has two lines; the first symlink reaches source.
  • After the append: shared file has exactly three lines; copy still has two. Do not replay the append.
  • After source-name removal: route.txt is absent, the surviving hard name has three lines and count one, and the original symlink is dangling. Do not recreate route.txt with the source seed redirection: that would create a new regular file identity while the shared file still survives.
  • Independent repair: distinguish the wrong stored path, an absent link after removal, and the corrected stored path before choosing the next action.

For partial creation between these stages, inspect every existing identity and create only the missing destination whose absence is confirmed. A declined removal means that transition has not happened. Do not infer completion from the command you intended to run.

For a full restart, use the same named, inspected removal procedure for only the recognized entries actually created; include route.txt only if its recorded name still exists. Remove empty owned directories afterward and start fresh. Preserve unexplained files and never replace a link forcibly to conceal a mismatch.

Sources and other study views

The command forms target the course's GNU Coreutils 9.4 and Bash 5.2 baseline. The upstream Coreutils manual may describe a newer release; this task uses only the bounded forms explained here. Device and inode values are actual observations to record, not fixed expected numbers.

Revision reconstructs the relationships and transitions; Quick Reference retrieves syntax with the necessary guards.

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy