fold: Wrap Text File / Line / Words To Fit in Specified Width

in Categories CentOS, Debian Linux, fedora linux, Gentoo Linux, Howto, Linux, programming, RedHat/Fedora Linux, Suse Linux, Sys admin, Tips, UNIX last updated November 25, 2008

fold is really nifty command line utility to make a text file word wrap. This is useful for large number of text files processing. There is no need to write a perl / python code or use a word processor.

fold command syntax

fold -sw {COUNT} {input.txt} > {output.txt}

Wrap input lines in each input.txt, writing to standard output.txt.


  • -s: break at spaces
  • -w: {COUNT} use COUN} as WIDTH columns instead of default 80.

For example, following command will wrap input.txt at 60 width columns:
$ fold -sw 60 input.txt > output.txt

A large number of files can be processed using for shell loop:

for i in *.txt
  fold -sw 65 $i > $i.output

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 2 comments/add one below):

2 comment

  1. Ah, this is great for grepping through a corpus of longlines-text, gzcat corpus.txt.gz | fold | grep foo

  2. I wish fold had an option no to fold lines unless they contain whitespace that provide a natural folding point.

    The -s option helps some, but fold will still spli a long URL, for example, when I might not want that.

    Perhaps a perl-based fold script is in order….

    Have a question? Post it on our forum!