fold command in Linux with examples
Last Updated :
05 Jul, 2023
The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command.
Syntax of `fold` command in Linux
fold [OPTION] [FILE]
We can specify options that will control line folding behavior and provide a file as input for processing. Consider if no file is specified, `fold` read from standard input.
Practical implementation of the `fold` command in Linux
fold GfG.txt
fold GfG.txt
In the above example, we have passed a text file named GfG to the fold command and as you can see in the output it wraps the line up to 80 columns.
Different options available in the `fold` command
Wrapping Lines with the `-w` option in `fold` command
By using this option in the fold command, we can limit the width by the number of columns. Using this command, we change the column width from the default width 80.
Syntax:
fold -w[n] GfG.txt
fold -w[n] GfG.txt
In the above example, we wrap the lines of GfG.txt to a width of 60 columns.
Handling Nonprinting Characters with the `-b` option
This option of fold command is used to limit the width of the output by the number of bytes rather than the number of columns. By using this we can enforce the width of the output to the number of bytes.
Syntax:
fold -b[n] GfG.txt
fold -b[n] GfG.txt
In the above example, we have limited the output width of the file to 40 bytes and the command breaks the output at 40 bytes.
Truncating Lines with the `-s` option
This option is used to break the lines on spaces so that words are not broken. If a segment of the line contains a blank character within the first width column positions, break the line after the last such blank character meeting the width constraints.
Syntax:
fold -w[n] -s GfG.txt
fold -w[n] -s GfG.txt
If you compare the above example with the previous one, you will see that the lines only break at spaces, whereas, in other examples, the output is displayed in such a way that some of the words are broken between the lines.
Conclusion
In this article we have discussed `fold` command in Linux which offers a convenient way to wrap or truncate line in the text files. We have discussed some common options such as `-w`, `-s` and `-b. Overall, we can say that this versatility empowers us to improve readability and fit content within specific constraints.
Similar Reads
expr command in Linux with examples The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions.What is the 'expr' Command?expr stands for "expression" and
3 min read
factor command in Linux with examples The factor command in Linux is used to print the prime factors of the given numbers, either given from the command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntaxfactor [NUMBER]You can provide one or more numbers as argu
2 min read
fc Command in Linux with Examples As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read
fc-cache command in Linux with Examples The fc-cache command in Linux scans the font directories and build font cache for applications which use fontconfig for their font handling. When no arguments are passed fc-cache scans each directory for font files readable by FreeType. A cache is created which contains properties of each font and t
2 min read
fc-list command in Linux with examples fc-list command is a part of the fontconfig system. It is used to list the available fonts and font styles. Using the format option, the list of all fonts can be filtered and sorted out. Moreover, multiple parameters can be passed to it after a colon symbol (:) to restrict the information that is be
3 min read
fdisk command in Linux with examples The 'fdisk' command in Linux, short for "format disk," is a powerful, dialog-driven utility used for creating and manipulating the disk partition table. Essential for system administrators and users looking to manage their hard driveâs structure, 'fdisk' supports a range of operations such as creati
5 min read
fg command in Linux with examples The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it.Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently runn
3 min read
fgrep command in Linux with examples The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
4 min read
file command in Linux with examples The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
3 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read