Command line

Editing a File In Place, Measured: sed -i vs perl -pi vs awk

The -i flag costs nothing, the backup costs nothing, and the awk temp-file form costs twice as much — but none of the six arms edits in place at all: every one replaces the file, breaks symlinks, breaks hard links, and the two temp-file forms widen the file mode.

Written November 10, 2026 · 14 min read

By the end of this guide
  • The table: the -i flag costs nothing, the backup costs nothing, and nothing edits in place
  • The closest published work is one run each, with no table
  • The symlink that stops being a symlink
  • The hard link that breaks on every arm

What this guide is

One corpus, six arms, three reps each, run twice: once the night before and once fresh at build time, on the same box, so every number below is one of two medians from the same probe. The corpus is 1,000,000 lines of log text, 77,547,692 bytes, built deterministically from a fixed seed: every 20th line carries level=error, 50,000 error lines in all, and the edit is one substitution, s/level=error/level=warning/g, applied to the whole file. Every rep of every arm returned rc=0, and every arm's output was byte-identical to the output Python computed independently from the corpus, on all 18 reps of both runs, so a timing number can never come from a run that produced different output.

The six arms are the same job written six ways: sed -i, sed -i.bak, perl -pi, perl -pi.bak, an awk program writing to a temp file followed by mv, and a sed program writing to a temp file followed by mv. The question is the one people ask before editing a file with a one-liner: does the -i flag cost anything, does the backup cost anything, and what does "in place" actually do to the file while it edits. The manual answers the last part in one sentence, and the probe checks it by inode number rather than taking the manual's word for it.

The answer is a correction in two directions. The -i flag costs nothing and the backup costs nothing: sed -i, sed -i.bak, perl -pi, and perl -pi.bak all land within about 30 percent of each other on both runs, and the backup arms are no slower than the plain arms on either run. But the awk temp-file form costs about twice as much as the fastest arm on both runs, and none of the six arms edits in place at all: every one replaces the file, a new inode on every rep, breaks symlinks, breaks hard links, and the two temp-file forms silently widen the file mode from 0640 to 0664.

The table: the -i flag costs nothing, the backup costs nothing, and nothing edits in place

Wall seconds, medians of three runs. Every rep returned rc=0, every output matched the independently computed expected output, and the inode changed on every rep of every arm on both runs.

The corpus: 1,000,000 lines, 77,547,692 bytes, every 20th line level=error, 50,000 error lines; the edit is s/level=error/level=warning/g; the output is 77,647,692 bytes on every rep.

  • sed -i: banked 0.2142 (0.2052 to 0.2204), fresh 0.1817 (0.1801 to 0.1897). Inode changed, mode 0640 preserved, no backup file.
  • sed -i.bak: banked 0.1724 (0.1691 to 0.1767), fresh 0.1718 (0.1693 to 0.1718). Inode changed, mode 0640 preserved, backup file left behind, byte-identical to the original corpus on all three reps of both runs.
  • perl -pi -e: banked 0.2264 (0.2224 to 0.2430), fresh 0.2316 (0.2242 to 0.2510). Inode changed, mode 0640 preserved, no backup file.
  • perl -pi.bak -e: banked 0.2239 (0.2160 to 0.2451), fresh 0.2180 (0.2175 to 0.2251). Inode changed, mode 0640 preserved, backup file left behind, byte-identical to the original corpus on all three reps of both runs.
  • awk '{gsub(...); print}' > tmp && mv: banked 0.4167 (0.4162 to 0.4236), fresh 0.4216 (0.4161 to 0.4218). Inode changed, mode widened 0640 to 0664 on every rep, no backup file.
  • sed 's/.../' > tmp && mv: banked 0.1886 (0.1848 to 0.2094), fresh 0.1855 (0.1845 to 0.2009). Inode changed, mode widened 0640 to 0664 on every rep, no backup file.

Against sed -i, the backup arm costs 0.80 times banked and 0.95 fresh; perl -pi costs 1.06 banked and 1.27 fresh; perl -pi.bak costs 1.05 banked and 1.20 fresh; the awk temp-file form costs 1.95 banked and 2.32 fresh; the sed temp-file form costs 0.88 banked and 1.02 fresh. The four sed and perl forms tie within about 30 percent on both runs, and the awk form is the only real gap, at about twice the fastest arm on both runs.

