Linux fold Command: Wrap Long Lines at a Fixed Width

fold wraps long input lines into multiple lines at a specified width. Unlike fmt, which naturally reflows paragraphs, it focuses on limiting line display width.

The default width is 80 columns. Using -s breaks lines at word boundaries whenever possible. Original line breaks are preserved, and only the result is sent to standard output.

Basic syntax

fold [options] [file...]

Installed implementations and options may vary depending on the distribution. Check the description on your current system with man fold.

Examples

Folding at the default width

Divides long lines based on 80 columns.

fold long-lines.txt

Specifying width as 60

Outputs to fit the terminal or document width.

fold -w 60 long-lines.txt

Word boundary first

If possible, fold at spaces to reduce breaking in the middle of words.

fold -s -w 60 paragraph.txt

Viewing output split by fixed width

fold -w 4 folds lines at the specified width. Since the string below has no spaces, a new line is created every four characters.

printf 'abcdefghij\n' | fold -w 4

Example output:

abcd
efgh
ij

To avoid breaking words whenever possible, consider the -s option. However, fold is different from fmt, which naturally rearranges paragraphs. For input containing tabs, multi-byte characters, or wide characters, you need to consider the difference between screen width and the number of bytes.

Main Options and Format

Options/Format Description
-w N Specifies the maximum width of output lines.
-s If possible, break lines at spaces.
-b Counts width based on bytes instead of screen columns.

Precautions when using

-b can break in the middle of UTF-8 characters, so do not use it carelessly on multilingual text. The actual character width and emoji display width on a terminal may vary depending on the environment. If the original lines of the data have meaning, it is safer not to use the folded result as new input data.

Frequently Asked Questions

How are fold and fmt different?

fold limits the length of each line, while fmt reflows line breaks in normal text paragraphs.

Official Documentation

The exact behavior of the option and the differences between implementations can be found in the official documentation for fold.