Linux Hard Links, Symbolic Links and Inodes
How it works
An inode describes a filesystem object and its metadata. A directory entry connects a name to that object. For ordinary local files, a device identifier plus inode number identifies the object at a particular time; inode numbers can be reused after deletion. A hard link is another directory entry for the same object. A symbolic link is a separate object that stores a target pathname. Hard links normally cannot cross filesystems, and ordinary users cannot hard-link directories. Symlinks can cross filesystems and can point to a nonexistent target. A relative symlink target is interpreted from the symlink’s containing directory, not from the shell working directory.
Prepare a safe practice environment
Use GNU/Linux and Bash as a normal user. From the extracted package root run bash lab-fixtures/files/setup.sh. It prints a fresh private directory under /tmp. Copy that path and enter it with cd (quote the path if needed); check pwd. Each code example below starts in a new lab with the same fixture: notes/hello.txt, notes/daily report.txt, notes/-draft, notes/.hidden, and empty directories archive and empty. Set export LC_ALL=C for the shown ordering and English diagnostics. Never run the modifying examples on your own files.
Two names for one inode
ln notes/hello.txt notes/second.txtstat -c '%d:%i links=%h' notes/hello.txt notes/second.txtThe device and inode pairs match, and both lines say links=2. The numbers themselves vary. This is one object, not two independent copies.
Create a symbolic link across sibling directories
ln -s ../notes/hello.txt archive/shortcutreadlink archive/shortcutcat archive/shortcutThe first line is the stored target ../notes/hello.txt; the second is hello from the resolved file.
Compare both links after removing the original name
ln notes/hello.txt notes/second.txtln -s ../notes/hello.txt archive/shortcutrm -- notes/hello.txtcat notes/second.txtreadlink archive/shortcutThe hard link still reads hello. The symlink still stores the old path, but that path no longer resolves. It is a dangling link. Only the fixture name was removed.
The hard link still names the original object, so it remains readable after the first name is removed. The symbolic link instead asks the filesystem to resolve the old pathname. readlink can report that stored pathname even when opening the target would fail.
Common mistakes and safe habits
Neither link is automatically a backup. Editing data through a hard link changes the shared object; replacing a name with a new inode is a different operation. A symlink can become broken or resolve to a different object if its target changes. Use readlink to inspect the stored path and stat -L when you deliberately want metadata of the target. Do not follow unknown links during modifying operations.
Try it yourself
Create a hard link notes/alias.txt and a symlink archive/alias.txt to the fixture hello file. Compare device/inode pairs, remove only the original name and predict which link still reads the data.
Use a fresh lab for the practice. When finished, return to the lab root if you are in a subdirectory, then leave it with cd ... Remove only the identified temporary lab folder using your file manager if desired. Never remove an unrelated path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Links and inode identity — Knowledge Check
Apply filesystem concepts in a fresh disposable Linux lab.
Which link shares the target inode?
Where is a relative symlink target resolved from?
What happens to a symlink when its only target name is removed?
Can a hard link normally span two filesystems?
Which pair identifies a local file object at a given time?