Code Snippets

All Snippets

The latest code snippets.

Background Jobs and wait

Fan out work to background processes and wait for them all.

Author: chris

Loop SSH Across Hosts

Run the same command on a list of hosts with -o BatchMode for key-only auth.

Author: chris

Install a Cron Job from a Script

Append a cron line idempotently with grep -q before crontab.

Author: chris

Find Files Recursively

find with -type f, size filter, and -exec for batch action.

Author: chris

Parse Args with getopts

Stdlib short-option parsing with the `getopts` builtin.

Author: chris

Trap SIGINT for Cleanup

Use `trap` to remove a tempdir even when the user hits Ctrl-C.

Author: chris

Check if a Command Exists

Use `command -v` — portable, no subshell, returns nonzero if missing.

Author: chris

Date Arithmetic in Bash

GNU date supports relative expressions — yesterday, +7 days, etc.

Author: chris

Filter Columns with awk

Print specific columns and filter by a numeric threshold.

Author: chris