How to Wrap Long Lines of Text in Linux Using the fold Command

Managing Terminal Line Widths with the fold Command

When processing unstructured text files, inspecting dense server log strings, reading unformatted API JSON payloads, or preparing raw markdown documents for terminal printing in Linux, excessively long lines frequently run off the right edge of your display screen. While modern terminal emulators support visual soft wrapping, soft wraps do not insert genuine line breaks into the underlying character stream. This causes downstream command pipelines, printers, and text filters to treat huge run-on sentences as a single massive line. Linux provides a classic POSIX standard utility called fold, part of GNU Coreutils, specifically designed to wrap long lines of text to a specified column width.

Basic Syntax and Default Column Width

The standard syntax for the fold command is concise:

fold [options] [file...]

By default, if you invoke fold on a file without specifying any flags, it wraps text lines to a standard historical terminal width of 80 columns (characters):

fold server_output.log

You can also pipe stream output from standard input (stdin) directly into fold:

cat unformatted_notes.txt | fold

Setting Custom Column Widths with -w

Depending on your display layout, terminal split-pane dimensions, or document specifications, 80 characters may not suit your needs. You can define an exact maximum column width using the -w (or --width) flag:

fold -w 50 document.txt

This breaks every line after exactly 50 characters by inserting a hard newline (\n) character into the stream. For wider, modern high-resolution displays, you can expand width to 120 characters:

fold -w 120 /var/log/audit.log

Breaking at Word Boundaries Using the -s Flag

By default, the fold utility counts characters rigidly. When it reaches its column limit, it will split a word right down the middle, placing the first half of a word on one line and the remainder on the next. This makes prose and human-readable documentation difficult to read.

To ensure line breaks only occur at natural word boundaries (spaces and tabs), pair the width parameter with the -s (or --spaces) flag:

fold -s -w 72 README.md

With -s enabled, fold looks back to the last whitespace delimiter before the 72-character threshold and breaks the line cleanly, preserving complete, unbroken words.

Handling Character Counts vs Raw Byte Offsets with -b

On modern systems utilizing UTF-8 character encoding, multi-byte international characters, emojis, or accent glyphs consume more than one physical byte on disk. By default, fold counts screen columns. If you are preparing legacy data streams or fixed-width binary protocols where hardware registers require rigid byte limits rather than visual characters, append the -b (or --bytes) switch:

fold -b -w 64 payload.dat

This enforces wrap points strictly based on byte volume rather than typographical column width.

Redirecting Wrapped Output and Pipeline Integration

Because fold outputs its transformed content to standard output (stdout), you can redirect formatted text directly to a new file or chain it with other Coreutils tools:

Saving Formatted Text to File

fold -s -w 60 raw_draft.txt > formatted_manuscript.txt

Piping into Text Processing Utilities

To number lines cleanly after wrapping:

fold -s -w 70 input.txt | nl -ba

To inspect wrapped files in a pager without horizontal scrolling:

fold -s -w 80 large_report.txt | less

Comparing fold with fmt and pr

Linux Coreutils includes several related text-formatting tools, each suited to distinct text transformations:

  • fold: Fast, lightweight line wrapping strictly bounded by character or byte thresholds. Ideal for logs, source code streams, and fixed-width data blocks.
  • fmt: A paragraph-level text formatter that combines short lines and splits long lines while re-justifying whitespace. Better for essay paragraphs than code logs.
  • pr: Prepares text files specifically for paginated physical printing, inserting page headers, footers, and column splits.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.