How to use the fold command on linux

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.

Estimated reading time: 2 minutes

Table of contents

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.

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

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.

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.

Further reading

  • fold man page
  • Fold files using fold command
  • fold: Wrap Text File / Line / Words To Fit in Specified Width

Have an update or suggestion for this article? You can edit it here and send me a pull request.

  • UNIX
  • Linux

Recent Posts

About the author

How to use the fold command on linux

George Ornbo is a Software Engineer based in Buckinghamshire, England.