Frequently Asked Question

What does sed -i do, and what is the catch?

sed -i edits a file in place: instead of printing the transformed text to standard output, it overwrites the original file with the result. It is a huge convenience for one-line mass edits, sed -i 's/old/new/g' *.conf will fix every config file in a directory in seconds. To keep a safety net, sed -i.bak saves the original as file.bak before clobbering it.

The catch is that there is no undo. If your regex is wrong or your replacement string is wrong, you have just corrupted every file you targeted. Also, BSD sed (the one on macOS) requires an argument to -i, even an empty one: sed -i '' 's/old/new/' file, whereas GNU sed accepts a bare -i. Scripts that hop between Linux and macOS will trip on this. Always test the substitution without -i first, eyeball a few lines of the output, and only then commit.

Further reading and video