Compendium Scriptorum

Compendium Scriptorum#

A map of every command you have met and where to find it again — not a list to memorise. Nobody holds all of this in their head; the skill the course keeps returning to is looking things up, and this page is where you look. Your browser’s find (Ctrl/Cmd+F) is the search box; each command links back to the notebook where it was introduced.

Part I

Command

Job

Workhorse flags

Introduced in

cat

print a file’s contents straight to the screen.

-n number every line

3 Looking at and handling files

cd

change the working directory — move around the tree.

<dir> go into a directory (by absolute or relative path)
~  /  cd go to your home directory (bare `cd` does this too)
.. go up to the parent directory
- go back to the previous directory you were in
/abs/path go to an absolute location, counted from the root `/`

2 Navigating the filesystem

clear

wipe the visible screen, giving you a clean prompt.

1 The shell and the terminal

cp

copy a file (or, with -r, a directory).

-r recursive — copy a directory and everything inside it
-i prompt before overwriting an existing file
-v verbose — print each file as it is copied

3 Looking at and handling files

date

print the current date and time.

+FORMAT print using a format string — `%F` is the date as `YYYY-MM-DD`, `%T` the time as `HH:MM:SS`

1 The shell and the terminal

echo

print its arguments back to standard output.

-n do not add the trailing newline
-e interpret backslash escapes such as `\n` (newline) and `\t` (tab)

1 The shell and the terminal

file

identify what kind of file something is, before you open it.

3 Looking at and handling files

head

show the first lines of a file.

-n N show the first N lines (default 10)
-c N show the first N bytes instead of lines

3 Looking at and handling files

history

list the commands you have run, newest last, each with a number.

-c clear the history list

1 The shell and the terminal

less

page through a file one screen at a time — the pager. (Its “flags” are keys you press while it is open.)

Space / b page down / page up
/pattern n / N search forward, then jump to the next / previous match
g / G jump to the top / the bottom
q quit

3 Looking at and handling files

ls

list directory contents.

-l long format: permissions, size, owner, timestamp
-a include hidden dotfiles
-h human-readable sizes (with `-l`)
-t sort by modification time, newest first
-R recurse into subdirectories

2 Navigating the filesystem

man

open the full manual page for a command.

-k word search every manual’s summary line for a keyword (same as `apropos`) — for when you don’t know the command’s name yet

1 The shell and the terminal

mkdir

make a new directory.

-p create parent directories as needed, and do not error if it already exists

3 Looking at and handling files

mv

move a file — or rename it (same command: renaming is just moving within a directory).

-i prompt before overwriting an existing file
-v verbose — print what was moved

3 Looking at and handling files

pwd

print the working directory: where you currently are in the filesystem.

1 The shell and the terminal

rm

remove (delete) a file. Permanently.

-r recursive — delete a directory and everything in it
-i prompt before each removal (a good habit)
-f force — never prompt, ignore missing files (dangerous; see the admonition)

3 Looking at and handling files

rmdir

remove an empty directory — and only an empty one.

3 Looking at and handling files

tail

show the last lines of a file.

-n N show the last N lines (default 10)
-c N show the last N bytes instead of lines
-f follow — keep printing new lines as the file grows (watch a running job’s log live); stop with Ctrl+C

3 Looking at and handling files

tldr

show a few practical, copy-paste examples for a command, skipping the exhaustive manual.

1 The shell and the terminal

touch

create an empty file, or update an existing file’s timestamp.

3 Looking at and handling files

tree

show a directory and everything beneath it as an indented tree.

-L n descend at most n levels (keep deep trees readable)
-d show directories only, not files

2 Navigating the filesystem

whoami

print the username the shell is running as.

1 The shell and the terminal

Part II

Command

Job

Workhorse flags

Introduced in

awk

split each line into fields and act on them — print columns, filter by condition, and compute (sum, mean, min/max). The field-aware, can-do-arithmetic tool cut could not be.

-F C set the field separator to C (the default is any run of whitespace — which is how it beats cut)
-v var=val pass a shell value into the program as an awk variable

8 awk and sed

cut

pick out columns from each line — by delimited field, or by character position.

-d C use C as the field delimiter (default is TAB)
-f N keep field N (or a list/range like `1,3` or `2-4`); use with `-d`
-c N keep character positions N instead of fields (e.g. `1-6`)

7 The stream toolkit

find

search a directory tree for files matching criteria — name, type, size, time — and optionally act on each one.

-name "PAT"  /  -iname match the filename against a quoted glob (`-iname` ignores case)
-type f  /  -type d match only files / only directories
-maxdepth N descend at most N levels deep
-size +Nk  /  -mtime -N by size (larger than N kB) or age (changed within N days)
-exec CMD {} + run CMD on the matches — `{}` is each path, `+` batches them into few calls
-print  /  -delete print the matches (the safe default) / delete them (dangerous — see the admonition)

5 Selecting files: globbing, brace expansion, and find

grep

print the lines of a file (or of its input) that match a pattern — literal text, or a regular expression.

-i match case-insensitively
-v invert — print the lines that do NOT match
-n prefix each match with its line number
-c print only a count of matching lines
-r recurse through a directory tree
-l print only the names of files that contain a match
-o print only the matched part of each line, not the whole line
-E  /  -F `-E` extended regex (the sane quantifiers/grouping); `-F` fixed string (the pattern is taken literally)

6 grep and regular expressions

sed

edit a stream line by line — substitute, delete, or selectively print — like grep that can also change what it matches.

-E use Extended regex (the same ERE as `grep -E`); sed otherwise defaults to Basic regex
-n suppress the automatic printing of every line; pair with the `p` command to print only what you choose
-i  /  -i.bak edit the file IN PLACE; `-i.bak` keeps a backup of the original first (see the admonition)

8 awk and sed

sort

order the lines of its input.

-n numeric order (treat the field as a number, not text)
-r reverse the order
-k N sort by field N (with `-t` to set the separator)
-t C use C as the field separator for `-k`
-u drop duplicate lines, keeping one of each

7 The stream toolkit

tee

copy what flows in to a file AND pass it straight through, so you can save a mid-pipeline result without breaking the pipeline.

-a append to the file instead of overwriting it

4 Redirection and pipes

tr

translate, delete, or squeeze characters in a stream (it reads standard input, so pipe into it).

SET1 SET2 translate each character of SET1 to the matching one of SET2 (e.g. `’a-z’ ‘A-Z’` upper-cases)
-d SET delete every character in SET (e.g. `-d ‘\r’` strips carriage returns)
-s SET squeeze runs of a SET character down to one (e.g. `-s ‘ ‘` collapses spaces)

7 The stream toolkit

uniq

collapse adjacent duplicate lines, and optionally count them.

-c prefix each line with the number of times it occurred
-d print only the lines that were duplicated
-u print only the lines that were never repeated

7 The stream toolkit

wc

count the lines, words, and bytes of its input.

-l count lines
-w count words
-c count bytes

7 The stream toolkit

xargs

read a list of items on standard input and turn them into arguments for a command — the bridge from a pipeline that produces names to a command that acts on them.

-0 items are NUL-separated, not whitespace — pair with `find -print0` to survive spaces and newlines in names
-n N use at most N items per command line
-I {} place each item where `{}` appears, instead of appending it at the end

5 Selecting files: globbing, brace expansion, and find

Part III

Command

Job

Workhorse flags

Introduced in

chmod

change a file’s permissions — who may read, write, and execute it.

+x  /  u+x  /  go-w  /  a+r symbolic: add (`+`) or remove (`-`) a permission for user (`u`) / group (`g`) / others (`o`) / all (`a`) — `+x` is the one you reach for, it makes a file executable
755  /  644  /  700 octal: set all three triads at once — `755` = `rwx` for you and `r-x` for everyone else, `644` = read/write for you and read-only for others, `700` = full access for you and nothing for anyone else
-R recurse into a directory tree, applying the change to everything inside (powerful — and easy to misfire; see the admonition)

11 Permissions and execution

nano

open the nano editor — modeless, with its key shortcuts shown along the bottom of the screen.

^O write the file out (save) — the `^` means the Ctrl key
^X exit (it offers to save first)
^W search (“where is”)

9 Editing in the terminal: Vim

printf

print formatted output from a template — the dependable, portable way a script reports its results.

%s insert a string argument as-is (e.g. a name or a path)
%d  /  %.3f an integer / a float to 3 decimals — `%.Nf` sets the precision, `%-8s` / `%8.3f` set a field width
\n  /  \t a newline / a tab in the template — printf adds NO trailing newline of its own (unlike echo), so you write `\n` yourself

12 Writing scripts

seq

print a sequence of numbers — for counting, and for driving loops and sweeps.

N print 1 up to N (`seq 5` → 1 2 3 4 5)
FIRST LAST count from FIRST to LAST (`seq 2 6` → 2 3 4 5 6)
FIRST STEP LAST count in steps of STEP (`seq 0 2 10` → the even numbers)
-w equal width: zero-pad so the numbers line up (`seq -w 8 10` → 08 09 10)

13 Control flow

time

measure how long a command takes — real (wall-clock), user, and system time — by prefixing it.

time CMD run CMD, then report its timing — `real` is the wall-clock time you actually waited for
time { …; }  /  time pipe | line as a shell keyword it can also time a whole pipeline, group, or loop, not just one program

13 Control flow

type

tell you what a name actually runs — a shell builtin, a function, an alias, or the path to a program — by searching exactly as the shell would.

-a show ALL matches, not just the first one the shell would use
-t print only the kind (`builtin`, `file`, `alias`, `function`) — handy inside scripts

11 Permissions and execution

vim

open the Vim editor — the modal editor that is on every cluster, and the one git, crontab, and friends drop you into whether you asked or not.

<file> open (or create) a file for editing
+N <file> open with the cursor already on line N
-R <file> open read-only (the `view` command does the same) so you cannot change it by accident

9 Editing in the terminal: Vim

Part IV

Command

Job

Workhorse flags

Introduced in

alias

give a short name to a longer command — typed shorthand for interactive use.

alias ll='ls -lh' define an alias (keep your favourites in `.bashrc`)
alias list every alias currently defined
unalias NAME remove an alias

14 Environment and modules

df

report free and used space on the mounted filesystems — how full the disk (or your cluster quota) is.

-h human-readable sizes
df -h . show the filesystem holding the current directory

15 Remote work and moving data

du

report how much disk space files and directories use — for finding what is filling your quota.

-s summary: one total per argument, not every file within
-h human-readable sizes (K, M, G)
-sh * the everyday combo: a readable total for each item here

15 Remote work and moving data

export

promote a shell variable to an ENVIRONMENT variable, so every child process and script inherits it.

export NAME=value set a variable AND mark it for inheritance, in one step
export NAME mark an already-set variable for inheritance
-p list every exported variable currently in the environment

14 Environment and modules

gzip

compress a single file in place (and gunzip to restore it) — the compression behind tar’s -z.

file compress to `file.gz`, removing the original
-d  /  gunzip decompress (`gunzip file.gz` is the same as `gzip -d`)
-k keep the original alongside the compressed copy
-9  /  -1 best compression (slow) / fastest (less compression)

15 Remote work and moving data

kill

send a signal to a process — most often to stop one — by its PID.

kill PID politely ask it to terminate (SIGTERM — it can clean up first)
kill -9 PID force-kill (SIGKILL — immediate, no cleanup; the last resort)
kill %1 refer to a background job by its `jobs` number instead of its PID

15 Remote work and moving data

ln

create a link — most usefully a symbolic link, a lightweight pointer from one path to another.

-s target link create a SYMBOLIC link named `link` pointing at `target`
-sf force — replace an existing link of that name

15 Remote work and moving data

module

load and unload software on a cluster — adding a chosen version of a tool to your environment on demand.

module avail list the software (and versions) available to load
module load NAME/VER add a module to your environment (puts it on `PATH`, sets its variables)
module list show what you currently have loaded
module purge unload everything — back to a clean environment
module swap A B  /  module unload NAME replace one module with another / drop a single one

14 Environment and modules

nohup

run a command immune to hangups, so it keeps going after you log out.

nohup CMD & run CMD in the background, ignoring the `SIGHUP` sent at logout

15 Remote work and moving data

printenv

print the environment — the exported variables the shell passes down to its children.

printenv list the whole environment, one `NAME=value` per line
printenv NAME print just one variable’s value (empty, non-zero exit, if it is unset)

14 Environment and modules

ps

take a snapshot of the processes running right now — what is alive, with their PIDs.

aux ALL processes for all users, with CPU/memory (the common BSD-style form)
-ef the same idea in System-V style; pipe into `grep` to find one
-p PID show just one process by PID

15 Remote work and moving data

