How to Use the Linux fold Command to Wrap Long Lines of Text

When working with raw server logs, API JSON responses, or poorly formatted database dumps in the Linux terminal, you frequently encounter strings of text that are thousands of characters long. Standard terminal emulators will soft-wrap this text visually, but if you need to redirect this output into a neat, 80-column text file for documentation, soft-wrapping is useless. To permanently hard-wrap text to a specific width, Linux provides the simple yet highly effective fold command.

What Does the fold Command Do?

The fold command takes an input stream of text and forcibly inserts a newline character (a line break) at a specific character count. By default, if you do not specify a width, fold will break lines exactly at the 80th character. This ensures that the resulting text file will display perfectly on any screen, in any text editor, without relying on the editor’s visual wrapping settings.

Basic Text Wrapping

You can use fold directly on a text file. Imagine you have a file named server_dump.txt containing a single, continuous line of 5,000 characters.

  1. Open your terminal application.
  2. To output the file with the default 80-character wrap, type:

fold server_dump.txt

The command will instantly print the text to your screen, chopped into neat, uniform 80-character blocks. The original file remains untouched. If you want to save this newly formatted text, redirect the output to a new file using the > operator:

fold server_dump.txt > formatted_dump.txt

Customising the Wrap Width

For modern displays or specific code documentation requirements, an 80-character limit might be too narrow. You can specify an exact character width using the -w (width) flag.

To wrap the text exactly at 120 characters, use the following syntax:

fold -w 120 server_dump.txt

This is exceptionally useful when piping output from other commands. For example, if you retrieve a massive Base64 encoded string from an API using curl, you can pipe it directly into fold to make it readable before it hits your screen:

curl https://api.example.com/key | fold -w 60

Wrapping by Words Instead of Characters

The most common complaint about the basic fold command is that it cuts words in half. If the 80th character happens to be in the middle of the word “configuration,” it will slice the word apart, leaving “configur” on one line and “ation” on the next. This makes reading natural language documents incredibly frustrating.

To fix this, you must use the -s (spaces) flag. This flag forces the fold command to look for the last available space before the character limit, ensuring that words remain whole.

fold -s -w 80 article_draft.txt

With this command, Linux will wrap the text as close to 80 characters as possible without ever breaking a word across a line break, resulting in a perfectly formatted, readable document.

Get the best tech tips delivered straight to your inbox.

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