LinuxCommandLibrary

fold

Wrap lines to fit a specified width

TLDR

Wrap each line to default width (80 characters)

$ fold [path/to/file]
copy

Wrap each line to width "30"
$ fold -w30 [path/to/file]
copy

Wrap each line to width "5" and break the line at spaces (puts each space separated word in a new line, words with length > 5 are wrapped)
$ fold -w5 -s [path/to/file]
copy

SYNOPSIS

fold [OPTION]... [FILE]...

PARAMETERS

-w, --width=WIDTH
    Use WIDTH columns instead of 80 (must be positive integer)

-s, --spaces
    Break at first whitespace within WIDTH columns, not mid-word

--help
    Display this help and exit

--version
    Output version information and exit

DESCRIPTION

fold is a Unix utility that wraps each input line to fit a specified column width, useful for formatting text output to match terminal sizes or fixed-width displays. By default, it uses 80 columns and breaks lines at exactly that point, even mid-word, preserving original line breaks but folding longer ones.

It processes input from files listed on the command line or standard input if none provided, writing wrapped results to standard output. Key options customize behavior: -w WIDTH sets the target width (positive integer), and -s prefers breaks at whitespace for better readability, mimicking simple word wrap.

Unlike advanced tools like fmt, fold does not rearrange words across lines or justify text; it acts strictly per input line. Ideal for log processing, code listings, or preparing plain text for email/printing. Handles multibyte characters in GNU version but assumes single-byte by default.

CAVEATS

Breaks mid-word without -s; ignores short lines; no joining of lines; assumes 1 byte per character unless multibyte locale.

EXAMPLES

fold -w 40 file.txt
Wrap every line to 40 columns.

fold -s -w 60 < input.txt
Read stdin, wrap at spaces to 60 columns.

HISTORY

First appeared in 1BSD (1977); standardized in POSIX.1-2008; GNU coreutils version adds multibyte support.

SEE ALSO

fmt(1), pr(1), expand(1), cut(1)

Copied to clipboard