fold turns long input lines into shorter output lines without touching the source file. It suits readable terminal output, fixed-width exports and simple pipelines, and the examples take about five minutes with only an ordinary shell account; nothing here needs elevated privileges.
The installed command is GNU coreutils 9.4, with a default width of 80 columns. It reads named files, or standard input when no file is given or the file name is -, and writes wrapped text to standard output. It never edits the input file in place.
That last point is a real safety boundary: redirecting output to a new file is easy, while redirecting it over the input can empty the file before fold even gets to read it. Keep input and output paths different until you have checked the result.
Start with a quick example. This feeds text through standard input and limits each output line to ten columns:
printf '%s\n' 'The quick brown fox' | fold -w 10
Expected output:
The quick
brown fox
The newline after quick is the wrap fold inserted; the original newline stays a line boundary too, so a single input line can become several output lines. Every input line is handled independently, and wrapping never carries on across an existing newline.
With no -w, GNU fold uses 80 columns. This check builds a predictable 81-character line and counts the resulting output lines:
line=$(printf '%081d' 0 | tr '0' 'x')
printf '%s\n' "$line" | fold | wc -l
Expected output:
2
Tip: set an explicit width in scripts anyway. It makes the intended layout visible and stops the command silently changing behaviour if a reader ever moves it to another implementation.
The default mode cuts right at the width, even mid-word. Add -s, or --spaces, to break at a space where one is available:
printf '%s\n' 'The quick brown fox' | fold -s -w 10
For this short input the output is still:
The quick
brown fox
The option matters more once a space sits close to the limit. It is a preference, not a promise: a word longer than the requested width still gets split, and a line with no suitable space gets wrapped at the width regardless.
printf '%s\n' 'short supercalifragilistic' | fold -s -w 8
Do not treat -s as a paragraph formatter. It only chooses where fold breaks a line; it does not reflow paragraphs, understand sentences, or remove existing whitespace.
Pass one or more file names after the options. This writes a separate wrapped copy, then previews it:
fold -s -w 72 input.txt > input-wrapped.txt
sed -n '1,5p' input-wrapped.txt
Replace input.txt with a real path. If the input cannot be read, fold reports an error, but the shell may still leave an empty output file since the redirection opens first. Check the command's own status before trusting the output:
if fold -s -w 72 input.txt > input-wrapped.txt; then
printf '%s\n' 'Wrapped copy written.'
else
printf '%s\n' 'Wrapping failed; inspect the input and output files.' >&2
exit 1
fi
Recovery: when the output is disposable, just remove the named copy and rerun after fixing the input path. Never put the same path on both sides of >.
-w WIDTH measures columns by default. -b, or --bytes, switches to counting bytes instead, and the two can differ with multibyte text or wide terminal characters:
printf '%s\n' 'cafe' | fold -w 3
printf '%s\n' 'cafe' | fold -b -w 3
Both produce:
caf
e
Plain ASCII hides the difference. For non-ASCII text, byte mode is a low-level limit that can split a character's own encoded bytes, which is a poor fit for text people read or a UTF-8-aware program consumes. Use column mode for readable terminal text, and test the actual locale and data whenever a downstream format specifies bytes.
Because fold reads standard input with no file named, it slots in after anything that produces text. This wraps only the first ten lines of a log-like stream:
journalctl -n 10 --no-pager | fold -s -w 100
journalctl needs permission to read the selected journal entries on some systems; a denial there is a journal permission issue, not a fold option failure. Try a readable source first if you need to isolate the problem:
printf '%s\n' 'a line from a pipeline' | fold -w 12
To make a file explicit in a mixed pipeline, use - as its name, equivalent to standard input for the GNU command:
printf '%s\n' 'pipeline input' | fold -s -w 12 -
fold writes output; it never updates the source. Use a new output file and replace the original only after checking it, with a backup if the source matters.fold file.txt means 80 columns, not your terminal's current width. Set -w for reproducible output.-s changes preferred break points; it does not mean "wrap at every space" and will not stop a long word exceeding the requested width.-b suits an actual byte-limit requirement, and is a poor default for human-readable Unicode.fold preserves input newlines and adds more; do not point it at CSV, JSON or another structured format unless the consumer genuinely expects the extra line breaks.fold -w WIDTH rather than relying on a default.-s prefers spaces but can still split long words.-b only for an actual byte requirement.fold --version, reporting GNU coreutils 9.4 on this machine.