brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sed -i.bak keeps a copy of the file you just changed

Sed
~/tips/sed-backup

sed -i.bak 's/old-host/new-host/g' config.txt

Edits a file in place with sed and saves the original as a backup copy.

sed -i edits a file in place, and there is no undo. Adding a suffix to the flag makes sed save the original first.

sed -i.bak 's/old-host/new-host/g' config.txt

After this, config.txt has the change and config.txt.bak is the file as it was. The attached form -i.bak works with GNU sed and with the BSD sed on macOS. Plain -i does not: on macOS it wants a suffix argument, which is why sed -i 's/a/b/' file fails there.

I would still run the command without -i first, since it prints the result to the screen and changes nothing.

Further reading: GNU sed manual

More Sed tips sed -n '$=' access.log echo 'hello world' | sed 's/.*/\U&/' printf 'key=1\nport=80\n' | sed '/^port=/a timeout=5' sed 's/\/mnt\/user\/appdata\/old/\/mnt\/user\/appdata\/new/' app.conf sed -n '210,218p' /etc/nginx/nginx.conf All Sed tips (6) All topics

Link