Start by wrapping text to a chosen width
A useful incident record can be too wide for a terminal, ticket, or printed handoff. fold reads text and inserts newlines so each output segment fits a selected width. It writes the transformed view to standard output and does not modify its input file. The important word is view: an inserted newline is indistinguishable from a newline that was already data.
Work in a directory where you may create a new fold-demo subdirectory. mkdir creates it; each && runs the next command only after success; and cd makes it the working directory. The printf format %s\n prints each following quoted record and a newline. A backslash immediately before each displayed newline is Bash continuation syntax, not record data. The shell's > redirection creates events.log, replacing that name if it exists. The printf article develops this separation of format from data.
The -w option takes the maximum width as its argument. Here -w 32 asks fold for segments no wider than 32 column positions. events.log is the input operand. With plain ASCII and no tabs or cursor-motion controls, one character occupies one column in this example.
mkdir fold-demo &&
cd fold-demo &&
printf '%s\n' \
'2026-11-20 edge01 auth-failure source=198.51.100.24' \
'2026-11-20 edge02 config-change actor=deploy' > events.log &&
fold -w 32 events.log2026-11-20 edge01 auth-failure s
ource=198.51.100.24
2026-11-20 edge02 config-change
actor=deployThe third output line ends with a space after change; it is preserved input, not padding. POSIX specifies a default width of 80 when -w is omitted. Width is a maximum for the newly produced segments, not a request to pad short lines.
Prefer blank boundaries when words matter
The first result split source because the width boundary landed inside that word. Add -s when a readable handoff should break after the last blank that fits. If no blank is available in a segment, fold still breaks at the width; one unbroken token can therefore be split.
This fixture is a single logical record. -s selects blank-aware breaks, while -w 20 retains the 20-column limit. POSIX calls the break character a blank, which includes a space or tab in the applicable locale. The blank remains before the inserted newline; fold does not trim or reflow the sentence.
printf '%s\n' 'ID=492 owner=platform severity=high action=review' > review.txt &&
fold -s -w 20 review.txtID=492
owner=platform
severity=high
action=reviewThe first three displayed lines each end in a space. fold -s is not a paragraph formatter: it neither joins short input lines nor balances line lengths. Use a formatter such as fmt when reflowing prose is the real job, and test that separate tool's rules.
Keep wrapping out of machine-readable pipelines
A display improvement can be a data-format failure. events.log contains two records because newline is its record separator. After folding, it contains four apparent lines. A downstream line-oriented counter, parser, or alert engine cannot tell which newlines were inserted.
The shell's | connects fold's standard output to wc's standard input. In wc -l, -l counts newline bytes. This is a count of the transformed view, not of source events. The wc article explains why counting delimiters and counting domain records are different contracts.
fold -w 32 events.log | wc -l4Keep the original artifact and generate a disposable view at the presentation boundary. Do not wrap before signing, hashing, parsing, importing, or comparing records. Removing every newline later cannot reconstruct the original two record boundaries.
Columns and bytes diverge at a tab
Without -b, POSIX fold measures display columns. A tab advances to the next tab stop, fixed every eight columns by the standard. With width 6, the tab after A would move beyond the boundary, so this GNU 9.7 execution inserts a newline before the tab and another after it. A terminal would otherwise expand the tab according to its current column.
The second command adds -b, which counts bytes rather than column positions. A, tab, and X are three bytes, so they remain together under a six-byte limit. od makes the invisible bytes inspectable: -A n suppresses offsets, -t x1 requests one-byte hexadecimal units, and -v prevents repeated rows from being abbreviated. The od article provides the fuller byte-inspection model. The 0a byte is newline and 09 is tab.
printf 'A\tX\n' > tabbed.txt &&
fold -w 6 tabbed.txt | od -A n -t x1 -v &&
fold -b -w 6 tabbed.txt | od -A n -t x1 -v 41 0a 09 0a 58 0a
41 09 58 0a-b changes the measuring rule; it does not convert tabs into spaces. If fixed tab stops are part of a display contract, the forthcoming expand article will cover converting tabs explicitly. FreeBSD's manual recommends expanding tabs or choosing a width that is a multiple of eight.
Backspace and carriage return change the column counter
Two other control characters make the default model less intuitive. Without -b, backspace decrements the current column count without going below zero, and carriage return resets it to zero. Neither byte deletes earlier input. fold passes both through, so the eventual display still depends on the consumer.
The first synthetic line contains abc, a backspace, and Xdefgh. Because backspace moves the counter back one position, GNU fold -w 6 places the inserted newline after f. The second contains a carriage return after abc; resetting the counter lets XY123 fit before the inserted newline. od -t c names control bytes instead of asking the terminal to act on them; -A n and -v retain the meanings introduced above.
printf 'abc\bXdefgh\n' | fold -w 6 | od -A n -t c -v
printf 'abc\rXY12345\n' | fold -w 5 | od -A n -t c -v a b c \b X d e f \n g h \n
a b c \r X Y 1 2 3 \n 4 5 \nThis column arithmetic models old finite-width output devices; it is not terminal emulation. OpenBSD documents a known mismatch for backspace-encoded double-width overstrikes. If exact rendering matters, normalize through an appropriate trusted renderer rather than assuming fold predicts every terminal.
Test Unicode on the installed implementation
A locale tells a conforming implementation how byte sequences form characters and how many columns those characters occupy. POSIX says a line must not be broken inside a character, even in byte-counting mode. That contract is not safe to infer from the command name or from another host's version.
The article's Debian 13 test environment has GNU Coreutils 9.7. In its C.UTF-8 locale, the command below feeds two three-byte copies of 界 followed by X. A width of four should not bisect a UTF-8 character, but GNU 9.7 inserts 0a after the first byte (e7) of the second 界; the continuation bytes 95 8c begin the next line. The observation is a version-specific incompatibility, not behavior to copy.
GNU Coreutils 9.8 added multibyte support and the -c/--characters option. Version 9.11 fixed output truncation on byte 0xff and delayed streaming, both regressions introduced in 9.8. Version 9.12, released September 14, 2026, is the current stable release at publication review time. Upstream master after 9.12 records additional, unreleased fixes: a partial read from a pipe or terminal can split a multibyte character into separate bytes, a bug introduced in 9.11; a wide character near the start of a line followed by multiple backspaces can wrap prematurely; and backspaces after a tab can be counted incorrectly, the latter two bugs introduced in 9.8. Those master fixes are not part of the released 9.12 record.
printf '界界X\n' | LC_ALL=C.UTF-8 fold -w 4 | od -A n -t x1 -v e7 95 8c e7 0a 95 8c 58 0aDo not use -c on 9.7 or assume a BSD has it. These are documented correctness bugs, not evidence of exploitation. Pin and test the deployed version with non-ASCII, pipe, tab, and backspace fixtures before using fold on internationalized evidence.
Wrapping hostile text does not make it terminal-safe
Logs and supplied artifacts can contain terminal control sequences. fold counts or passes their bytes, but it does not escape, remove, or validate them. The synthetic fixture below contains the escape sequence ESC [ 31 m, which requests red text on many terminals, and ESC [ 0 m, which resets styling. It uses no real alert or secret.
printf interprets \033 as the escape byte. Rather than print the result to a terminal, the pipeline folds it and sends it directly to od. In the output, 1b is ESC. The inserted 0a bytes divide the sequence after 1b 5b, but every original control byte remains. A later concatenation, pager, log viewer, or terminal can still interpret them in context.
printf 'status=OK \033[31mFAIL\033[0m actor=demo\n' |
fold -w 12 |
od -A n -t x1 -v 73 74 61 74 75 73 3d 4f 4b 20 1b 5b 0a 33 31 6d
46 41 49 4c 1b 5b 30 6d 20 0a 61 63 74 6f 72 3d
64 65 6d 6f 0aTreat untrusted text as data: inspect bytes, use a viewer with an explicit escaping policy, and preserve the original separately. A line-length cap can reduce horizontal sprawl, but it is not sanitization, an integrity check, or a defense against terminal escape injection.
Check failures before trusting the view
fold can read multiple files, but a mixed success is still a failed operation. GNU 9.7 diagnoses a missing operand on standard error, continues to the readable file, and exits nonzero. A report that captures only standard output could therefore look complete enough while silently omitting an input.
Run the command in a subshell so the demonstration can capture its status without changing the surrounding shell's error policy. The shell's 2> redirects diagnostics to fold.err, while > saves standard output to partial.txt. $? is the immediately preceding command's exit status; the outer printf records it. cat then prints the diagnostic and the partial output.
(
fold -w 32 missing.log events.log > partial.txt 2> fold.err
printf 'status=%s\n' "$?"
cat fold.err
cat partial.txt
)status=1
fold: missing.log: No such file or directory
2026-11-20 edge01 auth-failure s
ource=198.51.100.24
2026-11-20 edge02 config-change
actor=deployGNU diagnostic wording may vary by version and locale. Automation should test the exit status, not match this English sentence. The partial output is intentionally retained only inside the disposable lab; do not promote it as a complete report.
Write the wrapping contract before the command
The portable POSIX interface is small: -w width chooses a positive maximum, -s prefers the last blank that fits, and -b measures bytes instead of columns. GNU, FreeBSD, and OpenBSD provide those options, but their character-width behavior and edge cases have changed across releases. GNU's newer -c character-counting mode is an extension, not part of that common contract.
Decide whether the output is for people or another program; whether the limit is columns, bytes, or characters; which locale and implementation define width; whether tabs and cursor controls are permitted; and whether preserving original record boundaries matters. For an administrative handoff, preserve the source, generate the wrapped view last, record the exact tool version, and reject a failed run. fold is dependable when inserting a newline is explicitly allowed—and dangerous when a convenient display is mistaken for unchanged data.