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 umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount 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.

Linux shutdown Command: Schedule a Shutdown or Restart

Linux shutdown Command: Schedule a Shutdown or Restart

Learn how to schedule a Shutdown or Restart with the Linux shutdown command, including practical examples, key options, and important precautions.

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 ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cp Command: Copy Files and Directories

Linux cp Command: Copy Files and Directories

Learn how to copy files and directories with Linux cp, control overwrites, preserve attributes, handle symlinks, and verify important copies.

Linux xargs Command: Pass Standard Input as Command Arguments

Linux xargs Command: Pass Standard Input as Command Arguments

Learn how to pass Standard Input as Command Arguments with the Linux xargs command, including practical examples, key options, and important precautions.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.

Linux stat Command: Display Detailed File Metadata

Linux stat Command: Display Detailed File Metadata

Learn how to use the Linux stat command to inspect file size, permissions, ownership, timestamps, inode, and filesystem metadata, with essential options, practical examples, output interpretation, and common troubleshooting tips.