Skip to content

← Week 2 - Filesystem and files

Week 2 - Filesystem and files: notes

Status: Done

The first directory in the filesystem is called the root directory and contains files and subdirectories

what is the difference /$ and ~$ ?

In the bash prompt, the part after the colon shows your current directory:

  • your-username@hostname:/$ — you're in /, the root of the filesystem (the top-level directory that contains everything else). The $ means a normal (non-root) user.
  • your-username@hostname:~$ — you're in ~, your home directory, which is /home/your-username.

Some Useful Shortcuts

Useful ways to quickly change the current working directory:

Shortcut Description
cd Changes the working directory to your home directory.
cd - Changes the working directory to the previous working directory.
cd ~user_name Changes the working directory to the home directory of user_name. For example, cd ~bob changes to the home directory of user bob.

Exercise 1

Study the layout: /etc, /var, /usr, /home, /tmp, /proc. Explore each with ls and find.

Checklist (session 1, about 1.5 h; see the plan):

  • Write the directory table (below).
  • /etc: ls /etc | head -30, cat /etc/os-release, cat /etc/hostname, head /etc/passwd; count configs with find /etc -name "*.conf" 2>/dev/null | wc -l.
  • /var: ls /var/log, du -sh /var/* 2>/dev/null; find out what grows here and what /var/lib holds.
  • /usr: ls /usr/bin | wc -l, ls -l /bin /lib /sbin (confirm the symlinks), which dotnet git.
  • /home and /tmp: ls -la ~ (dotfiles such as .bashrc, .profile, .ssh), ls -ld /tmp (sticky bit).
  • /proc: head /proc/cpuinfo, head /proc/meminfo, cat /proc/$$/cmdline, ls -l /proc/$$/fd.
  • WSL: ls /mnt/c, findmnt /mnt/c, mount | head; compare with the week 1 filesystem performance notes.

Directories of Linux Systems

Directory Contents
/ Root directory; the top of the filesystem hierarchy.
/usr/bin Essential command binaries.
/boot Boot loader and kernel files.
/bin Essential command binaries; symlink to /usr/bin on modern systems.
/dev Device nodes (disks, terminals, /dev/null).
/etc System-wide configuration files.
/home Home directories of regular users.
/lib, /lib64 Shared libraries; symlinks to /usr/lib and /usr/lib64.
/lost+found Files recovered after a filesystem check.
/media Mount points for removable media.
/mnt Temporary mount points; WSL mounts Windows drives here (/mnt/c).
/opt Optional third-party software.
/proc Virtual filesystem exposing processes and kernel info.
/root Home directory of the root user.
/run Runtime data since last boot (PID files, sockets).
/sbin System administration binaries; symlink to /usr/sbin.
/snap Installed snap packages.
/srv Data served by this system (web, FTP).
/sys Virtual filesystem exposing devices and drivers.
/tmp Temporary files, world-writable (sticky bit, drwxrwxrwt).
/usr Installed programs, libraries and shared data.
/var Variable data: logs, caches, spool files.

/var/log: system and application logs

Path Contents
syslog General system messages; the first place to look.
auth.log Logins, sudo use, SSH attempts, authentication failures.
kern.log, dmesg Kernel messages and hardware/driver output from boot.
boot.log Output of services at boot time.
apt/ history.log (what was installed or removed) and term.log.
dpkg.log Low-level package install, upgrade and remove records.
unattended-upgrades/ Automatic security update logs.
journal/ The systemd journal (binary); read with journalctl.
wtmp, btmp, lastlog Binary login records; read with last, sudo lastb and lastlog.
apache2/, nginx/, postgresql/ Appear only when those packages are installed.
*.1, *.2.gz Older logs rotated by logrotate; read .gz files with zcat or zless.
ls -lh /var/log                 # list the files with sizes
sudo tail -f /var/log/syslog    # follow a log live
sudo less /var/log/auth.log     # page through a log
journalctl -b                   # logs from the current boot
journalctl -u ssh               # logs for one service
sudo du -sh /var/log/*          # what is using space
  • Many logs are readable only by root or the adm group, so use sudo.
  • Modern Ubuntu keeps much of its data in the systemd journal, so journalctl often shows more than the text files.
  • On WSL2 there is no full systemd boot by default, so some files (such as auth.log or kern.log) may be missing or sparse.

which: locate a command

which prints the full path of the executable that would run, searching $PATH in order.

which git          # /usr/bin/git
which -a python3   # every match in $PATH, not just the first
type -a git        # also shows builtins, aliases and functions
readlink -f $(which dotnet)   # follow symlinks to the real file
  • which misses builtins and aliases; use type for those, or command -v in scripts.
  • On WSL, PATH includes Windows directories, so /mnt/c/... in the output means the Windows version runs.

Exercise 2

Practice find, less, head, tail -f, du, df, ln (hard and symbolic links), tree.

Checklist (sessions 2 and 3, about 2.5 h; see the plan):

Viewing files:

  • less: /pattern, n/N, g/G, F (follow), q.
  • head -n 5, tail -n 5, wc -l, file.

Disk usage and tree:

  • df -h and df -hT; note the 9p/drvfs mounts for Windows drives.
  • du -sh, du -h --max-depth=1 | sort -h, du -ah | sort -rh | head.
  • Explain why df (per filesystem) and du (per directory tree) can disagree.

=> df asks "how full is the disk?", du asks "what's using the space?" (the next checklist item).

Links lab in /tmp/links-lab:

  • Create a.txt, ln a.txt hard.txt, ln -s a.txt soft.txt.
  • ls -li: compare inode numbers (hard link shares one, symlink does not).
  • Delete a.txt: hard link still works, symlink is dangling; find it with find . -xtype l.
  • stat each file and watch the link count.
  • Try a hard link to a directory and across filesystems; note both errors and why symlinks have no such limits.
  • Real-world symlinks: ls -l /etc/alternatives, readlink -f $(which dotnet).

find in depth (do it under ~, not /mnt/c):

  • Name and type: -name, -iname, -type f|d|l.
  • Size and time: -size +10M, -mtime -1, -mmin -30.
  • Owner and permissions: -user, -perm.
  • Actions: -exec cmd {} \; vs -exec cmd {} +; run without -delete first.
  • Pruning: find . -name bin -prune -o -name "*.cs" -print.
  • Hide errors with 2>/dev/null and note why it goes at the end.
  • Challenge: the 10 largest files under ~/src.
  • Challenge: .csproj files modified this week.
  • Challenge: all empty directories in a repo.
  • Compare find with locate (sudo updatedb) and fd.

head: first lines of a file

head prints the first 10 lines of a file or piped input.

head -n 5 /etc/passwd     # first 5 lines
head -n -3 file.txt       # everything except the last 3 lines
head -c 100 file.bin      # first 100 bytes
ls /etc | head -30        # first 30 entries of a command's output
head -n 20 file | tail -n 5   # lines 16-20
  • tail is the counterpart for the end of a file.
  • It stops reading once it has enough, so it is fast on huge files.

du: disk usage

du (disk usage) reports how much disk space files and directories take up, walking the directory tree recursively.

du -sh ~/src                      # one total for a directory, human-readable
du -h --max-depth=1               # size of each immediate subdirectory
du -h --max-depth=1 | sort -h     # same, sorted smallest to largest
du -ah | sort -rh | head          # 10 biggest files and directories under here
du -sh /var/* 2>/dev/null         # sizes of everything in /var, hiding permission errors
Option Meaning
-h Human-readable sizes (K, M, G).
-s Summary: only the total for each argument.
-a Include files, not just directories.
-c Add a grand total line at the end.
--max-depth=N, -d N Limit how deep it descends.
-x Stay on one filesystem (skips mounts such as /mnt/c on WSL).
--apparent-size Show file content size instead of blocks used on disk.

du versus df:

  • du adds up the files under a directory tree: "what is using the space?"
  • df asks the filesystem how much is used and free: "how full is the disk?"
  • They can disagree: a deleted file that a running process still holds open counts in df but is invisible to du. Filesystem overhead and mounts that du does not cross also cause differences.
lrwxrwxrwx 1 root root 11 2025-08-11 07:34 libc.so.6 -> libc-2.6.so

A symbolic link (symlink) is a special file that points to another file or directory by path, so opening the link opens the target.

Part Meaning
l File type: symbolic link
rwxrwxrwx Permissions of a symlink are always shown as 777; the target's permissions apply
libc.so.6 Name of the link
-> Points to
libc-2.6.so Target file the link resolves to

Programs ask for libc.so.6; the system can swap the real library (libc-2.6.so) for a newer one by repointing the link.

A listing (for example in /usr/lib) can show an entry that starts with l and has two names. That is a symbolic link (also called a soft link or symlink). On Unix-like systems a file can be referenced by multiple names.

The scenario:

  • A program needs a shared resource called foo, but foo changes version often.
  • Putting the version in the filename (foo-2.6) lets an administrator see which version is installed.
  • Renaming the file on every upgrade would mean updating every program that uses it.

The solution:

  • Install foo-2.6 and create a symlink foo that points to it. Programs open foo and get foo-2.6.
  • To upgrade, add foo-2.7, delete the foo link and create a new foo link that points to foo-2.7.
  • Both versions stay on disk. If foo-2.7 has a bug, delete the link and point it back to foo-2.6.
ln -s foo-2.6 foo      # foo -> foo-2.6
ln -sfn foo-2.7 foo    # repoint the link to the new version (-f replace, -n treat an existing link as a file)
ls -l foo              # lrwxrwxrwx ... foo -> foo-2.7

The libc.so.6 -> libc-2.6.so example above is exactly this pattern on a Fedora system: programs looking for libc.so.6 get libc-2.6.so.

A hard link also gives a file more than one name, but in a different way than a symlink. The differences between the two types are covered with ln in the links lab above.

File permissions: what mode 600 means

A mode like 600 is a permission setting in octal. Each digit is a sum of permission bits: read = 4, write = 2, execute = 1.

Digit Applies to Value Meaning
6 Owner (user) 4 + 2 read and write
0 Group 0 no access
0 Others 0 no access

So 600 means only the owner can read and write the file, and nobody else has any access. In ls -l it shows as -rw-------.

chmod 600 secret.txt         # set the mode
ls -l secret.txt             # -rw------- 1 you you ... secret.txt
stat -c '%a %A' secret.txt   # 600 -rw-------
  • It is the standard mode for private files. SSH refuses a private key (such as ~/.ssh/id_ed25519) that is not 600: "UNPROTECTED PRIVATE KEY FILE".
  • The usual private mode for a directory is 700, because the execute bit is needed to enter it.
  • Root can read the file regardless of its mode.

Exercise 3

Run a .NET app and follow its console output with tail -f while sending requests.

Checklist (session 4, about 1 h; see the plan):

  • Create or reuse an app: dotnet new webapi -o ~/src/tail-demo.
  • Terminal 1: dotnet run > /tmp/app.log 2>&1.
  • Terminal 2: tail -f /tmp/app.log.
  • Terminal 3: curl -i localhost:<port>/weatherforecast, plus a request to a missing route (404).
  • Try tail -n 50 -f, tail -F, grep --line-buffered on the live stream, and less +F.
  • Add logger.LogInformation(...) in code and find the line in the output.
  • Note what tail -f does when the file is deleted or truncated.

Exercise 4

Bandit levels 6-12.

Checklist (session 5; see the plan). Detailed notes go in bandit.md: approach and concept only, never passwords.

  • Level 6 → 7
  • Level 7 → 8
  • Level 8 → 9
  • Level 9 → 10
  • Level 10 → 11
  • Level 11 → 12
  • Level 12 → 13
  • Time-box each level to 30 minutes before looking for hints.
  • Update the progress table in overthewire/README.md.

Deliverable

A personal cheat sheet (Markdown) in a git repo; add to it all 6 months.

Started: cheatsheet.md. It is seeded with the commands from Week 1 and has a section ready for each later topic.

Week 2 commands added: head, tail, less +F, du, df, ln, tree, redirection and tee.

Checklist:

  • Fill the Viewing files, Disk usage, Links, Finding files and Tricky file names sections of the cheatsheet.
  • Add one "what I'd forget" example per command; only commands I have run.

Wrap-up

  • Answer the self-check questions.
  • Tick the exercises and the Done box in week-02-filesystem-and-files.md.
  • Set Status at the top of this file to Done.
  • Run the markdownlint check (see the README).
  • Commit on a branch like docs/week-02-... and open a PR.