fold

fold lines (POSIX)

Syntax:


fold [-bs] [-w width] [file...]

Options:

-b
Count width in bytes rather then column positions. If -b is specified, carriage-return, backspace and tab are treated as normal characters. Only newline remains special.
-s
If a segment of a line contains a blank within the first width column positions, break the line after the last such blank meeting the width constraints (i.e. avoid breaking the line in the middle of a word).
-w width
Specify the maximum line length. The default is 80 characters.

Examples:

Fold the file myfile.txt to a maximum line width of 80 characters, making the line breaks on the last word boundary (white space) before the width was exceeded and write the results to the standard output:


   fold -s myfile.txt

Fold the file myfile.txt to a width of 40 characters, and treat all characters except newline as occupying one character (byte) position and write the results to the standard output:


   fold -bw40 myfile.txt

Description:

The fold filter folds lines in files by breaking up lines that have a width in excess of 80 or, if specified, the width in the command-line -w option. Lines are folded by the insertion of a newline character. All output is written to the standard output.

In all cases, the current count of line width is reset to zero when a newline character is encountered in the input. In addition, when the -b option is not specified, the following characters have a special meaning when encountered in the input:

carriage-return
The current count of line width is set to zero.
backspace
The current count of line width is decremented by one (but not going below zero).
tab
The current count of line width is incremented to the next count for which count modulo 8 equals 1.

Exit status:

>0
An error occurred.
0
All files were processed successfully.

Files:

If no files are specified on the command line, fold reads lines to be folded from the standard input until EOF is reached.

fold writes all folded output from all input files to the standard output.

If an error occurs, a diagnostic message will be written to the standard error.

fold will read lines from the text files specified on the command line. If the -b is specified, the input files need not be text files.

See also: