Wrapping Long Lines, Measured: fold vs fmt vs awk
Six ways to wrap the same 510,000 lines to 80 columns. fold is the fastest arm and the only one whose output matches a hand-rolled awk wrapper exactly, but fmt costs 3.15 times as much and silently rewrites the shape of the file, while fold cuts a 200-character word in half and fmt refuses to break it at all.
Written November 10, 2026 · 15 min read
- The table: fmt costs 3.15 times fold, and rewrites the shape of the file
- The closest published work asserts a speed gap, with no table
- The word that will not break
- The join: fmt rewrites paragraphs, fold leaves them alone
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 510,000 lines of prose-like text, 81,527,846 bytes, built deterministically from a fixed seed: 10,000 paragraphs of 50 lines each, each line 120 to 200 characters, ASCII only, no tabs, a blank line between paragraphs. Every rep of every arm returned rc=0, and every arm's output was identical across all three reps, so a timing number can never come from a run that produced different output.
The six arms are the same job written six ways: plain fold at width 80, fold with -s to break at word boundaries, fold with -b to count bytes instead of columns, fmt at width 80, fmt with -s to split only without joining, and a hand-rolled awk wrapper that prints the first 80 characters of a line, then the rest, until nothing is left. The question is the one people ask before piping a file through either tool: which one wraps fastest, and what does each one silently do to the text while it wraps.
The answer is a correction in both directions. fold is the fastest arm and the only one whose output is byte-identical to the hand-rolled awk wrapper, but fmt, the "smart" tool, costs 3.15 times as much as fold and silently rewrites the shape of the file: it joins short lines inside each paragraph before wrapping, so its output has 175,493 fewer lines than fold's on the same input. And the two tools disagree on the one case that decides the choice: fold cuts a 200-character word with no spaces into 80, 80, and 40 characters, while fmt refuses to break a word at all and emits the whole word on one 200-character line, past the width you asked for, with no warning.
The table: fmt costs 3.15 times fold, and rewrites the shape of the file
Wall seconds, medians of three runs. Every rep returned rc=0, and every arm's output hash was identical across all three reps of both runs.
The corpus: 510,000 lines, 81,527,846 bytes, 10,000 paragraphs of 50 prose-like lines of 120 to 200 characters, ASCII only, no tabs, blank line between paragraphs.
fold -w 80: banked 0.2274 (0.2248 to 0.2331), fresh 0.2293 (0.2293 to 0.2305). Output 82,287,362 bytes, 1,269,516 lines, longest line 80.fold -s -w 80: banked 0.2463 (0.2442 to 0.2490), fresh 0.2434 (0.2381 to 0.2639). Output 82,306,014 bytes, 1,288,168 lines, longest line 80.fold -b -w 80: banked 0.2098 (0.2073 to 0.2181), fresh 0.2086 (0.2073 to 0.2090). Output 82,287,362 bytes, 1,269,516 lines, longest line 80.fmt -w 80: banked 0.7340 (0.7182 to 0.7341), fresh 0.7233 (0.7106 to 0.7274). Output 81,527,846 bytes, 1,094,023 lines, longest line 79.fmt -s -w 80: banked 0.6456 (0.6423 to 0.7158), fresh 0.6623 (0.6554 to 0.6873). Output 81,527,846 bytes, 1,294,245 lines, longest line 79.awkhand-rolled fold: banked 0.3785 (0.3647 to 0.3942), fresh 0.3748 (0.3708 to 0.3982). Output 82,287,362 bytes, 1,269,516 lines, longest line 80.
Against plain fold, fmt costs 3.23 times on the banked run and 3.15 on the fresh run; fmt -s costs 2.84 and 2.89; the awk wrapper costs 1.66 and 1.63; fold -s costs 1.08 and 1.06; fold -b costs 0.92 and 0.91. fmt is 1.14 times slower than fmt -s on the banked run and 1.09 on the fresh run: joining lines costs time, so the split-only form is the faster fmt. The fold to fold -b gap is within noise on both runs, so the guide claims no ordering between them, only that both are the fastest arms measured.
Three outputs are byte-identical on the tab-free corpus, verified on all three reps of both runs: fold, fold -b, and the awk wrapper produce the same output hash. On plain ASCII with no tabs, the hand-rolled awk fold is GNU fold. Paying 1.63 times for awk buys nothing in the output here. The fold -s output is different: breaking at word boundaries adds 18,652 bytes and 18,652 lines against plain fold, one extra newline per break that lands on a space instead of mid-word.
The shape change is the finding that matters most. fmt outputs 1,094,023 lines against fold's 1,269,516: 175,493 fewer lines, because it joins short lines inside each paragraph before wrapping. fmt -s outputs 1,294,245 lines, more than fold, because it never merges the short lines the corpus's paragraph structure leaves behind. Same input, same width, three different documents. If the line count of the file matters to anything downstream, a diff, a line-numbered listing, a parser that reads line by line, then fmt is not a drop-in replacement for fold.
One curiosity is reported as an observation, not a law. fmt and fmt -s output exactly 81,527,846 bytes on this corpus: the input byte count, to the byte, on both runs. The joins and splits net to zero here. Do not read that as a rule about fmt; it is the count on this one corpus, and a different paragraph structure would give a different count.
Two outliers stay in the guide. fmt -s on the banked run printed 0.7158 seconds on its third rep against 0.6423 and 0.6456 on the first two, a first-run-style spike on the third rep. It did not repeat on the fresh run, where fmt -s printed 0.6554 to 0.6873. fold -s on the fresh run printed 0.2639 on its first rep against 0.2381 and 0.2434. The 0.21 to 0.73 second cells sit near the spawn-and-read floor for an 81 megabyte file, so treat the exact ratios as one-machine figures; the fmt gaps of 2.8 and 3.2 times are the stable claims, because they hold across both runs.
The closest published work asserts a speed gap, with no table
The closest published work is a prose comparison, not a measurement, and the distinction is the gap this probe fills. The SysTutorials piece on wrapping long lines walks through fold and fmt with example commands: fold -w 80, fold -s -w 80, fmt -w 80, fmt with paragraph preservation, fmt -g, fmt -u. It states the difference in prose: fmt joins short lines together into paragraphs and breaks long lines at word boundaries, and it is the better choice for prose and documentation. It prints no timings, no corpus, no reps, and no output counts, so the 3.15 times gap, the 175,493 line shape change, and the 200-character word behavior are all unmeasured there.
The copyprogramming guide on wrapping long lines goes further and asserts the speed claim outright: its comparison table lists fold as Faster and fmt as Slightly slower. That is an assertion with no numbers behind it. No corpus is named, no run is timed, no rep is stated. This probe adjudicates the assertion with reps: fmt is not slightly slower, it is 3.15 times slower than fold on the same corpus at the same width, and the split-only form is 2.89 times slower. The claim holds in direction and is far bigger than "slightly" suggests.
The Linux Handbook piece on fold and fmt explains the tools in depth, including the awk one-liner that measures the longest line, and the learnbyexample coreutils page documents the options with examples, including the fact that fmt adds a final newline even when the input lacks one. The O'Reilly Unix Power Tools chapter states the fold to fmt difference in prose: fold breaks text at an arbitrary column even in the middle of a word, while fmt thinks in paragraphs. None of them runs a timing table. The MIT and Utah copies of the GNU textutils manual give the reference definitions this guide grounds its claims in: fold counts screen columns by default, so a tab may count more than one column, and -b counts bytes instead; fmt fills and joins lines, prefers breaking at sentence ends, reads whole paragraphs before choosing breaks, and its -s option splits long lines without joining short ones.
The word that will not break
The trap file is 355 bytes: one 200-character word with no spaces, three short lines in one paragraph, a blank line, a line of 72 x characters, a tab, and 8 y characters, and a last line with no trailing newline. Every trap below was executed on that file, on the same box, at build time.
fold cuts the 200-character word into lines of 80, 80, and 40 characters. That is what fold does: it breaks at the width you asked for whether or not a word boundary exists there. fmt emits the whole word on one 200-character line, past the width you asked for, with no warning. fmt never splits a word, even one longer than the width. The manual states the rule in the other direction: fold with -s breaks after the last blank before the maximum length, and if the line contains no such blank, it breaks at the maximum length as usual. fmt has no such fallback. If the file can contain a token longer than the width, a long URL, a base64 blob, a log line with no spaces, then fmt will hand you lines longer than the width, and fold will hand you the word in pieces. Neither behavior is a bug; the trap is choosing without knowing which one you get.
The join: fmt rewrites paragraphs, fold leaves them alone
Three short lines in one paragraph, "short line one", "short line two", "short line three". fmt joins them into one line, "short line one short line two short line three". fold leaves them as three lines. fmt -s does not join: its trap output is 8 lines against fmt's 5. The -s flag is the switch between the two behaviors, and the manual says so plainly: split lines only, do not join short lines to form longer ones. The practical rule follows from the corpus table, not from the manual: if the input is prose you want reflowed, fmt's join is the feature; if the input is records, one per line, fmt's join silently merges records.
The merge across the trap boundary is the finding the banking run did not predict. fmt's trap output is 5 lines, not the 6 a line-by-line reading predicts. The tab line's wrapped tail, the 8 y characters, and the unterminated last line, "no trailing newline here", are in the same paragraph, so fmt joins them into one line: "yyyyyyyy no trailing newline here". Verified with cat -A on the real output: the last line reads "yyyyyyyy no trailing newline here" with a trailing dollar sign marking the newline fmt added. A reader who counts fmt's output lines by counting input lines will be wrong by exactly this merge.
Tabs count differently under fold and fold -b
One line: 72 x characters, a tab, and 8 y characters. fold, counting columns, treats the tab as advancing to column 80, so it splits the line as 72 x characters plus the tab on the first line, and the 8 y characters on the second. fold -b, counting bytes, treats the tab as one byte, so it splits as 72 x characters plus the tab plus 7 y characters on the first line, and 1 y character on the second. The manual states the rule: fold counts screen columns by default, so a tab may count more than one column, while -b counts bytes rather than columns, so tabs, backspaces, and carriage returns each count as one column like other characters.
This is the same tab question the tab-conversion guide on this site answers from the other side: expand and unexpand change the tab characters themselves, while fold and fold -b only disagree about how to count them. The two guides do not overlap. If the file has tabs, pick the counting rule before picking the tool, because the same width gives a different break point.
The missing newline: fmt adds one, fold does not
The trap file's last line has no trailing newline. fmt adds one; fold preserves the absence. A round trip through fmt changes the file's last byte. The learnbyexample coreutils page states it directly: fmt adds a final newline character even when it was not present in the input. The awk wrapper adds one too: its trap output is 359 bytes against the input's 355, because the print statement appends a newline to the last line. If a checksum, a byte count, or a downstream tool depends on the file ending without a newline, do not pipe it through fmt.
Checklist
- Before wrapping prose, check whether the line count matters: fmt joins short lines, so its output has 175,493 fewer lines than fold's on this corpus, and fmt -s has 24,729 more.
- Before wrapping a file that can hold a token longer than the width, check the word rule: fold cuts it at the width, fmt emits it whole on a line past the width, with no warning.
- Before wrapping a file with tabs, check the counting rule: fold counts columns, so a tab can advance to the next multiple of eight, while fold -b counts bytes, so the same width breaks at a different character.
- Before piping a file through fmt, check the last byte: fmt adds a trailing newline when the input lacks one, and fold preserves the absence.
- Before quoting a ratio from this guide, check both runs: fmt costs 3.23 times fold banked and 3.15 fresh, fmt -s costs 2.84 and 2.89, awk costs 1.66 and 1.63, and the exact ratios are one machine, coreutils 9.4, warm cache, tmpfs.
- Before reusing these numbers, check the setup: WSL2, 32 CPUs, GNU coreutils 9.4, GNU Awk 5.2.1, one thread per arm, 510,000 lines, 81,527,846 bytes, outputs written to tmpfs files, not piped through a consumer.
Related guides on this site
- Numbering a Million Lines, Measured: cat -n vs nl vs awk vs grep -n
- Finding Lines by Prefix: look vs grep vs rg vs awk vs sed, Measured
- Cutting a Million-Line File Into Ten, Measured: split vs csplit vs awk
- Joining Two Files on a Key: join vs awk, Measured
Sources
- GNU coreutils manual, fold invocation — read 2026-11-10: fold counts screen columns by default, so a tab may count more than one column, and -b counts bytes instead; -s breaks after the last blank before the maximum length, and breaks at the maximum length when no blank exists.
- GNU coreutils manual, fmt invocation — read 2026-11-10: fmt fills and joins lines, reads whole paragraphs before choosing breaks, prefers sentence ends, and -s splits long lines without joining short ones; the goal width defaults to 93 percent of the width.
- SysTutorials, Wrapping Long Lines in Text Files on Linux — read in full 2026-11-10: fold and fmt example commands with prose explanations, no timings, no corpus, no reps, no output counts. The closest published work; the gap this probe fills.
- copyprogramming, Bash Wrap Long Lines Inside the Same Column — read in full 2026-11-10: a comparison table asserting fold is Faster and fmt is Slightly slower, with no corpus, no timed run, and no rep stated. This probe adjudicates the assertion: fmt is 3.15 times slower, not slightly slower.
- Linux Handbook, Format Text in Linux Terminal With Fold and FMT Commands — read 2026-11-10: fold and fmt explained with examples, including the awk one-liner that measures the longest line. Prose and examples, no timing table.
- learnbyexample, fold and fmt - CLI text processing with GNU Coreutils — read 2026-11-10: the options documented with examples, including that fmt adds a final newline even when the input lacks one, and that fmt never splits a word even when it exceeds the maximum width.
- O'Reilly Unix Power Tools, 35.2 Neatening Text with fmt — read 2026-11-10: fold breaks text at an arbitrary column even in the middle of a word, while fmt thinks in paragraphs and wraps lines continuously. Prose, no measurements.
- Veldica, Seeing the Hidden Characters: cat -A vs sed -n l vs od -c, Measured — the sibling guide: the same "look at the bytes" family, a display question, not a wrapping question. This guide measures wrapping; it does not re-teach hidden characters.
- Veldica, Tabs to Spaces and Back: expand vs unexpand vs sed vs awk vs perl, Measured — the sibling guide: tab conversion, changing the tab characters themselves. This guide measures how fold and fold -b count tabs; it does not re-teach conversion.
Keep Exploring
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.
Paste sshd -G output and get a hardening verdict on the values your server actually runs.
Paste a server block and get WebSocket, TLS, HSTS, and proxy-header verdicts.
Paste a systemd timer expression and get the specific reason it fails, the normalized form, and the past-date traps the built-in check hides.
The full sysadmin hub \u2014 systemd, cgroups, file tools, and measured Linux comparisons.