Hard links and symbolic links

Two ways to give a file a second name: a hard link, which is another directory entry for the same inode, and a symbolic link, which is a small file containing a path.

Lesson 5 of 6 in objective 104. Devices, Linux filesystems, filesystem hierarchy standard, part of LPIC-1 Exam 101-500.

Two ways to give a file a second name. Hard link — It is: Another directory entry for one inode; Across filesystems?: No — inode numbers are per filesystem; To a directory?: No; If the target goes: Nothing happens. There was no original. Symbolic link — It is: A small file containing a path; Across filesystems?: Yes; To a directory?: Yes; If the target goes: It dangles, pointing at nothing Hard link Symbolic link It is Another directory entry for one inode A small file containing a path Across filesystems? No — inode numbers are per filesystem Yes To a directory? No Yes If the target goes Nothing happens. There was no original It dangles, pointing at nothing
Two ways to give a file a second name.

What an inode is, and what a name is

A file's data and metadata live in an inode; the name is a directory entry pointing at that inode. A hard link is simply a second directory entry pointing at the same inode, so the two names are equal in every way — there is no original. The link count in ls -l is how many names the inode has, and the data is released only when that count reaches zero and no process holds it open. ls -i prints the inode number, which is how you prove two names are the same file.

Because a hard link is a directory entry pointing at an inode, and inode numbers are only meaningful within one filesystem, a hard link cannot cross a filesystem boundary. Directories cannot be hard linked either, on ordinary systems, because it would let you build loops in the tree.

Two names can be one file, and the inode number is what proves it. Left column, The name in the directory; right column, The inode it points at. backup.tar and archive.tar both point at Inode 41207 (Link count 2. Neither name is the original). notes.txt points at Inode 58106 (A different number, so a different file). The name in the directory The inode it points at backup.tar archive.tar Inode 41207 Link count 2. Neither name is the original notes.txt Inode 58106 A different number, so a different file
Two names can be one file, and the inode number is what proves it.

Symbolic links, and how they fail

A symbolic link is a tiny file whose content is a path. It can point anywhere — another filesystem, a device, a path that does not exist yet — and it is resolved when it is used. That flexibility is exactly its weakness: delete or move the target and the link remains, now dangling and pointing at nothing. ls -l shows it with an l in the type column and an arrow to its target.

ln makes a hard link, ln -s a symbolic one, and the argument order is target first, then the new name. A symbolic link created with a relative path is resolved relative to the LINK's directory rather than to the working directory you made it from, which is the usual cause of a link that works when created and breaks the moment you cd elsewhere.

Three commands show the path a link STORES rather than following it to what it points at: ls -l prints it after the arrow, readlink prints the bare string and nothing else, and stat reports it on a line of its own. Ordinary reading goes the other way — cat opens the target, and ls -L or stat -L follow the link too — and the split underneath is the lstat system call against stat. When a link points at another link, plain readlink still prints only the single hop it holds; readlink -f walks the whole chain and resolves every . and .. to one absolute path, -e is the strict variant that prints nothing unless every component exists, and -m the permissive one that checks nothing at all. realpath is the same canonicalisation as a command in its own right.

The order of an ln -s line, and where a relative target is read from. ln -s ../data/report.csv docs/report.csv — part 1, -s: Symbolic. Plain ln makes a hard link; part 2, ../data/report.csv: Target first. Resolved from docs, the link's own directory; part 3, docs/report.csv: The new name, second. This is the file created. ln 1 -s 2 ../data/report.csv 3 docs/report.csv 1 Symbolic. Plain ln makes a hard link 2 Target first. Resolved from docs, the link's own directory 3 The new name, second. This is the file created
The order of an ln -s line, and where a relative target is read from.
cd /home/carolln -s ../shared/data.csv /srv/app/data.csvreadlink /srv/app/data.csv../shared/data.csvthe stored string, unchanged. Nothing is resolved yetreadlink -f /srv/app/data.csv/srv/shared/data.csvread from /srv/app, where the link is — not /home/carol
A relative link, resolved from the link's own directory and not from yours.

Worth carrying in

ln target name
Hard link: another directory entry for the same inode.
ln -s target name
Symbolic link: a file containing a path.
ls -i
Inode numbers. Two names with one number are the same file.
ls -l
Link count in the second column; l and an arrow for symbolic links.
readlink
The one path string a link stores. stat and ls -l show the same thing.
readlink -f
Follow the whole chain to one absolute path. -e requires every component to exist; realpath is the same job.

What the exam does with this

Objective
104. Devices, Linux filesystems, filesystem hierarchy standard
Share of the exam
25% (the whole objective)
Questions in this lesson
11
Signed for by a person
0

Partly checked. None of the 11 questions here has been read against the cited source by a person. 11 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Hard links and symbolic links

Questions in this lesson

Practise Hard links and symbolic links

The rest of objective 104