As we continue with our GNU Core Utilities series we come to the fold command. The Linux fold command is a designed to wrap long lines of text to fit in a specified terminal width. It is a simple little utility that doesn't have many options. But as with many Linux text utilities, it serves an important role for people who mostly work on the command line.

Using the Fold Command to Wrap Long Lines of Text

The default behavior of the fold command is to wrap (or fold) long lines of text at 80 columns. This is usually enough to make a large text file much easier to read on the command line. It can be used on a file, multiple files or even standard input (STDIN). Let's take a look at how it works.

For this example I am using some text from a short story written by Ted Chiang called Exhalation. However, this could easily be a README document or license information for software. Either way we have a lot of text with long lines.

[savona@putor ~]$ cat exhalation.txt 
It has long been said that air (which others call argon) is the source of life. This is not in fact the case, and I engrave these words to describe how I came to understand the true source of life and, as a corollary, the means by which life will one day end.

The contents of the file above is one long line and it will stretch the entire length of the terminal.

You can wrap the contents of a file by simply passing the filename as an argument to the fold command. By default, fold will wrap the text at the 80th column.

[savona@putor ~]$ fold exhalation.txt 
It has long been said that air (which others call argon) is the source of life. 
This is not in fact the case, and I engrave these words to describe how I came t
o understand the true source of life and, as a corollary, the means by which lif
e will one day end.

This example may not translate well onto a browser windows since it is impossible for me to know what size screen and at what resolution you will be viewing it. I made this small animated gif to help visualize how it works.

Animated gif showing how the Linux fold command works.

Fold Command Options

Wrap Text Lines at Spaces Only

In the above example, you may have noticed that the fold command indiscriminately wrapped the text at 80 columns. Sometimes the 80th column was in the middle of a word. This makes the text hard to read. The -s (--spaces) option ensures the fold command wraps the line at the space nearest the 80th column.

[savona@putor ~]$ fold -s exhalation.txt 
It has long been said that air (which others call argon) is the source of life. 
This is not in fact the case, and I engrave these words to describe how I came 
to understand the true source of life and, as a corollary, the means by which 
life will one day end.

NOTE: The -s option can be used in conjunction with any other option.

Set Specific Width for Output

You can also use the -w (--width=WIDTH) option to specify how many columns you want displayed. Here we use "-w 40" to limit the width to 40 columns and include the -s option to ensure we only wrap at spaces.

[savona@putor ~]$ fold -w 40 -s exhalation.txt 
It has long been said that air (which 
others call argon) is the source of 
life. This is not in fact the case, and 
I engrave these words to describe how I 
came to understand the true source of 
life and, as a corollary, the means by 
which life will one day end.

Wrap Text at x Number of Characters

Using the -c (--characters) option allows you to specify the number of characters to display before the lines wraps instead of columns.

[savona@putor ~]$ fold -c44 exhalation.txt 
It has long been said that air (which others
 call argon) is the source of life. This is 
not in fact the case, and I engrave these wo
rds to describe how I came to understand the
 true source of life and, as a corollary, th
e means by which life will one day end.

NOTE: Remember, spaces are a valid character.

Limit Width by Bytes

You can use bytes as the limiting factor instead of columns or characters by using the -b (--bytes) option. Here we use 22 bytes.

[savona@putor ~]$ fold -b22 exhalation.txt 
It has long been said 
that air (which others
 call argon) is the so
urce of life. This is 
not in fact the case, 
and I engrave these wo
rds to describe how I 
came to understand the
 true source of life a
nd, as a corollary, th
e means by which life 
will one day end.

Conclusion

The fold command can be a real gem when you come across that unwrapped text file that you need to read. It makes working with long lines of text much easier. In this article we showed you how to wrap the lines of any input using the fold command.

Resources and Links