How to Wrap Text Columns Using the fold Command in Linux

When you use the Linux terminal to read a text file that was originally created on a system without strict margin limits, the lines of text will often stretch infinitely to the right side of the screen. If your terminal window is narrow, the text will violently wrap back around in unpredictable places, completely breaking the readability of the document. To artificially force a long string of text into a clean, highly readable column format, you must use the fold command.

How the fold Command Works

The fold command is a dedicated text-formatting utility. It reads a file line by line, mathematically counts the number of characters, and forcefully injects a hard line break (Enter) the exact millisecond the text hits a predefined margin width.

By default, if you run the command without any specific flags, it enforces a strict 80-character margin (the historical standard for old IBM punch cards and legacy terminal monitors).

fold messy_document.txt

The command will instantly output the entire document to your screen, ensuring that no single line of text is ever wider than exactly 80 characters.

How to Set Custom Width Margins

If you are trying to format text so it fits perfectly on a modern smartphone screen, an 80-character width might still be too wide. You can define a highly specific, custom margin width by using the -w (width) flag.

To forcefully wrap the text after exactly 50 characters, run:

fold -w 50 messy_document.txt

Wrapping by Words Instead of Characters

The default behavior of the fold command is incredibly aggressive. If the 50-character limit happens to land directly in the middle of a long word (like “administration”), the command will violently chop the word in half, putting “admin” on the first line and “istration” on the second line.

To prevent the command from destroying words, you must append the -s (spaces) flag. This instructs the algorithm to intelligently look for the nearest blank space before the margin limit, ensuring that line breaks only ever occur cleanly between whole words.

fold -s -w 50 messy_document.txt

This command produces perfectly formatted, highly readable columns of text that respect natural word boundaries, making it an essential tool for sanitizing old text files before printing them or embedding them into bash scripts.

Get the best tech tips delivered straight to your inbox.

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