fold folds the lines of text files or the standard input so that they do not exceed the preset or specified line length. The default presetting is 80 characters per line.
The lines are folded through the insertion of a newline character so that each output line (termed "segment" in the following) does not exceed the preset or specified line length (or does not exceed the maximum number of bytes). However, lines are not folded in the middle of a character. Behavior is not predictable if number is smaller than the number of columns which may be occupied by a single input character.
If a carriage return, backspace or tab is read in the input and the option -b is not set then the input is considered to be a character:
Backspace
The current line length is reduced by one. However, it cannot assume a negative value. fold does not insert a newline character either immediately before or after a backspace.
Carriage return
The current line length is set to zero. fold does not insert a newline character either immediately before or after a carriage return.
Tab
All tabs that are read move the column position pointer to the next tab stop position. Tab stop positions are located at all column positions n where: n modulo 8 is equal to 1.
Caution!
If the input lines contain underscores then fold may react incorrectly.
Syntax
fold[ -bs][ -w width][ file...] |
No option specified fold folds the input lines so that no line exceeds a maximum width of 80 characters (default).
(bytes) The line length is calculated in bytes not column positions.
(segment) If a line segment contains a blank within the first number of column positions (or bytes) then the line is folded after the last space which fulfils the number condition. If no space which fulfils this condition exists then the -s option is ineffective for this input line.
(width) Defines the maximum width of a line, where width is the number of characters per line. width should be a multiple of 8 if tabs are present in the input line.
Name of the text file to be folded. You may name several such files, in which case the specified options will be applied to each file. file not specified: |
Locale
The following environment variables affect the execution of dd: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), and for the determination of the width in column positions each character would occupy on a constant-width font output device. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
Printing a file named notes on standard output. The following result is obtained with the cat command:
Using the fold command, you have the option of redefining the length of the output lines. The following command folds the contents of the notes file so that each line has a maximum width of 40 characters:
|
See also
pr |