Linux and Unix fold command tutorial with examples
Tutorial on using fold, a UNIX and Linux command for folding long lines for finite widths. Examples of limiting column width, limiting by bytes and wrapping on spaces.
What is the fold command in UNIX? ¶
The fold
command in UNIX is a command line utility for folding contents of
specified files, or standard input. By default it wraps lines at a maximum width
of 80 columns. It also supports specifying the column width and wrapping by
numbers of bytes.
How to fold input ¶
To fold input using the fold
command pass a file, files or standard input to
the command. The result will be printed to standard output. The default is 80
columns but this is configurable.
fold lorum.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer at accumsan ips
um, ut sagittis dolor. Vivamus erat tellus, ullamcorper ut aliquam nec, maximus
at turpis.
This limits the width to 80 columns.
How to limit column width ¶
To change the column width from the default 80 pass the -w
option. In the
following example this is limited to 20
fold -w 20 lorum.txt
Lorem ipsum dolor si
t amet, consectetur
adipiscing elit. Int
eger at accumsan ips
um, ut sagittis dolo
r. Vivamus erat tell
us, ullamcorper ut a
liquam nec, maximus
at turpis.
How to limit width by bytes ¶
To limit the width by bytes use the -b
option. This will contstrain the width
of the output to the number of bytes specified. In the following example this is
set to 15.
fold -b15 lorum.txt
Lorem ipsum dol
or sit amet, co
nsectetur adipi
scing elit. Int
eger at accumsa
n ipsum, ut sag
ittis dolor. Vi
vamus erat tell
us, ullamcorper
ut aliquam nec
, maximus at tu
rpis.
How to wrap on spaces ¶
When using the -w
option fold will break words to wrap lines. To break on
spaces so that words are not broken to wrap lines use the -s
option.
fold -w 20 -s lorum.txt
Lorem ipsum dolor
sit amet,
consectetur
adipiscing elit.
Vestibulum sit amet
euismod purus.
Morbi suscipit
dignissim lacus, ut
ultricies justo
rhoncus at.
Further reading ¶
- fold man page
- Fold files using fold command
- fold: Wrap Text File / Line / Words To Fit in Specified Width
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Linux and Unix exit code tutorial with examples
Tutorial on using exit codes from Linux or UNIX commands. Examples of how to get the exit code of a command, how to set the exit code and how to suppress exit codes. -
Linux and Unix cat command tutorial with examples
Tutorial on using cat, a UNIX and Linux command for concatenating files and printing to standard output. Examples of showing the contents of a file, appending one file to another, and combining multiple files into one. -
Linux and Unix grep command tutorial with examples
Tutorial using grep, a UNIX and Linux command to print lines matching a pattern. Examples of finding text in a file, printing line numbers, counting the number of matches, searching recursively and ignoring case sensitivity.