Question bank — Directory structure — tree, DAG (hard links, symbolic links)
Before we start, two pictures that everything below leans on.
Picture 1 — a name is not an inode. Directory entries live in directories; the actual file lives in an inode. Names point at inodes; the inode keeps a counter of how many names point at it.

Recall The one fact that unlocks all traps
A directory entry (name) and an inode (data + metadata) are separate things. A name points at an inode; the inode stores a link count — call it — equal to the number of directory entries pointing at it. Hold this picture and most traps dissolve.
Picture 2 — one shared file turns the tree into a DAG. In a pure tree every file has exactly one name (one incoming arrow). The moment two names point at one inode, that node has two incoming arrows — no longer a tree, but still acyclic: a DAG.

Spot the difference visually
Hard link vs symlink at a glance. A hard link is a second name wired straight to the inode ( goes up). A symlink is a tiny separate inode whose data is the text of a path — it does not touch , so deleting the target leaves it dangling.

True or false — justify
True or false: rm file.txt deletes the file's data from disk.
rm calls unlink, which removes one name and decrements the inode's link count ; data is freed only when and no process holds it open.True or false: a hard link creates a copy of the file.
True or false: creating a symbolic link increases the target's link count.
True or false: a filesystem with hard links is still a tree.
True or false: you can hard-link a file across two different filesystems.
True or false: you can symlink to a directory.
True or false: every directory, even in a "pure tree", contains entries that make the directory graph cyclic.
. (itself) and .. (its parent), so the directory graph technically has cycles. That is precisely why the kernel — not users — manages these and why user directory hard links are banned.True or false: after rm a.txt on a file that also has hard link b.txt, cat b.txt still works.
rm a.txt only drops from 2 to 1; the inode and its data survive because b.txt still references them.True or false: a symlink and its target always share the same inode number.
l); the target has a different inode. That is the whole difference from a hard link, which shares the target's inode.True or false: stat on a symlink and stat on its target return the same metadata.
lstat reports the symlink's own metadata (tiny size = length of the path string, type l); following it to the target gives different size, type, and inode. See Unix system calls — link, unlink, symlink, stat.Spot the error
Spot the error: "A hard link points at a path, so if I rename the target the link breaks."
Spot the error: "The link count tells you how many symlinks point to a file."
Spot the error: "To keep a file alive, keep a symlink to it."
Spot the error: "When link count reaches 0 the data is freed immediately, no exceptions."
Spot the error: "ls -l shows s.txt -> a.txt, so s.txt is just an alias inode for a.txt."
-> and the l type char reveal it's a symlink: a separate inode whose data is the text a.txt. An alias-for-the-same-inode is a hard link and shows no arrow and no l.Spot the error: "Because it's a DAG, I can create a cycle of directories with hard links."
find, du) would never terminate.Why questions
Why does the OS separate a file's name from its inode instead of storing the name inside the file?
Why is the filesystem a DAG and not a general graph?
Why can't a symlink increment the target's link count even though it "points to" it?
Why does deleting one hard link never lose data but deleting a symlink's target does?
Why does the kernel cap symlink chain depth (typically about 40 hops)?
ELOOP — a Unix error code (from errno.h) meaning literally "too many levels of symbolic links".Why must hard links stay within one filesystem while symlinks can cross?
/ through mount points, so it can land anywhere.Edge cases
What happens to the link count when you ln -s a.txt s.txt (symlink)?
a.txt's link count stays unchanged (e.g. 1). Only s.txt's brand-new inode is created; symlinks never touch the target's count.What is a "dangling" symlink and how is one created?
cat on it gives "No such file or directory".If a file has link count 2 and one name is rm'd while a process still has it open, what is the state?
What does the link count of a brand-new empty directory equal, and why?
. entry pointing back at itself. Each subdirectory it later gains adds 1 (via that child's ..).Can a hard link and the file it links to have different permissions or timestamps?
If you cp a symlink, do you get a copy of the link or a copy of the target?
cp follows the link and copies the target's data; cp -P (or cp -a) copies the symlink itself (the path-string file). This is a frequent silent trap.Connections
- Inodes and File Metadata
- Path Resolution and the namei() routine
- Reference Counting and Garbage Collection
- Graphs — Trees vs DAGs
- Unix system calls — link, unlink, symlink, stat
- Mount points and Virtual File System (VFS)