Home > Text Processing & Search > fold

fold: Text Wrapping and Line Limiting

The fold command is used to wrap each line of input text to a specified width. It is useful for making long text files more readable or for processing text in environments with specific width limitations.

Overview

fold reads text from files or standard input and wraps each line to a specified width (defaulting to 80 columns), sending the output to standard output. This enhances readability when viewing long lines in a terminal or helps reformat text to meet specific layout requirements.

Key Features

  • Wraps lines to a specified width
  • Processes by bytes or columns
  • Wraps at spaces

Key Options

These are the main options that control the core functionality of the fold command.

Line Wrapping Control

Generated command:

Try combining the commands.

Description:

`fold` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Practical examples of using the fold command.

Default Wrapping (80 Columns)

fold example.txt

Wraps the content of a file to the default 80-column width.

Wrapping to a Specific Width (40 Columns)

fold -w 40 example.txt

Wraps the content of a file to a 40-column width.

Using with Pipes

echo "This is a very long text line. I will use the fold command to make this line shorter." | fold -w 30

Passes a long text line through a pipe to fold, wrapping it to a 30-column width.

Wrapping at Spaces

echo "This is a very long text line. I will use the fold command to make this line shorter." | fold -s -w 30

Wraps lines to a 30-column width at spaces, preventing words from being cut.

Wrapping by Bytes

echo "한글 테스트" | fold -b -w 5

Wraps text to a 5-byte width. Multi-byte characters like Korean may be broken.

Tips & Considerations

Useful tips and points to consider when using the fold command.

Leveraging Pipes

  • fold is very useful for processing text in real-time by connecting it with the output of other commands using pipes (|). For example, you can use it like `cat long_log.txt | fold -w 70`.

Bytes (-b) vs. Columns (-w)

  • The `-b` option calculates width in bytes, so using it with text containing multi-byte characters like Korean may result in broken characters. For general readability, it is recommended to use the `-w` (column-based) option.

Preserving Words (-s)

  • The `-s` option helps preserve the meaning of text by attempting to wrap lines at spaces rather than cutting words in the middle.

Same category commands