Site icon New Generation Enterprise Linux

Stop trying to edit config files with sed in place without a backup

Text Processing (Grep/Sed/Awk)

Stop trying to edit config files with sed in place without a backup

🧩 The Challenge

Everyone has nuked a production config file because they made a typo in a sed command and didn’t have a copy to revert to. I still remember the pit in my stomach the first time I realized I’d just deleted half of an apache config with a stray regex.

💡 The Fix

Start using the backup flag built into sed so the utility saves the original state of the file before it starts hacking it apart. It saves your bacon when that regex is slightly off.

sed -i.bak 's/original/replacement/g' /etc/nginx/nginx.conf

⚙️ Why It Works

Adding the suffix directly to the -i flag tells sed to rename the file to something like nginx.conf.bak right before it starts applying the edits. If the script goes sideways, you just move the backup back to the original name and breathe a sigh of relief.

🚀 Pro-Tip: Use a timestamped suffix in a loop if you’re feeling extra paranoid about running the same script multiple times.

Linux Tips & Tricks | © ngelinux.com | 9/2/2026

0 0 votes
Article Rating
Exit mobile version