Stop making temporary files when editing configs
Text Processing (Grep/Sed/Awk)
Stop making temporary files when editing configs
🧩 The Challenge
Need to change a line in a critical config file, but you’re worried about breaking things. So you `cp` the original, `sed` the live one, then remember to delete the backup copy if it works. Or worse, you `sed` to a *new* file, then `mv` it back. It’s clunky, it’s slow, and it fills up your directory with junk.
💡 The Fix
`sed` can edit files directly, in-place, and even make a backup of the original file for you, all in one shot. It’s a lifesaver for quick config tweaks, cutting out those annoying extra steps and potential data loss.
sed -i.bak 's/old_string/new_string/g' /etc/nginx/sites-available/default
⚙️ Why It Works
The `-i` option tells `sed` to edit the file directly. And that `.bak` right after it? That’s the backup extension. `sed` will create `/etc/nginx/sites-available/default.bak` with the original content *before* making any changes.
🚀 Pro-Tip: On BSD `sed` (like on macOS), use `-i ”` if you truly don’t want a backup. GNU `sed` usually expects a value after `-i`.
Linux Tips & Tricks | © ngelinux.com | 11/21/2026
