fold Command

Purpose

Folds long lines for fixed-width output devices.

Syntax

fold [ -b ] [ -s ] [ -w Width ] [ File... ]

Description

The fold command is a filter that folds long lines for a finite-width output device. By default, the command folds the contents of standard input, breaking the lines to a line width of 80 (eighty). You can also specify one or more files as input to the command.

The fold command inserts a new-line character in the input lines so that each output line is as wide as possible without exceeding the value specified by the Width parameter. If the -b flag is specified, line width is counted in bytes. If the -b flag is not specified:

  • Width is counted in columns as determined by the LC_CTYPE environment variable.
  • A backspace character decreases the length of an output line by 1.
  • A tab character advances to the next column where the column position is 1 plus a multiple of 8.

The fold command accepts -w Width values in multiples of 8 if the file contains tabs. To use other width values when the file contains tabs, use the expand command before using the fold command.

Notes:
  1. The fold command may affect any underlining that is present.
  2. The fold command does not insert new-line characters in the middle of multibyte characters even when the -b flag is used.

Flags

Item Description
-b Counts Width in bytes. The default is to count in columns.
-s Breaks the line after the rightmost blank within the Width limit, if an output line segment contains any blank characters. The default is to break lines so each output line segment is as wide as possible.
-w Width Specifies the maximum line width as the value of the Width variable. The default is 80.

Exit Status

This command returns the following exit values:

Item Description
0 All input files processed successfully.
>0 An error occurred.

Examples

To fold the lines of a file named longlines into width 72 (seventy-two), enter:

fold -w 72 longlines

Files

Item Description
/usr/bin/fold Contains the fold command.