fold

Updated: May 06, 2022

Fold lines (POSIX)

Syntax:

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

Runs on:

QNX Neutrino

Options:

-b
Count width in bytes rather than 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.

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 it doesn't go below zero).
tab
The current count of line width is incremented to the next count for which count modulo 8 equals 1.

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

Files:

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

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

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

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

Exit status:

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