fold command in Linux with examples

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 but this is configurable. To fold input using the fold command pass a file or standard input to the command.

Syntax:

$ fold [OPTION]... [FILE]...

Using fold command:

$ fold GfG.txt

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

Different options of fold command :

  • -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.

    Syntax:

    $ fold -w[n] GfG.txt

    In the above example, we wrap the lines of GfG.txt to a width of 60 columns.

  • -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.

    Syntax:

    $ 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.

  • -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.

    Syntax:

    $ 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.