fold Command in Linux



The Linux fold command wraps each input line to fit the specified width. It takes the text and breaks it into chunks to make it more readable in the terminal.

The fold command makes the files with long lines more comprehensible on a terminal with limited width. By default, it wraps lines by 80 columns. However, it can be modified.

Table of Contents

Here is a comprehensive guide to the options available with the fold command −

Syntax of fold Command

The syntax of the Linux fold command is as follows −

fold [options] [file]

The [options] field in the above command is used to specify options to modify the behavior of the command. The [file] field is used to specify the file that needs to be printed in the standard output.

Options of fold Command

The options of the fold command are listed below −

Flags Options Description
-b --bytes It is used to set width by bytes rather than columns
-c --characters It is used to set width by characters instead of columns
-s --spaces It is used to break the line in spaces
-w width --width=width It used to specify a custom width instead of the default 80
--help It is used to display command help
--version It is used to display the command version

Examples of fold Command in Linux

This section demonstrates the usage of the fold command in Linux with examples −

Wrapping Lines

To wrap lines of a text file with long lines by 80 columns, use the fold command with the file name.

fold file.txt
fold Command in Linux1

Wrapping Lines with Custom Width

By default, the fold command wraps lines by 80 columns. To change the default value, use the -w or --width options −

fold -w 50 file.txt
fold --width=50 file.txt
fold Command in Linux2

Breaking Lines in Spaces

By default, the fold command breaks the lines in the middle of the word. However, to prevent this behavior, use the -s or --spaces options −

fold -s -w 50 file.txt
fold --spaces --width=50 file.txt
fold Command in Linux3

Making the Format Permanent

To make the modified format of the lines permanent, redirect the standard output to another file.

fold -w 50 file.txt > newfile.txt
fold --width=50 file.txt > newfile.txt

Wrapping Lines of Standard Input

To wrap a long line of standard input, use the fold command in the following way −

echo "Linux is a versatile and open-source operating system widely used in various industries, from personal computing to enterprise servers" | fold -s -w 40
fold Command in Linux4

Reading Log with the fold Command

The log can be tough to read in the terminal. To make them readable, the fold command can be a handy tool.

sudo journalctl | fold -s -w 50
fold Command in Linux5

Conclusion

The fold command in Linux is used to wrap long lines to make them more readable on a screen with limited width. By default, it wraps each line by 80 columns. However, it can be changed. If the text file is not specified then the standard input is wrapped.

In this tutorial, we explained the fold command, its syntax, options, and usage in Linux through examples.

Advertisements