One cell moved between runs, and the guide says so plainly. The sed -i median fell from 0.2142 banked to 0.1817 fresh, a 15 percent move, while the other five arms moved under 3 percent. That move flips the sed-to-perl ordering: perl costs 1.06 times sed banked and 1.27 fresh. The 0.17 to 0.23 second cells sit near the spawn-and-read floor for a 77 megabyte file, so the exact ordering between the four sed and perl forms is floor noise, not a ranking. The stable claims are the ones that hold on both runs: the backup costs nothing measurable on either tool, and awk costs about twice as much. Do not quote the 0.80 or 0.95 ratios as "the backup makes it faster"; the backup arm is no slower than the plain arm on both runs, and that is the claim.

The finding that does not move at all is the inode. The inode changed on every rep of every arm on both runs, 36 of 36 timed reps. "In place" is a rename in all six forms, not an overwrite of the same file. The backreference.org piece states this from the manuals, and the probe verifies it by inode number rather than asserting it.

The closest published work is one run each, with no table

The closest published table is from 2011, and it is one run each on a different job. The klashxx piece runs perl and sed on an 897,872-line file to reorder and re-case names with a complex regex, and reports perl at 6.71 seconds against sed at 6.51, calling it a draw. It runs no awk arm, no -i.bak arm, and no symlink, hard-link, or mode checks, and it prints one run per tool with no reps.

The backreference.org piece on in-place editing is prose mechanics with no timings, and it is the piece this probe verifies. It states that sed creates a temp file and renames it over the original, which a simple ls -i before and after will show, and that perl unlinks the original file, which makes an interrupted perl edit more dangerous than an interrupted sed edit. It recommends always supplying a backup extension, and it calls the explicit temp-file form, command file > tempfile && mv tempfile file, the sane method. The probe agrees on the mechanics by inode number, agrees the backup is free, and disagrees only on the cost ranking: the temp-file form with awk is not free, it costs twice as much, while the sed temp-file form ties the -i forms.

The copyprogramming piece asserts numbers on a different job. Its table lists sed at 57 seconds and perl at 42 seconds on 50 million lines for case conversion, with no corpus named, no run timed, and no rep stated. Those numbers are asserted, not measured, and the job is case conversion, not in-place substitution. This probe does not adjudicate that assertion; it measures a different job, and the case-conversion family is a separate question this site does not re-answer here.

The manuals state the semantics the probe checks. The sed manual page says -i edits files in place and makes a backup if a suffix is supplied, and that the default operation mode breaks symbolic and hard links, which --follow-symlinks can change for symbolic links only. The perlrun documentation says -i renames the input file, opens the output file by the original name, and that without an extension the original file is kept open without a name and unlinked when perl exits, and that soft and hard links are not preserved. The probe confirms both: every arm breaks both kinds of link, and the backup arms leave a backup byte-identical to the original.

The trap file is three lines: two error lines and one info line, with a symlink pointing at it. All six arms replace the symlink with a regular file. The link is gone after the edit on every arm, the target file is untouched, its content unchanged and its inode the same, and the edited content lands in a new regular file at the link's path. A reader who edits through a symlink and then follows the link expecting the edited content will find the old content instead, because the edit went to a file that is no longer a link.

The fix is one flag, and it was executed. sed -i --follow-symlinks keeps the link and edits the target: the link is still a link after the edit, and the target file's content changed. The manual says so plainly: --follow-symlinks follows symlinks when processing in place. Perl has no equivalent flag in this probe; the perlrun documentation states that links are not preserved, full stop. If the file is reached through a symlink, either use the sed flag or edit the target by its real path.

The same three-line trap file, with a second name hard-linked to it. All six arms break the link. The other name keeps the original content on every arm, and the inodes diverge on every arm. No arm edits through a hard link, and there is no flag in this probe that makes one do so. An old sed man page (the die.net copy, from the sed 4.2 era) documents a -c/--copy option that avoided breaking links at the cost of atomicity, but that option was removed long ago: the box's GNU sed 4.9 rejects both --copy and -c as unrecognized, so on any current sed there is no such flag to run.

The practical rule follows from the trap, not from the manual: if two names share one inode, an in-place edit splits them. The name you edited gets the new content and a new inode; the other name keeps the old content on the old inode. If the file is hard-linked, edit it once per name, or replace the edit with a write through the existing inode, which none of these six arms does.

The backup that costs nothing

