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
    Set maximum line width to WIDTH columns. If not specified, the default width is 80 columns.

--width=WIDTH
    Identical to -w; set maximum line width to WIDTH columns.

-b
    Count bytes rather than display columns. This can be faster but may split multi-byte characters incorrectly.

--bytes
    Identical to -b; count bytes rather than display columns.

-s
    Break lines at spaces if possible. This option attempts to avoid splitting words by breaking at the last space before the specified width.

--spaces
    Identical to -s; break lines at spaces if possible.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The fold command wraps each line of input to fit a specified maximum width. It reads from standard input or specified files and writes the formatted output to standard output. By default, if no width is explicitly provided, it wraps lines at 80 columns.

fold is a straightforward and effective utility for reformatting text files, making them more readable by ensuring they fit within a terminal window or for preparing output that requires a fixed-width format. Unlike more complex text formatters such as fmt, fold operates on a line-by-line basis: it does not re-flow entire paragraphs, join short lines, or attempt to fill lines evenly. Instead, it simply breaks a line when its length exceeds the specified maximum width, inserting a newline character. This makes it ideal for simple line wrapping without altering paragraph structure.

CAVEATS

The default width is 80 columns if no -w or --width option is provided.

When using -b (byte counting), fold does not consider multi-byte characters and might split them, leading to corrupted output for non-ASCII text. It's generally recommended to use the default column counting unless you explicitly need byte-based wrapping.

fold simply wraps individual lines; it does not re-flow entire paragraphs like fmt does. Each input line is treated independently.

Lines longer than the specified width are always broken, even if -s is used and there are no spaces to break on within the limit.

CHARACTER VS. BYTE COUNTING

By default, fold counts display columns. This means it correctly handles tab characters (expanding them to spaces) and multi-width characters (like some Asian characters) when determining line length. However, if -b is used, it counts raw bytes, which can be faster but may produce unexpected results with multi-byte encodings (e.g., UTF-8) by splitting characters in the middle.

DIFFERENCE FROM FMT

While both fold and fmt deal with text formatting, their approaches differ significantly. fold is a simpler line wrapper; it treats each input line as a distinct unit and wraps it if it exceeds the specified width. fmt, on the other hand, is designed to re-flow paragraphs, joining short lines and breaking long ones to create a more uniformly filled block of text. For simple, line-by-line wrapping, fold is often the more direct choice.

HISTORY

The fold command is a standard utility found in Unix-like operating systems. It is part of the GNU Core Utilities (coreutils), a collection of fundamental tools for manipulating files and text.

Its basic functionality has been consistent across various Unix implementations for decades, serving as a simple yet effective tool for line wrapping. Its design reflects the common need to fit text output into fixed-width terminals or printers in early computing environments.

SEE ALSO

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

Copied to clipboard