Spunk Autos

Websites and web tools

Terminal Tips for People Who Maintain Websites

You do not need to live in the terminal to get value from it. A few habits make routine website jobs faster and safer: checking that pages respond, finding a string across a site's files, or re-running a long command without retyping it. Each tip below includes the exact command to try.

Move faster at the prompt

  • Press Ctrl+R and type part of an old command to search your history.
  • Type cd - to jump back to the previous folder.
  • Use !! to repeat the last command, for example sudo !! when you forgot permissions.
  • Press Ctrl+A and Ctrl+E to jump to the start and end of the line.
  • Add an alias for any command you type daily, in your shell's startup file.

Write scripts that fail safely

Start bash scripts with set -euo pipefail. The script then stops at the first failing command, treats unset variables as errors and notices failures inside pipelines. Without it, a failed step can be followed by a step that deletes or overwrites the wrong thing.

Always quote variables: write "$file", not $file. File names with spaces, which are common in photo folders from a camera or phone, otherwise split into several arguments.

Check a list of URLs in one go

Put one URL per line in a file called urls.txt, then run: cat urls.txt | while read -r u; do curl -s -o /dev/null -w "%{http_code} $u\n" "$u"; done. Each line of output shows the status code and the URL, so 404 errors and unexpected redirects stand out.

Run it in parallel

Checking one URL at a time is slow on a big list. With xargs -P 8, eight requests run at once. Keep the number modest so you do not overload your own server.

Search a site's files

To find every page that still mentions an old phone number, run grep -rn "555-0100" . inside the site folder. The -n flag prints line numbers. Add -l to list only the file names, which is handy before a bulk replace.

Keep output you will need later

  • Pipe through tee results.txt to see output on screen and save it at the same time.
  • Add 2>&1 to capture error messages as well as normal output.
  • Use less +F on a log file to follow new lines, and press Ctrl+C then q to stop.
  • Prefix a long-running command with time to learn how long it really takes.

Habits that prevent damage

  1. Check which machine you are on before running anything destructive; set a clear prompt on production servers.
  2. Work in a copy or a Git branch when editing site files in bulk.
  3. Never paste a command from a web page without reading it first.
  4. Keep secrets out of commands, because they end up in your shell history.