Foundations to Frontier
Tests
C. Programming, Tools and HPC › C4 Linux and Systems

The Shell, Scripting and File Systems

Research computing happens on Linux machines you reach through a shell: clusters, cloud VMs, robot computers, ground-station servers. This topic covers how the shell turns a line of text into processes (words, quoting, variables, globbing, pipes, redirection, exit codes), writing robust scripts (set -euo pipefail, quoting, shellcheck, find/xargs), the Unix file-system model (one tree, inodes, hard and symbolic links, permissions, mounts) and the storage layers of a cluster (home, scratch, parallel file systems) and why millions of small files are slow on them. In the exercises you implement shell tokenization, globbing and a pipeline of coreutils in Python and check them against shlex, then measure the small-files problem: block-size waste, tar overhead, per-file metadata latency and the speed-up from packing files into shards.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Talking to the computer in sentences

Instead of clicking icons, you can type short commands to a program called the shell: “list these files”, “find every line that says ERROR”, “count them”. The magic is that small commands can be chained with a pipe |, like an assembly line where each worker does one job and hands the result to the next. On the big computers scientists use, and on most robots, this is how almost everything is done.

Build a pipeline: which error happens most often?
cat flight.log
$ cat flight.log | grep ERROR | cut -d , -f 3 | sort | uniq -c | sort -rn | head -n 2
cat flight.log · the input file · 9 lines
2026-11-30T10:00,INFO,startup
2026-11-30T10:01,ERROR,imu_timeout
2026-11-30T10:02,WARN,battery_low
2026-11-30T10:03,ERROR,imu_timeout
2026-11-30T10:04,ERROR,gps_lost
2026-11-30T10:05,INFO,ok
2026-11-30T10:06,ERROR,imu_timeout
2026-11-30T10:07,ERROR,gps_lost
2026-11-30T10:08,ERROR,star_tracker
grep ERROR · keep matching lines · 6 lines
2026-11-30T10:01,ERROR,imu_timeout
2026-11-30T10:03,ERROR,imu_timeout
2026-11-30T10:04,ERROR,gps_lost
2026-11-30T10:06,ERROR,imu_timeout
2026-11-30T10:07,ERROR,gps_lost
2026-11-30T10:08,ERROR,star_tracker
cut -d , -f 3 · third comma-separated field · 6 lines
imu_timeout
imu_timeout
gps_lost
imu_timeout
gps_lost
star_tracker
sort · group identical lines together · 6 lines
gps_lost
gps_lost
imu_timeout
imu_timeout
imu_timeout
star_tracker
uniq -c · count runs of identical adjacent lines · 3 lines
      2 gps_lost
      3 imu_timeout
      1 star_tracker
sort -rn · numeric, largest first · 3 lines
      3 imu_timeout
      2 gps_lost
      1 star_tracker
head -n 2 · first two lines · 2 lines
      3 imu_timeout
      2 gps_lost
result: 3 imu_timeout / 2 gps_lost
Each program does one job and passes text on; the shell connects them with pipes, and they run concurrently, streaming line by line. The same one-liner works on a 50 GB log on a cluster login node, where loading the file into a notebook would not.
Why engineers care

Cluster jobs, flight-software builds, log triage after a test campaign and data transfers from ground stations are all driven from the shell. A five-command pipeline can answer “which fault occurred most often in last night’s 40 GB of telemetry logs?” in seconds, on the machine where the data already lives.

📝 Quick check (Level 0)
0/3 answered
  1. 1. What does the pipe | do?
  2. 2. The shell is…
  3. 3. In the demo, why is sort needed before uniq -c?

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Everyday commands

Task Command
where am I, what’s here pwd, ls -lh, tree -L 2
move around, create, copy, move, remove cd, mkdir -p, cp -r, mv, rm (no undo!)
look at files cat, less, head -n 20, tail -f run.log
search grep -rn "ERROR" logs/, find . -name "*.h5" -mtime -1
text processing cut, sort, uniq -c, wc -l, sed, awk
disk usage df -h (file systems), du -sh * (directories)
help man grep, grep --help

Redirection and exit codes

cmd > out.txt writes stdout to a file (overwriting), >> appends, 2> err.txt redirects stderr, 2>&1 merges stderr into stdout, < in.txt reads stdin from a file. Every command returns an exit status: 0 means success, anything else failure; $? holds the last one. a && b runs b only if a succeeded, a || b only if it failed.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Which redirect appends instead of overwriting?
  2. 2. An exit status of 0 means…
  3. 3. du -sh * shows…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

How the shell reads a line

Before running anything, the shell transforms the line in a fixed order:

  1. Splitting into words on whitespace, respecting quotes: 'single quotes' are completely literal; "double quotes" keep spaces but still expand $VAR; a backslash escapes one character.
  2. Expansion: $VAR and ${VAR}, command substitution $(cmd), arithmetic $((1+2)).
  3. Word splitting of unquoted expansion results (the reason to write "$file", not $file, for names with spaces).
  4. Globbing: unquoted *, ?, [abc] are replaced by matching file names, sorted; if nothing matches, bash leaves the pattern as is.
  5. Pipes and redirections are set up, and each command runs as a separate process.

Robust scripts

