fold command in Linux with examples

The fold command in Linux is used to wrap the lines of a file at a predetermined length. Its original use was to facilitate the viewing of large files on a terminal screen, and having each line be wrapped at a certain length so everything could fit on the monitor. Back in the 1970s, before terminals and applications had word wrap functionality by default, this was very handy.

These days, trying to fit the lines of a file on your screen is never really a problem. And that’s because the command line terminals and applications we use today will automatically wrap lines instead of letting them trail off of your screen. Even so, the fold command in Linux can still prove useful every once in a while.

In this tutorial, you’ll learn how to use the fold command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the fold command on Linux
fold command in Linux with examples
fold command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software fold
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The fold command in Linux is dead simple and only comes with a couple different options you can use. Therefore, it will only take a few moments for you to learn how to use it. Check out the examples below to quickly master this command.

fold command in Linux Examples

  1. To use the fold command, simply specify the name of a file whose lines you wish to wrap. If no file is specified, fold will read from standard input.
    $ fold file.txt
    
    Default usage of the fold command in Linux
    Default usage of the fold command in Linux

    Old Unix terminals have a max length of 80 characters. Therefore, by default, the fold command will output 80 characters for each line before wrapping to the next. Check out the screenshot above to see how 80 characters looks on a modern terminal.

  2. If you would like each line to wrap at some other character limit than the default of 80, you can use the -w or --width option in your fold command. Here’s an example where we instruct fold to wrap each line at 50 characters.
    $ fold -w 50 file.txt
    

    How to wrap lines at 50 characters
    How to wrap lines at 50 characters
  3. If you want the fold command to wrap lines according to how many bytes are in each line instead of characters, you can use the -b or --bytes option in your command.
    $ fold -b file.txt
    

    Optinally, use the -w command to also specify the number of bytes.

    $ fold -w 50 -b file.txt
    
  4. If you look at the screenshots above in our previous examples, you’ll notice that sometimes a line will break in the middle of a word. This happens because the fold command has a hard limit of characters or bytes it should display before wrapping to the next line. We can use the -s or --spaces option to instruct the fold command to wrap each line at the next space. This way, there will not be a linebreak in the middle of a word.
    $ fold -s file.txt
    

    Of course, also feel free to combine additional options with the -soption.

    $ fold -s -w 50 file.txt
    
    Using the fold command with -s option to avoid mid-word line breaks
    Using the fold command with -s option to avoid mid-word line breaks

    As you can see in the screenshot above, regardless of what character limit we specified, there are only line breaks when a space occurs rather than in the middle of a word.

NOTE
You can always use the man command to read more about the fold command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts

In this tutorial, we learned all about the fold command on Linux which is a very simple command that has fallen out of widespread use due to better technology. Still, it’s a default command on Linux systems and you may run into a situation that it becomes useful.