3.3. Navigating the Filesystem
Everything on a Debian system lives in one tree rooted at /. Programs,
configuration, user data, and the kernel's runtime interfaces all have
assigned places in that tree, and the assignment is stable enough that
knowing it is most of finding anything.
3.3.1. The Debian Directory Layout
List the root directory:
$ ls /
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
Figure 3.4 groups these by purpose.
Figure 3.4. Package-managed files under /usr, host configuration in
/etc, growing state in /var, and kernel interfaces in memory-backed
filesystems.
| Directory | Holds |
|---|---|
/etc |
System configuration: text files the administrator edits. |
/home |
Ordinary users' home directories, one per account. |
/root |
root's home; reachable only with privilege. |
/usr |
Package-installed programs, libraries, and shared data. |
/var |
Variable state: logs, caches, spools, service data. |
/tmp |
Temporary files; on this system, memory that is cleared at boot. |
/boot |
Kernel, initramfs, and boot loader files. |
/srv, /opt, /media, /mnt |
Served data, add-on software, mounted media. |
Three conventions follow from that table. Configuration belongs in /etc -
plain text files, so that everything from backup to a grep works on them.
Data that grows belongs in /var, never in /etc. And files under /usr
belong to packages: Chapter 6 manages them, and
manual changes there are overwritten on upgrade. /usr/local exists
precisely for locally installed software that the package manager must not
touch.
The paths /bin, /sbin, /lib, and /lib64 are still valid, but on
Debian 13 they are symbolic links into /usr - the "merged /usr" layout
that consolidates all package files under one directory:
$ ls -ld /bin /sbin /lib /lib64
lrwxrwxrwx 1 root root 7 May 8 2026 /bin -> usr/bin
lrwxrwxrwx 1 root root 7 May 8 2026 /lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 8 2026 /lib64 -> usr/lib64
lrwxrwxrwx 1 root root 8 May 8 2026 /sbin -> usr/sbin
Scripts written decades ago keep working through those links; new paths
should use the real locations under /usr.
Four directories in the listing are not on the disk at all. They are kernel-provided filesystems mounted at boot, which is why their contents look nothing like files on the root filesystem:
$ for d in /proc /sys /dev /run; do findmnt -no TARGET,FSTYPE "$d"; done
/proc proc
/sys sysfs
/dev devtmpfs
/run tmpfs
/proc exposes processes and kernel state, /sys devices and drivers,
/dev device files, and /run runtime state since boot. findmnt prints
what is mounted and where; the -no options select columns without
headings. /tmp is the same kind of memory-backed filesystem here:
$ findmnt -no TARGET,FSTYPE /tmp
/tmp tmpfs
Anything written to /tmp or /run disappears at boot - convenient for
scratch space, fatal for data. Chapter 11 covers
mounts generally and Chapter 12 disk filesystems.
Which package owns a file is a one-command question, and the answer distinguishes package files from local ones:
$ dpkg-query -S /usr/bin/ls
coreutils: /usr/bin/ls
ls comes from coreutils; an unowned path under /usr is a local change
worth investigating. Chapter 6 develops this
into full package inspection.
3.3.2. Absolute Paths, Relative Paths, and Hidden Files
An absolute path starts at the root (/home/john/...) and means the same
thing everywhere. A relative path starts from the working directory:
sub, ../notes, ./hello.sh. The shell tracks the working directory, and
every command that accepts file names interprets relative ones against it.
Build a small tree in the workspace to navigate:
$ mkdir -p nav/sub
$ printf "secret\n" > nav/.hidden
$ printf "plain\n" > nav/plain.txt
$ ln -s nav navlink
All four commands are silent on success. The fourth makes navlink a
symbolic link to nav - used in a moment to show how the shell handles
paths through links. Enter the directory and confirm where you are:
$ cd nav
$ pwd
/home/john/debian-handbook/shell-basics/nav
pwd prints the working directory as the shell sees it. Now list the
directory twice, plainly and with -a:
$ ls
plain.txt
sub
$ ls -a
.
..
.hidden
plain.txt
sub
The difference is the dot. A file whose name begins with a dot is hidden:
ls omits it, and * does not match it, but it is an ordinary file in
every other respect. Shell and program configuration lives in such files -
~/.bash_history and ~/.ssh/known_hosts from earlier sections are both
dotfiles. The two entries . and .. are the directory itself and its
parent, which is what relative paths are made of. Move down and back with
them:
$ cd sub && pwd && cd .. && pwd
/home/john/debian-handbook/shell-basics/nav/sub
/home/john/debian-handbook/shell-basics/nav
cd with no argument returns to your home directory, from anywhere; cd -
returns to the previous working directory and prints it, leaving the old
location in $OLDPWD:
$ cd sub && cd - && echo "OLDPWD=$OLDPWD"
/home/john/debian-handbook/shell-basics/nav
OLDPWD=/home/john/debian-handbook/shell-basics/nav/sub
Return to the workspace and take the path through the symbolic link. The
shell remembers the path you typed, and can also report the physical location
after resolving links - pwd and pwd -P:
$ cd ../navlink
$ pwd
/home/john/debian-handbook/shell-basics/navlink
$ pwd -P
/home/john/debian-handbook/shell-basics/nav
The first answer is the logical working directory - the path as entered,
with navlink in it. The second resolves the link and reports where the
directory really is. Neither is wrong; scripts that compare paths should
resolve first, and realpath does that for any path without changing
directories:
$ realpath navlink/.hidden
/home/john/debian-handbook/shell-basics/nav/.hidden
cd form |
Destination |
|---|---|
cd dir |
The named directory, relative or absolute. |
cd |
Your home directory. |
cd ~user |
That user's home directory. |
cd - |
The previous working directory; printed, and kept in $OLDPWD. |
A path that does not exist is an error, not a new directory:
$ cd /no/such/dir
-bash: cd: /no/such/dir: No such file or directory
$ echo status=$?
status=1
cd failed with status 1 and the shell stayed where it was. And the quickest
way home from the very root of the tree:
$ cd / && cd && pwd
/home/john
3.3.3. Creating, Copying, Moving, and Removing Files
The daily operations on files are four commands, each with a destructive corner that deserves its exact warning. Return to the workspace root first:
$ cd ~/debian-handbook/shell-basics
Then set up a working directory for the section:
$ mkdir -p files/dir
$ printf "one\n" > files/a.txt
The touch command sets timestamps - here a fixed date, so that a copy's
behavior is visible:
$ touch -d "2020-01-02 03:04:05" files/a.txt
Enter the directory and make two copies, one with plain cp and one with
cp -a:
$ cd files
$ cp a.txt b.txt
$ cp -a a.txt c.txt
Both copies contain the same bytes, but their metadata differs - compare the modification times:
$ stat -c "%n %y" a.txt b.txt c.txt
a.txt 2020-01-02 03:04:05.000000000 +0000
b.txt 2026-11-18 08:16:52.313649097 +0000
c.txt 2020-01-02 03:04:05.000000000 +0000
b.txt got the current time: plain cp writes a new file with new
timestamps. c.txt, copied with -a (archive mode), kept the 2020 timestamp
because -a preserves modification times, ownership where permitted, and
mode. Rename a file with mv, which also moves it between directories when
the destination is a path:
$ mv b.txt renamed.txt && ls
a.txt
c.txt
dir
renamed.txt
cp option |
Effect |
|---|---|
-a |
Archive mode: preserve mode, timestamps, and ownership where permitted; recurse. |
-r |
Copy directories recursively. |
-i |
Prompt before overwriting an existing file. |
-p |
Preserve mode and timestamps (no recursion). |
source - |
Read standard input when the source is -. |
mv option |
Effect |
|---|---|
-i |
Prompt before overwriting. |
-n |
Never overwrite an existing destination. |
Copying onto an existing name replaces it silently - the destructive
corner of both cp and mv. The next commands create a second file and
then overwrite the preserved-time copy c.txt with its content:
$ printf "two\n" > b.txt
$ cp b.txt c.txt
$ head -n 1 c.txt
two
The 2020 timestamp is gone with the old content. Interactive use can make
the prompt routine: cp -i and mv -i ask before overwriting, and many
administrators alias rm to something safer rather than relying on
reflexes.
The errors are as informative as the successes. mkdir refuses to replace:
$ mkdir dir; echo "status=$?"
mkdir: cannot create directory ‘dir’: File exists
status=1
The message went to standard error and the status is 1. cp reports a
missing source and does not create the destination:
$ cp missing.txt copy.txt; echo "status=$?"
cp: cannot stat 'missing.txt': No such file or directory
status=1
rm on a name that does not exist is the same shape:
$ rm missing.txt; echo "status=$?"
rm: cannot remove 'missing.txt': No such file or directory
status=1
rm option |
Effect |
|---|---|
-r |
Remove directories and their contents recursively. |
-i |
Prompt for each file. |
-f |
Never prompt; nonexistent names are not an error. |
Create a directory with a file inside it, and then remove the whole tree.
rm -r deletes the directory and everything in it, with no undo and no
trash can. Check the path once before pressing Enter:
$ mkdir -p tree/leaf && touch tree/leaf/file
$ rm -r tree
$ test ! -e tree && echo removed
removed
test ! -e tree succeeded because the name no longer exists at all.
One more interaction between copying and privilege completes the picture.
Return to the workspace root and make a directory for the demonstration,
then use install, which copies a file and sets its attributes - the way
to place a root-owned file where an ordinary user can read it:
$ cd ~/debian-handbook/shell-basics
$ mkdir -p ownership
$ sudo install -m 644 -o root -g root /etc/hostname /home/john/debian-handbook/shell-basics/ownership/root-owned.txt
None of the three commands prints anything; install copied /etc/hostname
with mode 644 and root ownership into the directory. Now copy that
root-owned file as john, even with -a:
$ cp -a /home/john/debian-handbook/shell-basics/ownership/root-owned.txt /home/john/debian-handbook/shell-basics/ownership/copy.txt
$ stat -c "%U:%G" /home/john/debian-handbook/shell-basics/ownership/copy.txt
john:john
cp -a preserves ownership only when the copying user may assign it;
changing a file's owner to root requires root. The copy is john's, which is
why system backups, restores, and repackaging jobs are run as root when
ownership must survive.
3.3.4. Symbolic Links, Hard Links, and Inodes
The filesystem stores a file's identity in an inode: one structure holding the file's permissions, timestamps, ownership, size, and the location of its data. A directory entry - a name - is just a record pointing at an inode number. Two names can point at the same inode; that is a hard link. A symbolic link is a different kind of object: a small file whose content is a path, resolved fresh every time it is followed. Figure 3.5 shows both before and after a deletion.
Figure 3.5. Left: original.txt and hard.txt share one inode (link
count 2) while sym.txt stores the name original.txt. Right: after
rm original.txt the hard link still resolves to the data; the symbolic
link dangles.
Build the situation. From the workspace root, make a directory, a file, a hard link to it, and a symbolic link to it:
$ cd ~/debian-handbook/shell-basics
$ mkdir -p links && cd links
$ printf "alpha\n" > original.txt
$ ln original.txt hard.txt
$ ln -s original.txt sym.txt
ln with one source and one destination creates the hard link; -s makes a
symbolic link instead. None of them prints anything. Now compare the three
names with stat, which reports the inode number %i, the link count %h,
the file type %F, and the name %n:
$ stat -c "%i %h %F %n" original.txt hard.txt sym.txt
655605 2 regular file original.txt
655605 2 regular file hard.txt
655606 1 symbolic link sym.txt
original.txt and hard.txt have the same inode number and a link count
of 2: they are two names for one file, of equal standing - there is no
"original" left in the filesystem. sym.txt is a separate object (a
different inode, link count 1) of type symbolic link. ls -li shows the
same facts with the arrow notation:
$ ls -li
total 8
655605 -rw-rw-r-- 2 john john 6 Nov 18 08:17 hard.txt
655605 -rw-rw-r-- 2 john john 6 Nov 18 08:17 original.txt
655606 lrwxrwxrwx 1 john john 12 Nov 18 08:17 sym.txt -> original.txt
The stored target of a symbolic link can be read directly - readlink for
the stored text, readlink -f for the fully resolved path:
$ readlink sym.txt && readlink -f sym.txt
original.txt
/home/john/debian-handbook/shell-basics/links/original.txt
The stored text is exactly what was typed: a relative name, resolved against whatever directory contains the link at the time it is used.
Now delete the name original.txt. This is safe for the data - the inode's
link count simply drops to 1 - but it breaks the symbolic link:
$ rm original.txt
The hard link still opens the file, because the inode survives while any name references it:
$ cat hard.txt
alpha
The symbolic link does not, because the name it stored is gone. Asked by
its full path, cat reports the failure:
$ cat /home/john/debian-handbook/shell-basics/links/sym.txt
cat: /home/john/debian-handbook/shell-basics/links/sym.txt: No such file or directory
The message says "no such file" even though sym.txt plainly exists - the
error is about the target of the link, and that wording is the usual clue
that a dangling link is involved. A link can even be created deliberately
pointing at nothing; it stays broken until a file with that name appears:
$ ln -s missing.txt broken.txt && readlink broken.txt
missing.txt
Find the symbolic links under a tree, with their targets, as one command - useful when auditing what depends on what:
$ find /home/john/debian-handbook/shell-basics/links -type l -printf "%p -> %l\n" | sort
/home/john/debian-handbook/shell-basics/links/broken.txt -> missing.txt
/home/john/debian-handbook/shell-basics/links/sym.txt -> original.txt
| Hard link | Symbolic link | |
|---|---|---|
| What it is | Another directory entry for the same inode | A separate file storing a path |
| Survives deletion of the other name | Yes; data lives while any link exists | No; breaks when the target name disappears |
| May cross filesystems | No | Yes |
| May point at a directory | Normally not | Yes |
Shows in ls -l |
Indistinguishable from a copy, except by inode | -> and mode l |
Hard links explain ordinary behavior too: renaming a file (mv) within a
filesystem only rewrites one directory entry - which is why it is instant
even for huge files. Chapter 12 covers what inodes mean
for capacity planning and filesystem repair.