#!/usr/bin/env bash
set -euo pipefail                 # stop on errors, on unset variables, and on failures inside pipes
IFS=$'\n\t'
out_dir="${1:?usage: $0 OUT_DIR}"  # required argument with an error message
mkdir -p "$out_dir"
for f in data/*.csv; do           # quote every expansion
  python process.py "$f" > "$out_dir/$(basename "$f" .csv).json"
done
find logs -name "*.log" -print0 | xargs -0 grep -l "FATAL" || true

Run shellcheck on every script; it catches unquoted variables, useless cats and many subtle bugs. Once logic grows past a screen, switch to Python.

🐍 Exercise 1: how the shell reads a command line
Implement a tokenizer with single quotes, double quotes (with $VAR and ${VAR} expansion), backslash escapes and pipe separators, and check it against Python's shlex; glob expansion with fnmatch (quoted words and non-matches stay literal); a small pipeline runner with cat, grep (-v, -i, -c), cut (-d, -f), sort (-n, -r), uniq (-c), wc -l, head (-n) and echo; a path normalizer for '.', '..' and '//'; and a permission-bits formatter.
📝 Quick check (Level 2)
0/3 answered
  1. 1. echo '$HOME' prints…
  2. 2. Why write "$file" with quotes?
  3. 3. set -o pipefail makes a pipeline fail when…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

The file-system model

Everything is one tree rooted at /; disks, network shares and parallel file systems are mounted at directories. A file’s data and metadata (owner, mode, size, timestamps, block pointers) live in an inode; a directory is a table from names to inode numbers. A hard link is a second name for the same inode; a symbolic link is a small file containing a path (it can dangle). Permissions are rwx for user, group and others (chmod 750, chown); for directories x means “may enter”. Space is allocated in blocks (typically 4 KiB), so a 1-byte file still occupies a block.

Storage on a cluster

  • Home ($HOME): small, backed up, often slow (NFS). Code and configs.
  • Project/work: shared, quota-limited, sometimes backed up.
  • Scratch: large, fast parallel file system (Lustre, GPFS/Spectrum Scale, BeeGFS), purged after some days, not backed up. Job data.
  • Node-local SSD ($TMPDIR): fastest, wiped after the job.

Parallel file systems stream huge files at many GB/s, but every open, stat and close is a round trip to a metadata server. Millions of small files therefore overload them; pack datasets into shards (tar/WebDataset, HDF5, Zarr with large chunks, C7) and stage to local SSD.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A symbolic link stores…
  2. 2. Where should a job write 2 TB of intermediate files on a cluster?
  3. 3. Why are millions of small files slow on Lustre?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Shell and storage, measured

Exercise 1. The tokenizer reproduces shlex.split on quoting cases including 'one two', a\ b and mixed quotes, expands $RUN and ${HOME} but not inside single quotes, and globs *.csv while leaving '*.csv' and non-matching patterns literal. On an 8-line flight log, cat | grep ERROR | cut -d , -f 3 | sort | uniq -c | sort -rn | head -n 2 gives 3 imu_timeout and 2 gps_lost. Without the first sort, uniq merges nothing (8 lines stay 8; with sort there are 5 distinct messages).

Exercise 2: the small-files problem. 200 000 files with lognormal sizes (median 48 KiB, 24.5 GiB in total) under three illustrative storage profiles (per-file metadata cost and streaming bandwidth: NVMe 20 µs and 3 GiB/s, NFS 1.5 ms and 0.5 GiB/s, parallel FS 1 ms and 5 GiB/s):

Quantity Value
extra space with 4 KiB / 64 KiB / 1 MiB blocks 1.6% / 29% / 716%
tar overhead (512-byte headers and padding) 0.58%
read time, parallel FS: file by file → 99 shards of 256 MiB 205 s → 5.0 s
read time, NFS 349 s → 49 s
read time, local NVMe 12.2 s → 8.2 s
file size for half the streaming bandwidth (t_meta·bw) 63 KiB / 786 KiB / 5.1 MiB

On the parallel file system, metadata accounts for 98% of the file-by-file time; sharding removes it (41× faster), while the local SSD barely notices. A file system with 1 MiB blocks would need 8.2× the space for this dataset. These profiles are assumptions for illustration: measure your own system with a timed read loop.

🐍 Exercise 2 (research): the small-files problem
Generate a lognormal file-size dataset; compute block-allocation waste for 4 KiB, 64 KiB and 1 MiB blocks and tar's overhead; model read time as files × metadata latency + bytes / bandwidth for three storage profiles; pack files into 256 MiB shards and compare; compute the file size at which half the streaming bandwidth is reached.

The shell in AI, robotics and aerospace

  • Cluster workflows: job scripts, data staging to node-local SSD, log triage with grep and awk.
  • Robots: ROS 2 launch and bag tools are shell commands; field debugging happens over SSH in a terminal (C4.2).
  • Ground segments: cron-driven shell scripts move telemetry files, verify checksums (sha256sum -c) and archive them.
  • ML datasets: WebDataset tar shards and sequential reads exist because of the small-files problem.

References

  1. W. Shotts (2019). The Linux Command Line, 2nd ed. No Starch Press.
  2. IEEE Std 1003.1 (POSIX), Shell Command Language (pubs.opengroup.org).
  3. GNU Bash Reference Manual (gnu.org/software/bash/manual).
  4. M. K. McKusick, G. V. Neville-Neil, R. N. M. Watson (2014). The Design and Implementation of the FreeBSD Operating System, 2nd ed., file-system chapters. Addison-Wesley.
  5. P. Braam (2019). “The Lustre storage architecture.” arXiv:1903.01955.