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+Rand 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 examplesudo !!when you forgot permissions. - Press
Ctrl+AandCtrl+Eto 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.txtto see output on screen and save it at the same time. - Add
2>&1to capture error messages as well as normal output. - Use
less +Fon a log file to follow new lines, and pressCtrl+Cthenqto stop. - Prefix a long-running command with
timeto learn how long it really takes.
Habits that prevent damage
- Check which machine you are on before running anything destructive; set a clear prompt on production servers.
- Work in a copy or a Git branch when editing site files in bulk.
- Never paste a command from a web page without reading it first.
- Keep secrets out of commands, because they end up in your shell history.