fold

text processingLinux/Unix
The fold command is one of the most frequently used commands in Linux/Unix-like operating systems. fold Wrap input lines to fit specified width

Quick Reference

Command Name:

fold

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

fold [options] [arguments]

Common Use Cases

    Syntax

    fold [options] [file...]

    Options

    Option Description
    -b, --bytes Count bytes rather than columns (important for multi-byte characters)
    -s, --spaces Break at spaces (avoid breaking words)
    -w, --width=WIDTH Set output line width to WIDTH columns (default is 80)
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the fold command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    fold file.txt
    Wrap each input line from file.txt to fit in 80 columns (default width).
    fold -w 60 file.txt
    Wrap each line to fit in 60 columns.
    fold -s file.txt
    Wrap at spaces (avoid breaking words).

    Advanced Examples:

    fold -w 40 -s file.txt Wrap at 40 columns, breaking at spaces to avoid splitting words. echo "This is a very long line that will be wrapped by the fold command" | fold -w 20 Pipe output to fold and wrap at 20 columns. fold -b -w 20 file.txt Count bytes instead of columns (important for multi-byte characters). fold -w 80 file1.txt file2.txt Wrap lines in multiple input files. fold -w 80 file.txt > wrapped.txt Save the wrapped output to a new file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The fold command is a text processing utility that wraps each line of input files to fit a specified width, creating line breaks as needed. Key features of fold: 1. Line Width Control: fold allows you to specify the maximum width of output lines (default is 80 columns) using the -w option. 2. Word-Sensitive Wrapping: With the -s option, fold breaks lines at spaces, avoiding breaking words in the middle, which improves readability. 3. Byte vs. Column Handling: The -b option allows counting bytes instead of columns, which is important when dealing with multi-byte characters like those in UTF-8 or other encodings. 4. Multiple File Support: fold can process multiple input files in a single command, applying the same wrapping rules to each. 5. Standard I/O Support: Like most Unix utilities, fold works with standard input when no files are specified, allowing it to be used in command pipelines. 6. Preservation of Line Endings: fold maintains the original line endings of the input text while adding new line breaks as needed. 7. No Content Modification: Unlike text formatters like fmt, fold only adds line breaks and doesn't modify whitespace or join short lines. Common use cases for fold include preparing text for fixed-width displays, formatting output for printing with specific width constraints, ensuring log file lines don't exceed certain length, and improving readability of long lines in text files.

    Related Commands

    These commands are frequently used alongside fold or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the fold command works in different scenarios.

    $ fold
    View All Commands