Previous Table of Contents Next

Module 58
fold (BSD)


The external fold command is a filter that folds lines longer than 80 characters into lines less than 80 characters. You can specify the width of the line instead of using the default of 80.

If tabs are present in the input data, you may want to send the data through expand first, then pipe it into fold.


Following is the general format of the fold command.

     fold [ -wwidth ] [ file_list ]
     fold [ -width ] [ file_list ]

BSD (Berkeley)
fold [ -width ] [ file_list ]


The following option and its argument may be used to control how fold functions.

-width -wwidth Specifies the width of the output line in characters. The default is 80. The width should always be a multiple of your tab length if tabs exist within the input data. Usually tabs are set every eight spaces.


The following argument may be passed to the fold command.

file_list A list of files to be read and checked for lines longer than the specified or default width.


The fold command may interfere with underlining in the input text.


Refer to the expand command described in Module 46.


The fold command reads a list of files or uses the standard input if no files are specified as arguments. The output is written to the standard output.


You use fold to break long lines down into shorter more readable lines. This is a very useful function if you have a terminal that does not wrap its lines onto the next line. It may also be used for printers that do not know how to wrap lines longer than a certain width onto the next line. By using fold you can perform the wrapping function before the data reaches the output device.

Some terminals automatically wrap long output lines onto the next line. This looks good but may cause problems with commands that attempt to count displayed lines. If the line is longer than 80 characters, it takes up two lines of your terminal screen. Programs like pr format the output based on the number of physical lines read. Thus a line may scroll off the top of your screen or your printed output may not be aligned on the paper correctly, because the number of physical lines does not match the number of displayed lines.

The fold command may be used to correct this problem, because it breaks the long lines down into shorter physical lines, making other commands count the proper number of physical lines.


In this activity you use the fold command to wrap long lines onto the next output line. First you must create a file with lines longer than 80 characters. Begin at the shell prompt.

1.  Create a file with two or three lines of text longer than 80 columns. Type vi xwide and press Return. You may need to :set wm=0 to prevent vi from inserting automatic new-lines in your text.
2.  Type i and enter the following text; press Return only where the <CR> notation appears.
   cj> vi xwide
   This is a line of text that is longer than 80 characters. It wraps around to the next line on some
   terminals but not on others. Therefore we use fold.<CR>
   Another line just for filling up space. The fold command cuts these lines
   off at 80 characters (or there about) and displays the remainder of the line on the next output
3.  Type ZZ to save the contents and exit the editor.
4.  Type cat xwide and press Return. If five lines of text appear, your terminal wraps long lines onto the next line automatically. If only two lines appear, you have a terminal that is not wrapping the lines.
5.  Type fold xwide and press Return. Notice the lines are displayed from 1 to 80 and 81 to 132 on the next line.
6.  Type rm xwide to remove the file created in this activity.
7.  Turn to Module 105 to continue the learning sequence.

Previous Table of Contents Next