Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the fold command. It allows you to limit the length of lines when displaying a text file by “folding” long lines into smaller pieces and, of course, you can save the shortened lines into another file. Here’s a file with a very long line:
The file just shown has only 2 lines and the second is wrapped around a couple times to the width of the terminal window.
By default, when we use the fold command, it breaks lines at 80 characters. So, if I fold it, I see this:
Notice that some words (like “listen”) are broken into two pieces. To limit lines to 80 characters but prevent breaking words in this way, add a -s to ensure that the command breaks lines on spaces:
To both break lines on spaces and limit lines to 40 characters, you can use a command like this:
That’s your Linux tip for the fold command.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.