Fold Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

fold is a Unix command used for making a file with long lines more readable on a limited width computer terminal by performing a line wrap.

Most Unix terminals have a default screen width of 80, and therefore reading files with long lines could get annoying. The fold command puts a line feed every X characters if it does not reach a new line before that point. If the -w argument is set, the fold command allows the user to set the maximum length of a line.

~/codeFactory$ fold --help
Usage: fold [OPTION]... [FILE]...
Wrap input lines in each FILE, writing to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes         count bytes rather than columns
  -s, --spaces        break at spaces
  -w, --width=WIDTH   use WIDTH columns instead of 80
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report fold translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/fold>
or available locally via: info '(coreutils) fold invocation'
~/codeFactory$ fold test4.txt 
fold is a Unix command used for making a file with long lines more readable on a
 limited width computer terminal by performing a line wrap.
Most Unix terminals have a default screen width of 80, and therefore reading fil
es with long lines could get annoying. The fold command puts a line feed every X
 characters if it does not reach a new line before that point. If the -w argumen
t is set, the fold command allows the user to set the maximum length of a line.

In the above example we have passed a text file to fold command and as you can see in output it wraps the line up to 80 columns.

Using fold with options:

1. -w: By using this option in fold command, we can limit the width by number of columns. By using this command we change the column width from default width of 80.

~/codeFactory$ fold -w50 test4.txt 
fold is a Unix command used for making a file with
 long lines more readable on a limited width compu
ter terminal by performing a line wrap.
Most Unix terminals have a default screen width of
 80, and therefore reading files with long lines c
ould get annoying. The fold command puts a line fe
ed every X characters if it does not reach a new l
ine before that point. If the -w argument is set, 
the fold command allows the user to set the maximu

2. -b: 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.

~/codeFactory$ fold -b50 test4.txt 
fold is a Unix command used for making a file with
 long lines more readable on a limited width compu
ter terminal by performing a line wrap.
Most Unix terminals have a default screen width of
 80, and therefore reading files with long lines c
ould get annoying. The fold command puts a line fe
ed every X characters if it does not reach a new l
ine before that point. If the -w argument is set, 
the fold command allows the user to set the maximu
m length of a line.

3. -s: 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.

~/codeFactory$ fold -w50 -s test4.txt 
fold is a Unix command used for making a file 
with long lines more readable on a limited width 
computer terminal by performing a line wrap.
Most Unix terminals have a default screen width 
of 80, and therefore reading files with long 
lines could get annoying. The fold command puts a 
line feed every X characters if it does not reach 
a new line before that point. If the -w argument 
is set, the fold command allows the user to set 
the maximum length of a line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s