FactorPad
Build a Better Process

Linux fold Command Summary with Examples

Linux fold allows you to wrap text to a specified width.
  1. Purpose - Learn what fold is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with fold.
  4. Script - Add fold to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux fold, 100 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / fold-command.html


An ad-free and cookie-free website.


Examples of the Linux fold Command

Intermediate

Learn to format text across multiple lines at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux fold Command Summary with Examples (4:34)

Video Script

The Command and Why You Need It

Our one hundredth word, or command to memorize is fold from our category Text.

fold allows you to wrap text to a specified width.

Common Linux fold Options
-options description
--help Print help screen
-b, --bytes Use bytes to limit width
-wN, --width=N Wrap at N number of columns, default is 80

Recall that since video (tutorial) #87, we've been using a script to demonstrate workflow, and now we'll use fold at the command line and then see it in that script.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The fold command has 5 options and the argument is a file, or alternatively fold will work with standard input.

Like most commands, help is available with double-dash --help, and fold is specified with -b for bytes, and -w for words, to define the line on where you will fold it.

So why is fold an important command? Well, it'll help us wrap up the Playlist on an easy note. ;) And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Using echo let's create "a long line of text that goes on and on and never seems to end."

$ echo "a long line of text that goes on and on and never seems to end" a long line of text that goes on and on and never seems to end

Okay, there it is.

Now, to wrap this, we pipe it into fold and use the option -w15 to cut it right at the 15th character.

$ echo "a long line of text that goes on and on and never seems to end" | fold -w15 a long line of text that goes on and on and n ever seems to e nd

Good, but notice how it cuts words here? So it isn't a perfect solution.

Next, let's see fold in action with our script that we started in video (tutorial) #87. There is a lot to this, because we have building on it, and I'll provide this code for you (below) if you'd like to deconstruct it on your end.

$ funscript

This script does provide a good subset of what we've covered in roughly 6 hours together.

The current date and time: Wed Nov 23 00:43:58 PST 2016 What is your name? Paul Thank you Paul Lines and files since your last archive: 0 ./video79.script 5 ./video85a.txt 43 ./video91.txt 0 ./video75.txt 0 ./video76.txt 61 ./video85b.txt 109 total You've been busy. See video91.txt for a list of files Here are your last five files. Nice work! video75.txt video76.txt video85a.txt video85b.txt video91.txt

We started with a print of recent work and a reminder to archive. Right? If you recall from earlier scripting videos.

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: k

Next, we created a menu that catches errors. If you type something that's not on that menu it will kick right back (hitting k triggered it to loop back to the original menu.)

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 1 You selected 1. One moment please... Collecting system status Disk space and % used Size Use% 4.8G 1%

The first item will report the system status. And that runs through and presents a disk status update for you.

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 2 You selected 2. One moment please... Collecting returns data Investment modeling boot camp data downloaded

The second item will download a file from the extraordinarily useful and helpful website factorpad.com ;) and then third, we'll view the file.

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 3 You selected 3. One moment please... This is a sample of the data set Date MSFT EBAY ABT MRK Market Tbill 4/30/2003 0.056175 0.089087 0.083536 0.062066 0.072716 0.001000 5/30/2003 -0.037544 0.094070 0.096480 -0.044689 0.027079 0.000900 6/30/2003 0.041853 0.023119 -0.017733 0.096319 0.035890 0.001000 7/31/2003 0.030031 0.031731 -0.100672 -0.087036 -0.031487 0.000700 8/29/2003 0.004165 0.032805 0.026752 -0.039496 0.006057 0.000700 9/30/2003 0.048265 -0.031944 0.055831 0.013306 0.021365 0.000800 10/31/2003 -0.054537 0.042692 0.004456 -0.125840 -0.033307 0.000700 11/28/2003 -0.016450 -0.000715 0.037072 -0.082486 -0.015645 0.000700 12/31/2003 0.064566 0.156021 0.054299 0.147581 0.105617 0.000800

This takes the top 10 lines and shows you what's in that file. We use it for a boot camp video series.

And now we finish with fold.

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 4 You selected 4. Thank you for your time. Let's finish up... ...by using the fold command on this line of text:

Very good (remember we will return to our final script in the next tutorial).

I hear them out there Waves are folding, always do. Now we meet again

(Below is the fold.)

I hear them out there Waves are folding, always do. Now we meet again

A Final Tip

Okay now you know how to use fold. And you know the syntax for commands, options and arguments.

One last tip about the fold command. So for those who didn't fold, and made it through all 100 videos, congratulations and thank you. I hope you learned a lot. And for the first timers, this video provides a glimpse at our structure and an outcome. So please join us.

Okay, thanks for visiting today. I hope this was a helpful introduction to the fold command.

(Below is the code for this entire final project called funscript.)

#!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 95, we add four functions above other code # First form uses: functions { commands return } syntax # 1) Function prints system statistics function fun_stats { printf "\tCollecting system status\n" echo "Disk space and % used" df -h --output='size','pcent' /home sleep 5s return } # Second form uses: name () { command return } syntax # 2) Function collects returns text file data from factorpad.com website fun_returns () { printf "\tCollecting returns data\n" wget factorpad.com/returns.txt --quiet -O video95.txt printf "\tInvestment modeling boot camp data downloaded\n" sleep 5s return } # 3) Function uses head to show the first 10 lines of that file fun_summary () { printf "\tThis is a sample of the data set\n" head video95.txt sleep 5s return } # 4) Function completes the video Playlist with a fold command in video 100 fun_finish () { printf "\n\t...by using the fold command on this line of text:\n" sleep 3s clear echo -e " \n" echo -e "I hear them out there Waves are folding, always do. Now we meet again\n" sleep 6s clear echo -e " \n" echo -e "I hear them out there Waves are folding, always do.Now we meet again\n" | fold -w29 sleep 10s return } clear # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s # From video 89, we take input from the keyboard echo -e "What is your name? \c" read yourname echo "Thank you $yourname" # From video 90, report files and lines since last archive echo "Lines and files since your last archive:" find . -type f -cnewer "video73.tar" | xargs wc -l # From video 91, use tee to print files and show last five echo "You've been busy. See video91.txt for a list of files" echo "Here are your latest five files. Nice work!" ls *.txt | tee video91.txt | tail -5 # From video 92, use printf to start our next section sleep 4s clear printf "Ok, %s, what would you like to do next?\n" $yourname # From video 96, set up the while statement while true; do clear # From video 94, use if to set up the menu printf "\nPlease select from the following options:\n" printf "\t(1) Show system status\n" printf "\t(2) Collect returns data\n" printf "\t(3) See a sample of the data\n" printf "\t(4) Finish the Linux Essentials playlist\n" printf "\t(5) Quit\n" echo -e "\n Your choice: \c" read -n 1 yourpick if [[ "$yourpick" =~ [1-5] ]]; then if [[ $yourpick == 1 ]]; then printf "\n\tYou Selected 1. One moment please...\n" sleep 2s fun_status continue fi if [[ $yourpick == 2 ]]; then printf "\n\tYou selected 2. One moment please...\n" sleep 2s fun_returns continue fi if [[ $yourpick == 3 ]]; then printf "\n\tYou selected 3. One moment please... \n" sleep 2s fun_summary continue fi if [[ $yourpick == 4 ]]; then printf "\n\tYou selected 4.\n" sleep 1s printf "\n\tThank you for your time.\n" sleep 1s printf "\n\tLet's finish up...\n" sleep 2s fun_finish break fi if [[ $yourpick == 5 ]]; then printf "\n\tYou selected 5.\n" sleep 1s echo -e "\n\tThank you, have a nice day." sleep 1s break fi else printf "\n\tInvalid input. Please try again\n" sleep 2s continue fi # From video 96, close out the while/do/done block done

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Don't lose sight of this growing ad-free learning site and YouTube Channel. Subscribe, follow @factorpad on Twitter and join our no-spam email list for announcements of new Linux content.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / fold-command.html


linux fold command
linux fold examples
fold line
fold commmand
wrap text
line wrap
new line
line feed
carriage return
wrap by bytes
bash script
shell script
shell script example
foldline
fold command example
fold command syntax

A newly-updated free resource. Connect and refer a friend today.