rsync

sync files and directories — locally or over ssh — copying only what has changed. The workhorse for moving data on and off a cluster.

-a archive mode: recurse and preserve permissions, times, and symlinks (the everyday default)
-v  /  --progress verbose / show a progress bar on large transfers
-z compress data in transit (helps over a slow link)
--dry-run show what WOULD transfer, changing NOTHING — run this first
--delete delete files on the destination that are gone from the source (dangerous — see the admonition)

15 Remote work and moving data

sacct

report accounting/history for jobs that have run — their state and exit code.

sacct your recent jobs and how they ended (`COMPLETED`, `FAILED`, `CANCELLED`, `TIMEOUT`)
-j JOBID one job by id

16 SLURM submission scripts

sbatch

submit a batch script to the scheduler — it queues the job and prints a job ID.

sbatch script.sh queue the script; it runs later on a compute node, its output going to `slurm-<jobid>.out`
--array=1-N submit a job ARRAY — run the script N times, each task with its own `$SLURM_ARRAY_TASK_ID`
(resources) set with `#SBATCH` directives at the top of the file, not as command-line flags

16 SLURM submission scripts

scancel

cancel a queued or running job by its job ID.

scancel JOBID cancel that one job
-u "$USER" cancel ALL of your jobs at once (use with care)

16 SLURM submission scripts

scp

copy files to or from a remote machine over ssh — a simple one-shot transfer.

file host:path copy a local file UP to the remote
host:path . copy a remote file DOWN to here
-r copy a directory and everything in it

15 Remote work and moving data

sinfo

show the cluster’s partitions (queues) — their names, time limits, and how busy they are.

sinfo list the partitions with their `STATE` (`idle`, `mix`, `alloc`) and `TIMELIMIT`

16 SLURM submission scripts

source

run a script in the CURRENT shell (not a child), so the variables, functions, and PATH changes it makes persist.

source FILE  /  . FILE execute FILE’s lines in this shell — `.` is the POSIX spelling of the same thing

14 Environment and modules

squeue

show the job queue — what is pending, running, and whose.

-u "$USER" only YOUR jobs (the everyday view)
-j JOBID one specific job by id
ST column the state: `PD` pending (waiting in line), `R` running, `CG` completing

16 SLURM submission scripts

ssh

open a shell on a remote machine — or run a single command there — over an encrypted connection.

user@host log in as `user` on `host` and get an interactive remote shell
host CMD run ONE command on the remote and return (e.g. `ssh euler squeue`)
-i keyfile authenticate with a specific private key
-p N connect on port N (the default is 22)

15 Remote work and moving data

tar

bundle many files and directories into one archive (and, with -z, compress it) — for transport or backup.

-czf a.tar.gz dir/ CREATE a gzip-compressed archive of dir/ (c=create, z=gzip, f=file)
-xzf a.tar.gz EXTRACT a gzip archive
-tzf a.tar.gz LIST an archive’s contents without extracting
-C dir change to dir first — archive from, or extract into, a chosen directory

15 Remote work and moving data

tmux

run a persistent terminal that survives disconnects — detach, log out, and reattach later with your work still running.

tmux start a new session
Ctrl-b d DETACH — leave it running and drop back to your normal shell
tmux attach  /  tmux a re-attach to the running session after you reconnect
tmux ls list running sessions

15 Remote work and moving data

top

watch processes live — a continuously updating table of what is using the CPU and memory.

top start the live view (press `q` to quit)
htop a friendlier colour version, if installed — scrollable, with click-to-kill

15 Remote work and moving data

watch

re-run a command every couple of seconds and show its latest output in place — to watch something change.

watch CMD re-run CMD every 2 seconds (e.g. `watch squeue` to watch a job queue)
-n N set the interval to N seconds
-d highlight what changed since last time

15 Remote work and moving data

Part V

Command

Job

Workhorse flags

Introduced in

make

run a Makefile — build each target from its prerequisites, rebuilding only what is out of date.

make build the first (default) target in `Makefile`
make TARGET build a specific target by name (e.g. `make clean`, `make results.txt`)
-n dry-run — PRINT the commands it would run, without running them (the `–dry-run` habit, Notebook 15)
-j N run independent recipes in parallel across N jobs (the `xargs -P` idea, Notebooks 5 & 17)

18 make and Makefiles