Linux fold Command: Wrap Long Lines at a Fixed Width

fold wraps long input lines into multiple lines at a specified width. Unlike fmt, which naturally reflows paragraphs, it focuses on limiting line display width.

What is the fold command?

The default width is 80 columns. Using -s breaks lines at word boundaries whenever possible. Original line breaks are preserved, and only the result is sent to standard output.

Basic syntax

fold [options] [file...]

Installed implementations and options may vary depending on the distribution. Check the description on your current system with man fold.

Examples

Folding at the default width

Divides long lines based on 80 columns.

fold long-lines.txt

Specifying width as 60

Outputs to fit the terminal or document width.

fold -w 60 long-lines.txt

Word boundary first

If possible, fold at spaces to reduce breaking in the middle of words.

fold -s -w 60 paragraph.txt

Viewing output split by fixed width

fold -w 4 folds lines at the specified width. Since the string below has no spaces, a new line is created every four characters.

printf 'abcdefghij\n' | fold -w 4

Example output:

abcd
efgh
ij

To avoid breaking words whenever possible, consider the -s option. However, fold is different from fmt, which naturally rearranges paragraphs. For input containing tabs, multi-byte characters, or wide characters, you need to consider the difference between screen width and the number of bytes.

Main Options and Format

Options/Format Description
-w N Specifies the maximum width of output lines.
-s If possible, break lines at spaces.
-b Counts width based on bytes instead of screen columns.

Precautions when using

-b can break in the middle of UTF-8 characters, so do not use it carelessly on multilingual text. The actual character width and emoji display width on a terminal may vary depending on the environment. If the original lines of the data have meaning, it is safer not to use the folded result as new input data.

Frequently Asked Questions

How are fold and fmt different?

fold limits the length of each line, while fmt reflows line breaks in normal text paragraphs.

Official Documentation

The exact behavior of the option and the differences between implementations can be found in the official documentation for fold.

More in This Category
Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux Tutorial / File Permissions and Safe sudo Use

Linux Tutorial / File Permissions and Safe sudo Use

Learn to read file and directory permissions, practice chmod safely, and understand when to use sudo and how to diagnose access errors.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux nohup Command: Keep a Command Running After Logout

Linux nohup Command: Keep a Command Running After Logout

Learn how to use the Linux nohup command to keep a command running after the terminal closes or the user logs out, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ssh-copy-id Command: Install a Public Key on a Remote Account

Linux ssh-copy-id Command: Install a Public Key on a Remote Account

Learn how to install a Public Key on a Remote Account with the Linux ssh-copy-id command, including practical examples, key options, and important precautions.

Linux fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk command, including practical examples, key options, and important precautions.