100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

In-Place Editing

Learn how sed's -i option edits files directly on disk, how to create backups, and the portability differences between GNU and BSD/macOS sed.

Advanced sedBeginner8 min readJul 10, 2026
Analogies

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

bash
# 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 file

Because '-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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#InPlaceEditing#Place#Editing#Files#Directly#StudyNotes#SkillVeris#ExamPrep