Linux and Unix fold command tutorial with examples

Tutorial on using fold, a UNIX and Linux command for folding long lines for finite widths. Examples of limiting column width, limiting by bytes and wrapping on spaces.

The UNIX and Linux fold command
The UNIX and Linux fold command

What is the fold command in UNIX?

The fold command in UNIX is a command line utility for folding contents of specified files, or standard input. By default it wraps lines at a maximum width of 80 columns. It also supports specifying the column width and wrapping by numbers of bytes.

How to fold input

To fold input using the fold command pass a file, files or standard input to the command. The result will be printed to standard output. The default is 80 columns but this is configurable.

fold lorum.txt

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer at accumsan ips
    um, ut sagittis dolor. Vivamus erat tellus, ullamcorper ut aliquam nec, maximus
    at turpis.

This limits the width to 80 columns.

How to limit column width

To change the column width from the default 80 pass the -w option. In the following example this is limited to 20

fold -w 20 lorum.txt
Lorem ipsum dolor si
t amet, consectetur
adipiscing elit. Int
eger at accumsan ips
um, ut sagittis dolo
r. Vivamus erat tell
us, ullamcorper ut a
liquam nec, maximus
at turpis.

How to limit width by bytes

To limit the width by bytes use the -b option. This will contstrain the width of the output to the number of bytes specified. In the following example this is set to 15.

fold -b15 lorum.txt
Lorem ipsum dol
or sit amet, co
nsectetur adipi
scing elit. Int
eger at accumsa
n ipsum, ut sag
ittis dolor. Vi
vamus erat tell
us, ullamcorper
 ut aliquam nec
, maximus at tu
rpis.

How to wrap on spaces

When using the -w option fold will break words to wrap lines. To break on spaces so that words are not broken to wrap lines use the -s option.

 fold -w 20 -s lorum.txt
Lorem ipsum dolor
sit amet,
consectetur
adipiscing elit.
Vestibulum sit amet
euismod purus.
Morbi suscipit
dignissim lacus, ut
ultricies justo
rhoncus at.

Further reading

Tags

Can you help make this article better? You can edit it here and send me a pull request.

See Also