Shell: How to wrap text files to fit specified width

You can use command called fold. It wrap each input line (or lines from file) to fit in specified width. Width can be specified in columns or bytes size. By default Wrap input lines in each FILE from standard input (keyboard ), writing to standard output (screen).

Syntax
fold -b size -w width file-name

Where,

  • -b : Use bytes rather than columns
  • -w : use WIDTH columns instead of 80 (which is default)

Examples

$ fold -w 40 file.txt
$ cat file.txt | grep something | fold -b 20


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
2 comments… add one
  • cym13 Nov 16, 2014 @ 13:58

    I was annoyed by the fact that the pager was always there so I wrote a small script that takes place of the pager and does not use the real pager if the output fits on the screen

    http://pastebin.com/QU88kSz8

    To use it, change the REAL_PAGER variable in the script and do:

    alias yaourt=”PAGER=smartpage yaourt –color –pager”

    (smartpage is the name of my script)

    • aclarke Aug 7, 2015 @ 6:18

      Hi

      Since you’re using ‘less’ in your script, the whole issue is already covered by the -F flag. I also find combining with -X (so it doesn’t clear screen before or after) is a nice touch.

      Personally, I use

      export LESS=”-M -e -F -i -Q -X -R”

      in my profile. Check out all those options, your mileage may vary.

      Cheers

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum