Code & computing / QUICK REFERENCE

Bash & Linux essentials

Navigate files, inspect text, handle quoting and control a shell without relying on destructive shortcuts.

40 entries

Inspect files and space

First twenty lines
head -n 20 notes.txt

Reads the beginning of a text file. [2]

Last twenty lines
tail -n 20 notes.txt

Reads the end of a text file. [2]

Follow a rotating log
tail -F app.log

GNU behavior retries the filename after rotation. [2]

Count lines
wc -l notes.txt

Counts newline characters. [2]

File metadata
stat notes.txt

Shows size, times and permissions. [2]

Disk space by filesystem
df -h

Reports mounted filesystem usage. [2]

Directory disk usage
du -sh logs

Summarizes allocated space beneath logs. [2]

Create nested directories
mkdir -p "project files/output"

Creates absent parents without deleting contents. [2]

Quoting and data flow

Literal string
printf '%s\n' '$HOME'

Single quotes keep the dollar sign literal. [1]

Expand a variable safely
printf '%s\n' "$HOME"

Double quotes preserve the value as one argument. [1]

Set a shell variable
project='my project'

No spaces around the assignment operator. [1]

Export to child processes
export PROJECT_NAME='atlas'

Makes the value available to subsequently launched children. [1]

Capture output
today=$(date +%F)

Captures stdout; trailing newlines are removed. [1]

Pipe output to a command
printf '%s\n' one two | wc -l

Sends stdout through the pipe. [1]

Redirect with overwrite
printf '%s\n' draft > notes.txt

Creates or replaces notes.txt: existing contents are lost. [1]

Append to a file
printf '%s\n' next >> notes.txt

Adds to the end instead of truncating. [1]

Interactive shell controls

Move to line start
Ctrl+a

Default Emacs-style readline binding. [1]

Move to line end
Ctrl+e

Default Emacs-style readline binding. [1]

Search previous commands
Ctrl+r

Review the matched command before executing it. [1]

Cancel foreground command
Ctrl+c

Sends an interrupt; applications may handle it differently. [1]

Suspend foreground job
Ctrl+z

Stops, rather than terminates, a job. [1]

List this shell's jobs
jobs

Does not list every system process. [1]

Resume in foreground
fg

Resumes the current job on your terminal. [1]

Show previous exit status
printf '%s\n' "$?"

Run immediately after the command of interest. [1]

Reserved placement, separate from reference controls. No ad network is active.

Updated Bash 5.x / GNU userland

Sources

  1. Bash reference manual ↗
  2. GNU Coreutils manual ↗
  3. GNU grep manual ↗
  4. GNU Findutils manual ↗