Linux Filesystem Navigation and Disk Usage


  • Description: Day-to-day commands for moving around the filesystem (cd, pwd, ls, tree, mkdir) and inspecting space (df for mounted filesystems, du for file/directory size). Includes the common df-vs-du confusion and useful flags for both.
  • My Notion Note ID: K2B-3-2
  • Created: 2020-06-03
  • Updated: 2026-05-19
  • License: Reuse is very welcome. Please credit Yu Zhang and link back to the original on yuzhang.io

Table of Contents


1. Navigating with cd and pwd

  • cd [PATH] — change working directory.
    • cd (no arg) → $HOME.
    • cd - → previous directory ($OLDPWD). Toggles like Alt-Tab.
    • cd ~user → that user's home.
  • Path shorthands:
    • . current dir, .. parent, ../.. grandparent.
    • ~ $HOME, ~- $OLDPWD.
    • Leading / → absolute path; otherwise relative.
  • pwd — print working directory.
    • pwd -P resolves symlinks to physical path.
    • pwd -L (default) keeps the logical path the shell remembers.

2. Listing with ls

  • ls [OPTION]... [FILE]... — list directory contents. With no FILE, lists ..
  • Common flags (combinable, e.g. ls -lahS):
Flag Meaning
-a, --all include hidden (dot) files
-A like -a but skip . and ..
-l long format (perms, owner, size, mtime)
-h, --human-readable sizes in K/M/G (with -l / -s)
-S sort by size, largest first
-t sort by mtime, newest first
-r reverse the sort
-d list directory itself, not its contents
-R recurse into subdirectories
-1 one entry per line (script-friendly)
--color=auto colorize by file type (usually aliased on by default)
  • Colors carry meaning — by default: directory (blue), executable (green), symlink (cyan), broken symlink (red), archive (magenta). Tunable via LS_COLORS.
  • Don't parse ls output in scripts. Filenames can contain spaces, newlines, and quotes. Use globs or find -print0 | xargs -0 instead.

3. Visualizing with tree

  • tree [OPTION]... [DIR]... — recursive directory listing in ASCII tree form. Not installed by default on some distros (apt install tree).
  • Useful flags:
    • -L <N> — limit recursion depth to N levels.
    • -d — directories only.
    • -a — include hidden files.
    • -P 'pattern' — show only files matching glob.
    • -I 'pattern' — ignore files matching glob (e.g. tree -I 'node_modules|.git').
    • --gitignore — respect .gitignore rules.
    • -s, -h — show size (bytes / human-readable).
    • -f — full path prefix on each entry.

4. Creating with mkdir

  • mkdir [OPTION]... DIRECTORY...
  • Flags:
    • -p, --parents — create intermediate dirs and don't error if target already exists. Idiomatic for scripts.
    • -m MODE — set permissions at creation time (e.g. mkdir -m 700 secret).
    • -v, --verbose — print each dir as it's created.
  • Pitfall: mkdir parent/child fails if parent doesn't exist. Always reach for -p unless you specifically need the failure.

5. df — Filesystem-Level Space

  • df [OPTION]... [FILE]... — report how much of each mounted filesystem is used. Reports per-mountpoint, not per-file.
  • Common flags:
Flag Meaning
-h, --human-readable sizes in K/M/G (powers of 1024)
-H, --si powers of 1000
-T, --print-type show filesystem type (ext4, tmpfs, xfs)
-t TYPE show only filesystems of TYPE
-x TYPE exclude filesystems of TYPE
-i show inode usage instead of bytes
-a, --all include pseudo-fs (proc, sysfs, …) usually hidden
  • Read the output as:
    Filesystem    1K-blocks      Used Available Use% Mounted on
    /dev/sda2     480000000 200000000 280000000  42% /
    
  • df -h . — space on the filesystem that holds the current dir. Frequent.
  • df -ih — inodes are exhausted before bytes more often than you'd expect (many tiny files); always check both.

6. du — File and Directory Size

  • du [OPTION]... [FILE]... — estimate disk usage of files and directories (allocated blocks, not logical size).
  • By default: recurses, prints subtotals for each directory.
  • Common flags:
Flag Meaning
-h, --human-readable K/M/G (powers of 1024)
-a, --all include files, not just directories
-s, --summarize show only the total for each argument
-c, --total print a grand total at the end
-d N, --max-depth=N only show entries up to depth N
-x, --one-file-system don't cross mount points
-t SIZE, --threshold=SIZE positive: ≥ SIZE; negative: ≤ SIZE
--exclude=PATTERN skip matching paths (--exclude='*.o')
--apparent-size logical file size, not allocated blocks
  • Common recipes:
    • du -sh * → one summary line per item in the current dir.
    • du -h -d 1 . → first-level subdirs only.
    • du -ahd 1 . | sort -rh | head → top 10 biggest entries.
  • Interactive alternative: ncdu — curses-based, navigable, faster for "where did my disk go?" exploration.

7. df vs du

df du
Source filesystem superblock walks directory tree
Granularity per mounted filesystem per file / per directory
Speed instant slow on big trees
Sees deleted-but-open files yes (counts as used) no (file is unlinked)
Sees other filesystems yes no, with -x
  • Pitfall — df says full but du says small. Usually a deleted file still held open by a process. Find it with lsof +L1 (or lsof -nP \| grep deleted). Restart the process or truncate the FD to reclaim space.
  • Pitfall — du totals don't match df used. Bind mounts, snapshots, reserved root blocks (~5% on ext4), and du -x boundaries all cause divergence.

8. References

  • man cd, man ls, man df, man du, man mkdir, man tree
  • GNU coreutils manual — info coreutils → "Disk usage" node
  • ncduhttps://dev.yorhel.nl/ncdu