Bash and Linux 5 · Searching and text

Replace text in a file

Replace every occurrence of a word in place.

sed -i 's/old/new/g' file.txt

Caution: -i edits the file in place. On macOS use sed -i '' instead. Test without -i first.

Official Bash and Linux 5 documentation

More searching and text commands

All searching and text commands · Bash and Linux 5 cheatsheet

Was this page helpful?