sed -i.bak and perl -pi.bak leave a .bak file whose content is byte-identical to the original corpus on all three reps of both runs, and the backup arms are no slower than the plain arms on either run: 0.1724 against 0.2142 banked and 0.1718 against 0.1817 fresh for sed, 0.2239 against 0.2264 banked and 0.2180 against 0.2316 fresh for perl. The four plain arms leave no backup file. The perlrun documentation states that perl creates the backup file whether or not any output is actually changed, and the probe's backups confirm the file exists with the original content on every rep.

There is no measured reason to skip the backup on a file you care about. The cost is zero on both runs, and the backup is the only copy of the original that survives the edit. The backreference.org piece makes the same recommendation from the mechanics alone: perl unlinks the original, so an interrupted edit without a backup loses it. This probe did not test an interrupted edit, so the guide does not present the interrupt danger as a measured finding; it is prose here, from the documentation, not from a run.

The mode the temp file widens

The two temp-file forms widen the mode on every rep of both runs, and the four -i forms preserve it on every rep. The corpus file starts at mode 0640. After sed -i and perl -pi, with and without .bak, the edited file is still 0640. After awk ... > tmp && mv and sed ... > tmp && mv, the edited file is 0664. The temp file is created under the shell's umask, which is 0002, and mv keeps the temp file's mode. A chmod after the mv is required to keep a restrictive mode; the -i forms do it for free.

One correction belongs in the guide itself. The banking run's summary wrote the widened mode as 0644. The probe log on both runs says 0664, and the box umask is 0002, so 0664 is the measured value. Under a shell with umask 022 the same form would produce 0644, which is where the 0644 figure came from. The widening is the stable finding; the exact mode depends on the umask of the shell that creates the temp file, so check yours before trusting either number.

Checklist

  • Before editing a file in place, check whether the file is a symlink: all six arms replace the link with a regular file and leave the target untouched, and only sed -i --follow-symlinks edits the target and keeps the link.
  • Before editing a hard-linked file, check the other names: all six arms break the link, the other name keeps the original content, and no flag in this probe edits through a hard link.
  • Before skipping the backup, check the cost: the .bak arms are no slower than the plain arms on either run, and the backup is byte-identical to the original on every rep.
  • Before quoting a sed-to-perl ratio from this guide, check both runs: perl costs 1.06 times sed banked and 1.27 fresh, because the sed cell moved 15 percent between runs while the other five arms moved under 3 percent, so the ordering is floor noise and only the tie and the awk gap are stable.
  • Before piping a file through a temp-file form, check the mode: the temp-file arms widen 0640 to 0664 under this box's umask 0002, while the -i forms preserve 0640, and a chmod after the mv is required to keep a restrictive mode.
  • Before reusing these numbers, check the setup: WSL2, 32 CPUs, GNU sed 4.9, Perl v5.38.2, GNU Awk 5.2.1, one thread per arm, 1,000,000 lines, 77,547,692 bytes, outputs written to tmpfs files, warm page cache, shell umask 0002.

Sources

  • sed(1) Linux man page — read 2026-11-10: -i edits files in place and makes a backup if a suffix is supplied; the default mode breaks symbolic and hard links, which --follow-symlinks can change for symbolic links; the page's -c/--copy option does not exist in the box's GNU sed 4.9.
  • perlrun, Perl documentation — read 2026-11-10: -i renames the input file, opens the output file by the original name, and with no extension the original is kept open without a name and unlinked when perl exits; soft and hard links are not preserved; the backup is created whether or not any output changed.
  • klashxx, Text processing performance, Perl vs sed — read in full 2026-11-10: 2011, 897,872 lines, a complex regex reorder and re-case job, one run each, perl 6.71 seconds against sed 6.51, called a draw; no awk arm, no -i.bak arm, no symlink, hard-link, or mode checks. The closest published table; the gap this probe fills.
  • backreference.org, In-place editing of files — read in full 2026-11-10: prose mechanics, no timings; sed creates a temp file and renames it over the original, perl unlinks the original, the explicit temp-file form is the sane method, and a backup extension should always be supplied. The mechanics this probe verifies by inode number.
  • copyprogramming, Tail Tr Pipeline Outperforms Sed And Perl On Massive Files — read 2026-11-10: asserts sed 57 seconds and perl 42 seconds on 50 million lines for case conversion, with no corpus, no timed run, and no rep stated. A different job, asserted not measured; this probe does not adjudicate it.
  • Veldica, Converting CRLF to LF: tr vs sed vs perl vs awk, Measured — the sibling guide: uses sed -i as one arm of a conversion job and never measures in-place mechanics. This guide measures the mechanics; it does not re-teach the conversion table.

Keep Exploring