One tree, rooted at /
3 min readOn Windows you deal with separate drive letters, and a file name starts with something like C:. Linux has one tree. Your own files, the installed programs, the devices, and even the kernel's running view of the machine all hang off a single the top of the filesystem tree, written as /, with every other path descending from it. If you can read that tree, you can find and fix almost anything on the system.
The the way an operating system organises files and directories on storage, in Linux a single tree rooted at / follows a standard layout at its top level called the Filesystem Hierarchy Standard, or FHS. Each top-level directory has a job. /etc holds system-wide configuration. /home holds each user's personal files. /var holds changing data such as logs and caches. /usr holds installed programs and libraries. /dev holds device files, the way the system talks to hardware. /tmp holds temporary files that may be cleared on reboot. /run holds runtime state created since boot, and /sbin holds system-administration programs that often need root.
On a modern distribution you may notice /bin and /sbin are just symbolic links to /usr/bin and /usr/sbin. This merged-usr layout concentrates the programs in one place, while the separate names remain for backwards compatibility. A few more top-level directories complete the picture: /mnt is for filesystems an administrator mounts temporarily by hand and /media is where removable media such as USB sticks are mounted automatically, /root is the root user's home directory, /opt holds self-contained third-party software, and /srv holds data served to others such as a website.
Two directories are special: /proc and /sys. They are a filesystem whose contents the kernel generates in memory on the fly, with nothing stored on disks. The kernel synthesises their contents while the system runs, so they live in memory and disappear at shutdown. That is why they report a size of zero even though they are full of entries:
ls -ld /proc /sys
Typical output (yours will differ) shows dr-xr-xr-x and a size of 0 for both. The size is 0 not because the directory is empty, but because nothing in it is stored on disk. It is kernel state, rebuilt every boot. /proc exposes one directory per running process plus system statistics; /sys exposes devices and kernel objects. Both are how administrators and tools read live kernel state through ordinary file operations.
A BusyBox-based board still needs the same core directories: /bin, /etc, /dev, /proc, /sys and /tmp. It often omits /home and merges /usr into /bin to save space, and it mounts /proc and /sys early in init scripts because tools rely on them. The mental model carries over unchanged.
Paths and navigation
3 min readTo use the tree you have to name a location, and there are two ways. A a slash-separated list of directory names that locates a file or directory in the tree is either absolute or relative. An absolute path starts at the root and names the full route, such as /home/learner/notes. A relative path starts from where you are right now, shown by pwd (print working directory). If you are in /home/learner, then notes means /home/learner/notes. Absolute paths always mean the same thing no matter where you run them; relative paths depend on your current directory.
Three shortcuts save typing. . means this directory. .. means the parent. ~ means your home directory. So cd .. goes up one level, cd ~ goes home, and ls . lists the current directory explicitly. You can chain them: cd ../.. goes up two levels, and ~/notes is your home's notes directory.
pwd
cd /tmp
pwd
cd ..
pwd
cd ~
pwd
The first cd uses an absolute path (it starts with /), so it works from anywhere. The second moves up one level, relative to wherever you now are.
An edge case worth knowing: the root has no parent. From /, running cd .. simply stays at /. It does not error and it cannot go higher, because there is nothing above the root. The kernel treats .. at the root as / again, which keeps the tree consistent everywhere.
Three more conventions round out navigation. A trailing slash is usually harmless: /tmp/ and /tmp name the same directory. cd - jumps back to your previous directory, which is handy when toggling between two places. And to run a program that sits in your current directory you must prefix it with ./, because the shell does not search . in the PATH; ./script is an explicit relative path, while script alone would be looked up in the PATH directories.
learner@embeddex:~/lab$ # Where am I now? learner@embeddex:~/lab$ pwd /home/learner/lab learner@embeddex:~/lab$ # Move with an absolute path learner@embeddex:~/lab$ cd /tmp learner@embeddex:/tmp$ pwd /tmp learner@embeddex:/tmp$ # One level up: /tmp's parent is / learner@embeddex:/tmp$ cd .. learner@embeddex:/$ pwd / learner@embeddex:/$ # The root has no parent, so this stays at / learner@embeddex:/$ cd .. learner@embeddex:/$ pwd / learner@embeddex:/$ # Tilde always goes home learner@embeddex:/$ cd ~ learner@embeddex:~/lab$ pwd /home/learner/lab
A file is a name and an inode
3 min readHere is the mental model that makes the rest of this session click: a file is two separate things. The data lives in a numbered record called an the on-disk record that holds a file's metadata and the location of its data blocks, identified by a number rather than by its name. The name is just an entry in a directory that points to that inode. The inode holds the size, permissions, ownership, three timestamps, and a the number of names that point at an inode; deleting a name decrements it — how many names point at it. Notice what is missing: the inode does not store the filename. The name lives only in the directory entry.
stat shows this. Run it on a file and read the Links line, which is the count of names pointing at that inode, and the Inode line, which is its number:
echo hello > a.txt
stat a.txt
ls -i a.txt
Typical stat output (yours will differ) includes Size: 6, Links: 1, and an Inode: number. ls -i prints that same inode number as the first column. A fresh file has a link count of 1: exactly one name points at it.
The important idea is that the name and the data are separate. A directory is not a box that contains files; it is a table of name to inode. This separation is exactly what makes links possible, which is the next two sections.
ls -li shows all of this in one line: inode number, permissions, link count, owner, group, size, date and name. Directories also have link counts, and they start higher than 1: a fresh directory has at least 2, because its own name in the parent plus its internal . entry both point at it, and each subdirectory adds one more through its .. entry. That is why a directory's link count grows as you add subdirectories, while a regular file's link count only grows when you hard-link it.
learner@embeddex:~/lab$ echo hello > a.txt learner@embeddex:~/lab$ # Note Links: 1 and the Inode number learner@embeddex:~/lab$ stat a.txt File: a.txt Size: 6 Blocks: 8 IO Block: 4096 regular file Device: 8,48 Inode: 72241 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ learner) Gid: ( 1000/ learner) Access: 2026-11-15 05:45:18.070069357 +0200 Modify: 2026-11-15 05:45:18.070069357 +0200 Change: 2026-11-15 05:45:18.070069357 +0200 Birth: 2026-11-15 05:45:18.070069357 +0200 learner@embeddex:~/lab$ # The same inode number appears as the first column learner@embeddex:~/lab$ ls -i a.txt 72241 a.txt learner@embeddex:~/lab$ # Inode, permissions, link count, owner, size and name in one line learner@embeddex:~/lab$ ls -li a.txt 72241 -rw-r--r-- 1 learner learner 6 Nov 15 05:45 a.txt
Hard links
3 min readBecause a name is separate from the data, you can give one inode several names. ln creates a a second directory entry that points at the same inode as another name, sharing the same data. Both names point at the same data, and the link count rises to 2. Delete one name and the data survives, because the other name still points at it. Neither name is the original; they are two equal labels for one file.
1create the link
echo important > orig.txt
ln orig.txt hard.txt
ls -li orig.txt hard.txt
Both lines show the same inode number, and the link count (the second column of ls -l) is now 2.
2delete one name and watch
rm orig.txt
cat hard.txt
stat hard.txt
cat hard.txt still prints the data, and stat now reports Links: 1. Removing one name only lowered the count; the data lives until the last name is gone.
Hard links have two hard limits. They cannot cross filesystems, because an inode number is only meaningful within its own filesystem. And hard links to directories are normally forbidden, because they could create cycles in the tree that would break traversal. In both cases the system refuses with an error, and a symbolic link is the right tool instead.
Trying to hard-link across filesystems fails with Invalid cross-device link, and linking to a directory fails with hard link not allowed for directory. Reach for ln -s in both cases.
To find every name for one file, search by inode number: find . -inum <number> lists all hard links to it. Note the difference from copying. cp reads the data and writes a fresh inode, so the copy is independent, while ln shares the inode, so editing one name changes what the other name sees. Hard links are used deliberately to deduplicate identical files, and by backup tools such as rsync --link-dest that keep unchanged files as hard links to save space.
learner@embeddex:~/lab$ # one file, one name learner@embeddex:~/lab$ echo important > orig.txt learner@embeddex:~/lab$ ls -li orig.txt 72241 -rw-r--r-- 1 learner learner 10 Nov 15 05:45 orig.txt learner@embeddex:~/lab$ ln orig.txt hard.txt learner@embeddex:~/lab$ # same inode number, link count is now 2 learner@embeddex:~/lab$ ls -li orig.txt hard.txt 72241 -rw-r--r-- 2 learner learner 10 Nov 15 05:45 hard.txt 72241 -rw-r--r-- 2 learner learner 10 Nov 15 05:45 orig.txt learner@embeddex:~/lab$ rm orig.txt learner@embeddex:~/lab$ # the data survives through the other name learner@embeddex:~/lab$ cat hard.txt important learner@embeddex:~/lab$ stat -c 'links=%h inode=%i name=%n' hard.txt links=1 inode=72241 name=hard.txt
Symbolic links
3 min readln -s creates a a small file whose content is a path to another file, resolved by the kernel each time it is opened, usually called a symlink. Unlike a hard link, it has its own inode and its own data — the data is just a path string. ls -l shows the link with an arrow, and readlink prints the stored path:
echo important > orig.txt
ln -s orig.txt soft.txt
ls -l soft.txt
readlink soft.txt
The ls -l line reads soft.txt -> orig.txt. A symlink does not change the target's link count, because it does not point at the inode — it points at a name.
Because a symlink stores a path, not a reference to an inode, it can break. Delete the target and the link is left dangling: it points at a name that no longer exists, and reading it fails with No such file or directory.
rm orig.txt
cat soft.txt
The target of a symlink can be relative or absolute, and this matters. A relative target such as orig.txt is resolved starting from the directory that contains the link, not from your current directory. An absolute target such as /home/learner/orig.txt always means the same thing. This is why moving a link and its relative target together keeps working, while moving only one of them breaks the link.
readlink -f resolves a symlink all the way to a final real path, following every link in the chain, and it fails if the target is missing. A symlink can also point at itself, directly or in a circle; the kernel caps the number of links it follows and then fails with Too many levels of symbolic links, which is the tell-tale sign of a link loop.
learner@embeddex:~/lab$ echo important > orig.txt learner@embeddex:~/lab$ ln -s orig.txt soft.txt learner@embeddex:~/lab$ # The arrow shows soft.txt -> orig.txt learner@embeddex:~/lab$ ls -l soft.txt lrwxrwxrwx 1 learner learner 8 Nov 15 05:45 soft.txt -> orig.txt learner@embeddex:~/lab$ # The stored path string learner@embeddex:~/lab$ readlink soft.txt orig.txt learner@embeddex:~/lab$ # Delete the target learner@embeddex:~/lab$ rm orig.txt learner@embeddex:~/lab$ # A dangling symlink fails to read learner@embeddex:~/lab$ cat soft.txt cat: soft.txt: No such file or directory
Inside the inode and the directory entry
4 min readNow look under the hood. A a name paired with an inode number, stored inside a directory, linking a filename to its file is tiny: a name and an inode number, and nothing else. The inode is where everything about the file lives: the file type and permissions (the mode), the owning user and group, the size in bytes, three timestamps, the link count, and a set of pointers to the data blocks.
The three timestamps trip people up. atime is the last access time, mtime the last modification of the file's contents, and ctime the last change to the inode itself, such as a rename, a chmod, or a hard-link creation. ctime is change time, not creation time. Creation (birth) time is a separate, newer field: ext4, XFS and Btrfs store it, and since coreutils 8.31 stat shows it on the Birth: line through the statx() system call (older tools and some filesystems show -). A rename changes ctime but not mtime, because the data did not change.
Start at the root or the working directory: the kernel begins at
/for an absolute path, or at the process's working directory for a relative one.Read the next component: it looks up the next name, such as
home, in the current directory's entries.Follow the inode: the matched entry gives an inode number, and the kernel reads that inode to learn whether it is a directory.
Descend and repeat: if it is a directory, the kernel moves into it and repeats from step 2 for the next component.
Resolve links and the last component: a symlink redirects to its stored target; the final component yields the inode the name was after.
This walk is cached in memory as dentries (directory-entry cache objects), which is why repeating a path is fast even though the on-disk lookups are many small reads.
Going deeperWhat an ext4 inode and directory entry actually look like
On ext4, an inode is a fixed-size structure per file. Its fields include i_mode (file type and permissions), i_uid and i_gid (ownership), i_size (logical size), i_links_count (link count), i_atime, i_mtime, i_ctime, and a 60-byte area that says where the data lives. On ext2 and ext3 that area is an array of block pointers: 12 direct pointers, then one single-, one double- and one triple-indirect pointer, which is how a fixed-size inode can describe a very large file. ext4 normally uses the same 60 bytes for an extent tree instead: each extent says "these N consecutive blocks start at block B", so a large contiguous file needs a handful of extents rather than thousands of pointers (filefrag -v file shows them; ext4 still reads the old indirect format for compatibility). A directory is itself a file whose blocks hold entries of the form:
struct ext4_dir_entry_2 {
__le32 inode; /* inode number this name points at */
__le16 rec_len; /* length of this entry */
__u8 name_len; /* length of the name */
__u8 file_type; /* regular file, dir, symlink, ... */
char name[]; /* the name, not null-terminated */
};
Notice the inode field is just a number and the name sits next to it. This is the literal realisation of "a directory is a table of name to inode", and it is why a filename can be at most 255 bytes on ext4 while the inode knows nothing about the name.
Globbing and the shell
3 min readThe shell can expand patterns before it runs a command, which is how you select many files at once. A a filename pattern using wildcards such as * and ? that the shell expands into a list of matching names is expanded by the shell itself, not by the program you run. ls *.txt never sees the asterisk: the shell replaces the pattern with every matching filename first. * matches any number of characters, ? matches exactly one, and [...] matches one of the listed characters or a range such as [a-c].
The classic mistake is thinking ? means one or more. It means exactly one. So f?.txt matches f1.txt but not f10.txt (two characters) or f.txt (zero).
ls f?.txt
mkdir txt log png bak
mv *.txt txt/
mv *.log log/
{a,b} is different. It is brace expansion, which happens even when no files match, and it is not really a wildcard: {txt,log} expands to the two words txt log regardless of what is on disk. Two more subtleties matter. * does not match a leading dot, so hidden files such as .bashrc need .* or .[!.]* explicitly. And if a pattern matches nothing, the shell by default passes the literal pattern to the program, which is why a typo can produce a confusing "No such file or directory" naming the pattern itself.
Going deeperGlobbing is not a regular expression
People who know regex often reach for * and . the way regex uses them, and it breaks. In a glob, * means any characters, not "zero or more of the previous thing", and . is a literal dot, not "any character". So *.txt means "anything ending in .txt", while the equivalent regex would be .*\.txt. A glob also matches whole names, while a regex can match part of a line. grep takes a regex; the shell's filename expansion is a glob. Keep the two syntaxes separate.
Bash gives you control over the edge cases. With shopt -s nullglob, a pattern that matches nothing expands to nothing at all instead of the literal pattern, which removes a whole class of surprising errors in loops. With shopt -s failglob, the shell reports an error instead. With shopt -s extglob, you get richer patterns such as @(a|b) for one of several and +(x) for one or more, though these stray beyond the portable basics.
f2.txt
learner@embeddex:~/lab$ # * matches any run, including zero: f.txt is back too
learner@embeddex:~/lab$ ls f*.txt
f.txt
f1.txt
f10.txt
f2.txt
learner@embeddex:~/lab$ mkdir txt log
learner@embeddex:~/lab$ # The shell expands *.txt before mv ever runs
learner@embeddex:~/lab$ mv *.txt txt/
learner@embeddex:~/lab$ mv *.log log/
learner@embeddex:~/lab$ ls txt log
log:
app.log
txt:
f.txt
f1.txt
f10.txt
f2.txt
learner@embeddex:~/lab$ # Brace expansion works even when no file matches
learner@embeddex:~/lab$ echo {txt,log}
txt logDeleted but still open
3 min readrm does not destroy a file; it calls the the operation that removes a name from a directory and decrements the inode's link count system call, which removes one name and decrements the link count. The data is freed only when two things are both true: the link count has reached zero, and no process has the file open. If a process still holds a a process's handle to an open file, used by read and write, independent of the file's name, the kernel keeps the inode and its blocks alive until the last descriptor closes. This is why df can report a disk full while du finds nothing large: a deleted file is still held open, invisible to directory listings but still using blocks.
Two levels are involved, and it is worth keeping them straight. The file descriptor is the small number a program holds; it points to an open file description, which records the file offset and mode; that in turn points to the inode. When a process is forked, the child shares the same open file description, and that is exactly why a deleted log file does not free its space until every process that inherited the descriptor has closed it. Log rotation depends on this: a daemon keeps writing to a renamed and deleted file until it is told to reopen, which is why logrotate sends a signal after rotating.
1hold a file open and delete its name
echo secret > data.bin
exec 3<data.bin
rm data.bin
exec 3<data.bin opens the file on descriptor 3 in the current shell. Deleting the name removes the only directory entry, but the descriptor still refers to the inode.
2see the proof in /proc
ls -l /proc/$$/fd/3
The entry shows descriptor 3 still pointing at data.bin, with the path marked (deleted). The inode is alive even though no directory lists the name.
3read the data back
cat <&3
The data is still there, read through the open descriptor. Close it and the blocks are finally freed.
Named and open: the file has one name and one open descriptor; the link count is 1.
Unlinked:
rmremoves the name and the link count drops to 0, but the descriptor keeps the inode referenced.Still allocated: blocks stay allocated, so
dfstill counts them whileduno longer sees them.Visible as (deleted):
/proc/<pid>/fd/shows the descriptor pointing at a path marked(deleted).Last close frees it: when the final descriptor closes, the inode and its blocks are reclaimed and the space appears.
learner@embeddex:~/lab$ echo secret > data.bin learner@embeddex:~/lab$ # open the file on descriptor 3 learner@embeddex:~/lab$ exec 3<data.bin learner@embeddex:~/lab$ rm data.bin learner@embeddex:~/lab$ # the kernel still knows the file, marked (deleted) learner@embeddex:~/lab$ ls -l /proc/$$/fd/3 | sed 's/.*-> //' /home/learner/lab/data.bin (deleted) learner@embeddex:~/lab$ cat <&3 secret
Common mistakes and how professionals diagnose them
3 min readThese are the mistakes learners hit most, with the symptom, the cause, and the diagnostic that proves it.
cat link.txt: No such file or directory. The symlink's target was deleted, so the link dangles. Diagnose withls -lto see the arrow, then either recreate the target or remove the link.f?.txtdid not matchf10.txt.?matches exactly one character andf10has two. Diagnose by reading the pattern aloud, then usef*.txt, where*matches any number of characters, including none.ln: Invalid cross-device link. A hard link cannot cross filesystems, because inode numbers are local to one filesystem. Diagnose withdfon both paths; useln -sinstead.- Disk still full after
rm. A process still holds the deleted file open. Diagnose withlsof +L1, which lists files with a link count below 1, then restart or close that process. duanddfdisagree wildly. Either deleted-but-open files (found withlsof +L1) or files hidden underneath a mount point (mounting over a non-empty directory hides its contents fromdubut not fromdf).No space left on devicewhiledf -hshows free space. The inode table is full. Checkdf -i.cd ..at/did nothing. The root has no parent; the kernel maps..at the root to/again. This is expected, not an error.- Confused by
statshowingLinks: 1. It is the count of names pointing at the inode. Hard-link the file and watch it become 2; a symlink does not change it.
Going deeperInode exhaustion on small embedded filesystems
A filesystem can be full in two independent ways: out of data blocks, or out of inodes. Embedded flash filesystems are sized tightly, and a directory full of thousands of tiny files can exhaust the inode table long before the data blocks are used. df -i shows inode usage separately from df's block usage, and a filesystem at 100% inodes refuses to create new files even with gigabytes free. This shows up on real devices as log files that cannot be written or packages that fail to install with No space left on device. On ext4 the inode count is fixed at mkfs time, so the fix is either to remove small files or recreate the filesystem with a larger inode count. Tools like find -printf or a count of directory entries reveal where the inodes went.
Before changing anything, confirm the diagnosis with one command: ls -l for a dangling link, lsof +L1 for a deleted-but-open file, and df -i for inode exhaustion. The fix that matches the proven cause is usually small.
Try it
1 min readOpen the lab for this session and work through it in a scratch directory. Organise 500 mixed files into four folders using four mv commands, run the hard-link and symlink experiment and record which link still reads after you delete the original, then hold a file open, delete it, and recover the data through /proc/$$/fd/. Each exercise proves one idea from this lesson, and the practice pack's questions and interview prompts cover the same ground. Do the lab before moving on to viewing and editing text.
Key takeaways
- Linux is one tree rooted at /, and the FHS says where each kind of file lives; /proc and /sys are virtual.
- An absolute path starts at the root; a relative path starts from the working directory; cd .. at / stays at /.
- A filename is a directory entry pointing to an inode that holds the data, metadata and link count.
- A hard link is a second name for the same inode; a symlink stores a path and dangles when its target is deleted.
- Hard links cannot cross filesystems or point at directories; symlinks can do both.
- Globbing is done by the shell: * matches any run, ? exactly one character, and {a,b} is brace expansion.
- Deleting a name only lowers the link count; the data lives until the last open file descriptor closes.
Reflect
- Explain to a colleague, in two sentences: describe the single filesystem tree and state what /etc, /var, /home, /usr, /dev, /proc, /sys and /tmp hold.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- SpecificationFilesystem Hierarchy Standard 3.0
- Man pageinode(7) — Linux man page
- Man pagestat(1) — Linux man page
- Man pageln(1) — Linux man page
- Man pagesymlink(7) — Linux man page
- Man pageunlink(2) — Linux man page
- Man pageproc(5) — Linux man page
- Official docsext4 filesystem documentation
- BookThe Linux Programming Interface (Michael Kerrisk)
- Man pagehier(7): filesystem layout
- Man pageglob(7): pathname patterns
- Man pagefind(1)
- Free bookThe Linux Command Line (William Shotts)
Official documentation first. Links checked when the lesson was published.
