grep1-6 Exercise 3: Rename a Word Across a Folder, Safely ========================================================== (Run in a fresh copy of the practice folder from the chapter. Make it first with the block at the top of the chapter.) echo "--- preview (nothing changes):" grep -rlZ colour site | xargs -0 sed -n 's/colour/color/gp' echo "--- apply, with backups:" grep -rlZ colour site --exclude='*.bak' | xargs -0 -r sed -i.bak 's/colour/color/g' echo "left saying colour (outside the backups): $(grep -rl colour site --exclude='*.bak' | wc -l)" echo "--- run it again:" grep -rlZ colour site --exclude='*.bak' | xargs -0 -r sed -i.bak 's/colour/color/g' echo "files in site:"; find site -type f | sort echo "--- restore from the backups:" find site -name '*.bak' -print0 | while IFS= read -r -d '' f; do mv -- "$f" "${f%.bak}"; done echo "files saying colour again: $(grep -rl colour site | wc -l); backups left: $(find site -name '*.bak' | wc -l)" Output: --- preview (nothing changes): body { color: blue } the color red another color my color file --- apply, with backups: left saying colour (outside the backups): 0 --- run it again: files in site: site/css/s.txt site/css/s.txt.bak site/index.txt site/index.txt.bak site/my notes.txt site/my notes.txt.bak --- restore from the backups: files saying colour again: 3; backups left: 0 WHY THIS WORKS AS AN ANSWER --------------------------- Preview first: sed -n with the p flag prints only the lines that would change, so you can read exactly what the real run will do. The apply step uses -lZ with xargs -0 so 'my notes.txt' is one argument, -i.bak so every original survives, --exclude='*.bak' so the backups are never searched, and xargs -r so that a run with nothing to do runs nothing. The second run finds no file to change, so grep prints no names and xargs -r does not start sed at all (without -r, GNU xargs starts sed once with no files and it complains 'sed: no input files'). The file list shows each file once plus its single .bak: no .bak.bak. The restore loop reads the NUL-separated names, moves each backup over the edited file, and the last line proves it: three files say colour again and no backup is left. This is reversible only because the backups were made BEFORE the edit.