Linux Command Reference ยท Text Processing
Linux fold command
Reference for the Linux fold command: wrap lines at a fixed width, break at spaces with -s, count bytes or columns, and how fold differs from fmt.
fold wraps each input line so that no output line is longer than a given width (80 columns by default). It is a purely mechanical tool: it cuts lines at the width limit, optionally backing up to the last space, and it does not join short lines or touch paragraph structure.
Use it to make long log lines fit a terminal, to produce fixed-width blocks such as base64 output, or to prepare plain-text email. When you want proper paragraph reflow, use fmt instead.
fold [OPTION]... [FILE]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
fold [-bs] [-w WIDTH] [FILE]...
| Option | Description |
|---|---|
-w N, --width=N |
Maximum line width (default 80). -N is an old shorthand. |
-s |
Break at the last space before the limit instead of in the middle of a word. |
-b |
Count bytes instead of display columns. |
Returns: 0 on success, non-zero if a file cannot be read.
Usage notes
- Cuts mid-word by default. Add
-sfor readable text. If a single word is longer than the width,-sstill has to cut it. - Lines are never joined. Short lines stay short and blank lines stay blank. Use
fmtto reflow paragraphs. - Columns, not characters: by default a tab counts as moving to the next multiple of 8 and a backspace subtracts one. A carriage return resets the column. For plain ASCII text this makes no difference.
- Multi-byte text: with
-b, a UTF-8 character can be split in half and produce invalid bytes. Without-b, GNUfoldcounts columns by characters in a UTF-8 locale, but combining marks and wide characters may be measured oddly. - Trailing spaces: with
-sthe space at the break point stays on the upper line, so lines can end in a space. Pipe throughsed 's/ *$//'if that matters. - Handy for fixed-width chunks:
fold -w 64is how base64 data is shaped into lines, andfold -w 1puts each character on its own line. - Terminal width: use
fold -s -w "$(tput cols)"to fit the current terminal.
Related
fmt,cut,column,tr- Lessons: Viewing text, Text pipelines