Text and processing
The fold command
Wraps long lines so they fit a fixed width, breaking them at the exact column or at the previous space. Useful for showing text in narrow terminals or in plain-text emails. It also helps prepare text before printing it or pasting it into forms with a limited width.
Most useful options
-w NMaximum width of each line. The default is 80 columns-sBreaks at the last space within the width, instead of in the middle of a word-bCounts bytes instead of columns. An accented letter takes two bytes in UTF-8, so the result changesCopy-ready examples
$ fold -w 20 texto.txtbreaks each line at 20 columns. Without -s, it splits words in half.Example output
The quick brown fox jumps over the lazy dog near the river b ank.
$ fold -s -w 20 texto.txtthe same, but it breaks at the last space. Words stay whole.Example output
The quick brown fox jumps over the lazy dog near the river bank.
$ printf 'abcdefghij\n' | fold -w 4useful to test the width: the last chunk can be shorter.Example output
abcd efgh ij
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| fold: nonexist: No such file or directory | The file does not exist or the path is misspelled. | Check the name with ls, or pipe the text through standard input. |
| fold: invalid number of columns: 'abc' | The value given to -w is not a whole number. | Write digits only, for example -w 60. |
| fold: invalid number of columns: '0': Numerical result out of range | A width of zero columns makes no sense. | Use a width of at least 1. |
Typical mistakes
- Without -s, fold splits words in half, which is hard to read in prose. For normal text always add -s.
- With -b it counts bytes, not characters: an accent takes two, so the cut comes earlier than expected. Remove -b if you want the width in columns.
- fold does not change the text content, it only inserts line breaks. Lines that already fit are left as they are.
Differences from similar commands
FAQ
How do I wrap text at 80 columns without breaking words?
With fold -s -w 80 file.txt. Without -s, the cut falls exactly at column 80, even in the middle of a word.
Does fold modify the original file?
No. It only writes the result to standard output. Redirect to a new file if you want to keep it.
Why does fold cut accented letters badly?
You are probably using -b, which counts bytes. Remove -b so the width is measured in columns.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course