Text processing
fold
Wrap long lines to a fixed width, optionally breaking only at spaces.
fold [OPTION...] [FILE...]
fold breaks every input line that is longer than a width (80 by default) into several lines.
It does nothing else: short lines are left alone and lines are never joined. With -s it
breaks at the last space before the limit instead of in the middle of a word.
Use fold for hard-wrapping data or fixed-width output, such as splitting a long base64 or hex
string into lines. For reflowing prose into paragraphs, fmt is usually the better tool
because it also joins short lines back together.
Examples
Wrap at a fixed width
fold -w 20 README.md
# demo A small sample proje ct used by every exa mple on this site. It has logs, CSV and JSON data, a little source code and som e photos. ## Layout - data/ CSV, JSON a nd text files - src/ source code - logs/ application logs
Lines longer than 20 columns are cut at exactly 20, even in the middle of a word
(proje / ct). Short lines and the blank line pass through unchanged.
Break at spaces instead of inside words
fold -s -w 20 notes.txt
Meeting notes, March - Move backups to the new server - Rotate the API key before Friday - Check disk usage on web01 - TODO: write the deploy script Ask Priya about the error rate on web02.
With -s each break happens after the last space that fits, so words stay whole. The space
stays at the end of the first part, which can matter if you compare the output.
Split a long string into fixed-size chunks
echo 0123456789abcdefghijklmnopqrstuvwxyz | fold -w 8
01234567 89abcdef ghijklmn opqrstuv wxyz
A string without spaces is cut into 8-character pieces, the same idea used to wrap a long hex or base64 value.
One character per line
echo 'shell' | fold -w 1
s h e l l
A width of 1 puts each character on its own line, a quick way to count letters with sort | uniq -c.
Tabs count as several columns
printf 'a\tb\tc\n' | fold -w 10 | cat -A
a^Ib$ ^Ic$
fold measures display columns: a tab advances to the next multiple of 8, so a, a tab and
b already reach column 9 and the second tab starts a new line. -b would count bytes instead.
Trap: accented characters are counted as bytes
echo 'ééééééééé' | fold -w 4
éé éé éé éé é
In this version of GNU fold, a width counts bytes, and each é is 2 bytes in UTF-8, so
only 2 characters fit per line. Expect narrower lines for non-ASCII text.
Common pitfalls
foldwithout-scuts words in half. Add-sfor text meant to be read.foldnever joins lines, so a paragraph that was already wrapped at 40 columns stays ragged when you fold it at 72. Usefmtto reflow.GNU
foldhere counts bytes for non-ASCII text, so lines with accented or CJK characters come out shorter than the width you asked for.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
-w,-sand-bexist on both GNU and BSD/macOSfold; the long options (--width,--spaces,--bytes) are GNU only.
Options worth knowing
A selection, described in our own words. For the full list, see fold(1) or fold --help.
| Option | What it does |
|---|---|
-w, --width WIDTH | Wrap at WIDTH columns instead of 80. |
-s, --spaces | Break after the last blank before the limit, keeping words whole. |
-b, --bytes | Count bytes rather than columns, so tabs and backspaces count as one. |