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.

Syntax

fold [opciones] [archivo...]Try it in the terminal →

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 changes

Copy-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

MessageCauseFix
fold: nonexist: No such file or directoryThe 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 rangeA 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

  • fmt: reflows whole paragraphs, joining and regrouping lines to fit. fold only breaks lines that are too long.
  • cut: extracts specific columns or fields. It does not adjust the width of the text.
  • wc -L: prints the length of the longest line. It helps you pick a reasonable -w value.

Related 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