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

-b, --bytes=WIDTH
    Fold lines based on byte count rather than column width. Break after WIDTH bytes.

-s, --spaces
    Break at spaces. Attempt to break lines at whitespace characters (spaces or tabs).

-w, --width=WIDTH
    Set the width to WIDTH (default 80).

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The fold command is a Unix utility that folds lines from input files to fit a specified width. It reads data from either the specified files or standard input, and writes the folded output to standard output. The primary purpose of fold is to format text for terminals or other output devices with limited line lengths, preventing lines from exceeding the display's capability. This can improve readability when dealing with long lines in plain text files.

The command can break lines at specific column positions or based on byte counts. When dealing with multibyte characters, it intelligently handles them to prevent splitting characters mid-sequence (usually when using `-b` option). By default, fold breaks lines based on display column width. The utility is part of the GNU coreutils package, commonly found in most Linux distributions.

CAVEATS

The default width is 80 characters. When folding based on byte counts with multibyte characters, using the `-b` option can cause issues if the width value cuts a character in half; this is avoided when using default column width. The command may not correctly handle all Unicode characters.

CHARACTER ENCODING

fold relies on the current locale setting to correctly handle character encoding, especially when dealing with UTF-8 or other multibyte encodings. Inconsistent locale settings may lead to unexpected results when using the `-b` option.

HISTORY

The fold utility is a standard part of the coreutils package, originating from the GNU project. Its inclusion reflects the long-standing need for text formatting tools in Unix-like environments. It is designed for simple text formatting and manipulation.

SEE ALSO

fmt(1), pr(1)

Copied to clipboard