Reference

Shell & command line

Read commands, quoting, streams, paths and scripts.

Scope and conventions

POSIX sh (2017 baseline), Bash additions and named Unix tools. Examples are independent fragments; file paths are illustrative. Bash-only syntax is labeled. macOS and Linux utility flags can differ.

Files & inspection

Select files without parsing ls

POSIX sh

find walks a tree. -exec passes each pathname as an argument without splitting it on whitespace.

Fragment

POSIX sh · Select files without parsing ls
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
find ./src -type f -name '*.ts' -exec wc -l {} +

Do not feed arbitrary newline-delimited filenames to unquoted command substitution.

Search source with explicit scope

POSIX sh

ripgrep is a separately installed tool. -F searches literal text, -n shows line numbers, -g filters paths, and --files lists searched files.

Fragment

POSIX sh · Search source with explicit scope
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
rg --files -g '*.ts' ./src
rg -n -F 'loadUser(' ./src
rg -n -g '*.ts' 'TODO|FIXME' ./src

By default rg filters ignored, hidden and binary files. Exit 1 means no match; 2 means an error. No match in this set does not establish absence everywhere.

Inspect a bounded amount of text

POSIX sh

head and tail select ends of a file; sed -n selects a line range; wc counts. These operate on text, not structured JSON.

Fragment

POSIX sh · Inspect a bounded amount of text
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
head -n 20 ./notes.txt
tail -n 20 ./notes.txt
sed -n '10,25p' ./notes.txt
wc -l ./notes.txt

Create, copy and move paths

POSIX sh

mkdir -p creates missing parent directories. cp copies; mv moves or renames. Existing destinations can be overwritten according to each utility’s rules.

Fragment

POSIX sh · Create, copy and move paths
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
mkdir -p ./scratch/reports
cp ./notes.txt ./scratch/notes-copy.txt
mv ./scratch/notes-copy.txt ./scratch/notes.txt

Use a disposable workspace when learning file operations; inspect the destination first.

Read file and directory permissions

POSIX sh

r/w/x mean read/write/execute for files. On directories, x allows traversal and w controls entry changes subject to other rules. chmod u+x adds owner execute permission.

Fragment

POSIX sh · Read file and directory permissions
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ls -ld ./scripts
ls -l ./scripts/check.sh
chmod u+x ./scripts/check.sh

Permissions and ownership are separate. umask masks initial permission bits; existing files do not change.

Remove a chosen file or empty directory

POSIX sh

rm removes a directory entry; rmdir removes an empty directory. Neither operation normally goes through a desktop trash folder. -r recursively descends and -f suppresses prompts/some missing-file errors.

Fragment

POSIX sh · Remove a chosen file or empty directory
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ls -l ./scratch/notes-copy.txt
rm ./scratch/notes-copy.txt
rmdir ./scratch/empty-directory

Inspect the exact operands before deleting. Removing a name does not guarantee secure erasure of underlying data.

Select and transform ordinary text

POSIX sh

grep selects matching lines; cut selects delimited fields; sed can select or replace text; awk provides record/field processing. Each tool has its own regex and escaping rules.

Fragment

POSIX sh · Select and transform ordinary text
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
grep -n 'ERROR' ./app.log
cut -d , -f 1 ./simple-records.txt
sed 's/old/new/g' ./notes.txt
awk '{ total += $2 } END { print total }' ./amounts.txt

cut is not a CSV parser: quoted delimiters and embedded newlines need a CSV-aware tool.

Create temporary storage with a declared tool baseline

POSIX sh

mktemp creates a uniquely named temporary file/directory. It is common on Linux/macOS, but not part of the POSIX.1-2017 utility baseline.

Fragment

POSIX sh · Create temporary storage with a declared tool baseline
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
scratch=$(mktemp -d "${TMPDIR:-/tmp}/example.XXXXXX") || exit 1
printf 'temporary directory: %s\n' "$scratch"
# Write disposable files under "$scratch".

Use cleanup that only removes the path you created. GNU/BSD mktemp flags differ beyond common template forms.