Editing Files Directly
By default sed reads a file and writes the result to standard output, leaving the original untouched. The '-i' (in-place) option changes this so sed writes the modified content back to the file itself. Under the hood, sed does not truly edit in place — it writes to a temporary file and then renames it over the original, which keeps the operation atomic and avoids corrupting the file if the process is interrupted midway.
Cricket analogy: Default sed is like practising in the nets where the real scoreboard is untouched; '-i' is playing the actual match where every run you score is written permanently into the official record.
Creating Backups With -i
You can ask sed to preserve the original by giving '-i' a suffix. In GNU sed, '-i.bak' saves a copy of the original with a '.bak' extension before writing changes, so 'notes.txt' is backed up as 'notes.txt.bak'. This is a cheap safety net for irreversible edits. If the suffix contains a '*', GNU sed treats it as a template where the '*' is replaced by the filename, letting you place backups in another directory or add a prefix.
Cricket analogy: The '.bak' file is like the third umpire's saved video of the original delivery — if the on-field call (edit) turns out wrong, you can go back to the untouched footage.
GNU vs BSD/macOS Portability
The '-i' option is the most notorious portability trap in sed. GNU sed accepts '-i' with no argument (edit with no backup) and '-i.bak' with the suffix attached directly. BSD sed, including the sed shipped on macOS, requires an explicit argument for '-i' — even an empty string — so the correct macOS form is "-i ''" for no backup and '-i .bak' with a space for a backup. A script written as 'sed -i s/a/b/ file' will fail on macOS because it treats 's/a/b/' as the backup suffix.
Cricket analogy: It is like the different playing conditions between formats: a shot that scores in a day match (GNU) can get you out under the floodlit rules (BSD) because the local regulations differ.
In-Place Editing in Practice
# GNU sed: edit in place with no backup.
sed -i 's/localhost/127.0.0.1/g' config.ini
# GNU sed: edit in place, keeping a .bak backup.
sed -i.bak 's/DEBUG/INFO/' app.log
# BSD / macOS sed: no backup requires an explicit empty argument.
sed -i '' 's/localhost/127.0.0.1/g' config.ini
# Portable pattern: avoid -i entirely, write to a temp file, then move.
sed 's/foo/bar/' file > file.tmp && mv file.tmp fileBecause '-i' works by writing a temp file and renaming it, the file's inode changes and, on some systems, ownership or permissions may reset to the invoking user's defaults. If exact permissions or hard links must be preserved, edit to a temp file and copy the content back rather than relying on '-i'.
Never redirect sed's output back to the same file with 'sed ... file > file'. The shell truncates 'file' to zero length before sed reads it, destroying your data. Always use '-i', or write to a different temporary file and move it into place afterward.
- '-i' makes sed write changes back to the file instead of standard output.
- sed edits atomically via a temp file and rename, not a true in-place write.
- GNU '-i.bak' (suffix attached) creates a backup; a '*' in the suffix templates the filename.
- GNU accepts a bare '-i'; BSD/macOS requires an explicit argument, e.g. "-i ''" for no backup.
- '-i' can change the file's inode and reset ownership or permissions.
- Never do 'sed ... file > file' — the shell truncates the file first.
- For portability, either match the target sed's syntax or write to a temp file and mv.
Practice what you learned
1. What does the '-i' option do in sed?
2. How does GNU sed create a backup while editing in place?
3. What is the correct BSD/macOS sed form for in-place editing with no backup?
4. Why must you never run 'sed 's/a/b/' file > file'?
5. How does sed implement in-place editing internally?
Was this page helpful?
You May Also Like
Backreferences and Groups
Master capturing parts of a match with groups and reusing them via backreferences in both the pattern and the replacement of sed substitutions.
Branching and Loops in sed
Learn how sed uses labels, unconditional branches, and conditional branches to build loops and control flow inside its scripting language.
Multiline Processing
Learn how sed handles multiple lines at once using the pattern space, the hold space, and the D, P, N, H, and G commands to perform edits that span line boundaries.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics