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

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.

ADVERTISEMENTS

fold command syntax

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

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

Where,

  • -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
do
  fold -sw 65 $i > $i.output
done
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Kevin Aug 19, 2009 @ 8:00

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

  • reikred Apr 18, 2012 @ 21:15

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.