fold Command: Tutorial & Examples

Adjust the width of output lines to improve readability

The fold command in Linux is a simple yet powerful utility that wraps input lines to a specified width by inserting line breaks. This is especially useful on servers and virtual machines where terminal width is limited or when processing text files and command outputs that contain very long lines. By controlling line width, fold helps improve readability and prevents horizontal scrolling in the terminal.

What Fold Does

The fold command reads text from one or more files or from standard input and inserts line breaks so that no output line exceeds a given width. By default, this width is 80 columns. If a line is longer than the specified width, fold breaks it into multiple lines of the desired length. Fold does not reformat or justify text; it simply wraps lines at fixed widths or at spaces when requested.

Why Fold Is Important

On Linux servers, command outputs or log files often contain extremely long lines that can wrap awkwardly or require horizontal scrolling in the terminal. This can make reading or processing such output difficult. Using fold helps:

  • Improve terminal readability by breaking long lines.
  • Prepare text for printing or displaying on narrow-width devices.
  • Format output for further processing by other tools that expect limited line widths.
  • Avoid issues when viewing logs or configuration files remotely over slow or limited bandwidth connections.

Thus, fold is an essential tool for sysadmins, developers, and anyone working regularly with text on command line environments.

How Fold Works

Fold reads the input line by line and tracks the length of each line in columns or bytes. When the length exceeds the specified width, it inserts a newline character. By default, fold counts columns and breaks lines strictly at the width limit, potentially splitting words. When the -s option is used, fold tries to break lines at the last space character before the width limit to avoid splitting words. The -b option makes fold count bytes instead of columns, which can affect output when dealing with multibyte or wide characters.

For example:

echo "This is a very long line of text that will be wrapped by fold at the default 80 columns." | fold

Output:

This is a very long line of text that will be wrapped by fold at the default 80
columns.

Here, fold inserts a line break to ensure no output line is longer than 80 columns.

How To Use Fold

The basic syntax of fold is:

fold [OPTIONS] [FILE]...
  • If no FILE is specified or if FILE is -, fold reads from standard input.
  • The most common option is -w WIDTH or --width=WIDTH to specify the maximum line width.
  • Use -s or --spaces to break lines at spaces instead of arbitrary columns.
  • Use -b or --bytes to count bytes instead of columns (useful for fixed-width byte processing).

Examples:

fold -w 50 mytextfile.txt

This wraps lines in mytextfile.txt so that none exceed 50 columns.

cat logfile.log | fold -s -w 80

Reads from the logfile and wraps lines at spaces to 80 columns for easier viewing.

Common Command Line Parameters

  • -w WIDTH, --width=WIDTH
    Set the maximum line width (number of columns). Defaults to 80.

  • -b, --bytes
    Count bytes instead of columns when wrapping lines. Useful when text contains multibyte characters and you want to count raw bytes.

  • -s, --spaces
    Break lines at the last space character before the width limit to avoid splitting words.

  • -h, --help
    Display help and exit.

  • --version
    Show version information.

Practical Examples Using Fold

Example 1: Wrapping Output from a Command

ps aux | fold -w 100

Wraps the output of ps aux so that each line fits within 100 columns.

Output snippet:

root         1  0.0  0.1 169084  1048 ?        Ss   Jun01   0:03 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    Jun01   0:00 [kthreadd]
...

Example 2: Wrapping Text Input With Word Boundaries

echo "The quick brown fox jumps over the lazy dog multiple times in a row to test fold." | fold -s -w 20

Output:

The quick brown fox
jumps over the lazy
dog multiple times
in a row to test
fold.

Example 3: Using Fold With Bytes Counting

printf "áéíóú\n" | fold -b -w 3

Output:

áé
íó
ú

Here, since each accented character may be multiple bytes, counting bytes changes how fold wraps lines.

Example 4: Reading From Standard Input

fold -w 40

Then type or paste text, press Ctrl+D to end input, fold will wrap input lines at 40 columns.

Potential Problems and Pitfalls

  • Using a very small width (e.g., 1 or 2) produces output that is hard to read due to excessive line breaks.
  • Using -b when your text contains multibyte characters (like UTF-8) can split characters improperly, causing garbled output.
  • Using -s with input that has no spaces results in fold falling back to breaking at width without word boundaries.
  • Fold does not reformat text or handle indentation; it only inserts line breaks.
  • Lines longer than the terminal width might cause display issues if fold output is piped incorrectly.
  • When folding log files, ensure that timestamps or multiline entries are not broken improperly for easier parsing.

Possible Alternatives Or Related Commands

  • fmt: Reformats paragraphs to fit within a given width, attempting to maintain word boundaries and paragraphs.
  • pr: Converts text files for printing, supporting page headers, margins, and column formatting.
  • cut: Extracts sections from each line of input but does not wrap lines.
  • less: Allows viewing long files interactively with line wrapping options.

Use fold when you need simple line wrapping without reformatting paragraphs.

Tips And Best Practices

  • Use fold in pipelines to improve readability of command outputs on narrow terminals.
  • Combine fold with -s to avoid splitting words.
  • Avoid using -b unless you have a specific need to count bytes exactly.
  • To view long log files, consider fold combined with less or cat for paging.
  • Use fold in scripts when preparing text for fixed-width displays or printers.
  • Remember fold is not a text formatter; for paragraph formatting, use fmt.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA