In Linux, the fold command can wrap text that displays wider than a monitor's width. Credit: wragg / Getty Images The Linux fold takes lines of text and breaks them into chunks based on the arguments that you provide. With no arguments, fold will break lines at 80 characters. The first example below uses a single-line text file that includes indications of character positions. First, we count the number of characters and lines in the file using the wc -l and wc -l command: $ wc -c wide_text 251 wide_text $ wc -l wide_text 1 wide_text So, this file has 251 characters (including a carriage return) and a single line of text. Next, we display the file using the cat command: $ cat wide_text .........1.........2.........3.........4.........5.........6.........7.........8.........9........10........11........12........13........14........15........16........17........18........19........20........21........22........23........24........25 Then we use the fold command to break the file into separate lines: $ fold wide_text .........1.........2.........3.........4.........5.........6.........7.........8 .........9........10........11........12........13........14........15........16 ........17........18........19........20........21........22........23........24 ........25 In the example above, a 250-character line is passed to the fold command, and it generates four lines of text – three with 80 characters and a fourth with the remaining 10. The fold command does, however, provide the -c (characters) option to break lines into larger or smaller chunks. In the example below, we break the wide_text file contents into 50-character chunks. $ fold -c50 wide_text .........1.........2.........3.........4.........5 .........6.........7.........8.........9........10 ........11........12........13........14........15 ........16........17........18........19........20 ........21........22........23........24........25 Folding the text and creating a new file in one step is easy: $ fold -c50 wide_text folded_text $ wc -l folded_text; wc -c folded_text 5 folded_text 255 folded_text As you can see, the folded_text file contains five lines of text and 255 characters. You can also fold lines by bytes instead of characters using the -b option when this is useful. In some cases, this changes the output. Now let’s look at a more realistic example in which we break a text file with very long lines into more reasonable lines for viewing. First, to view how many characters are in each line in a file, we use a script like the one shown below. The two if commands at the top check that a file name has been provided and that the specified file exists. The while command at the bottom displays the length of each line in the file. #!/bin/bash # check for an argument if [ $# -eq 0 ]; then echo -n "file: " read file else file=$1 fi # make sure the file exists or exit if [ ! -f $file ]; then echo "ERROR: cannot find $file" exit 1 fi $ count characters in each line while read line; do echo $line | wc -c done The script displays the length of each line in the file, clearly more than would display well in a terminal window, so it's a good candidate for folding. $ chars-per-line Lorem_ipsum | head -6 7 137 149 153 154 35 If we break this file into 80 characters lines, we may see something like the example below using the centuries old "Lorem ipsum" text: $ fold Lorem_ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i ncididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostru d exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aut e irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat n ulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. As you can see from the example above, breaking text files at a fixed position won't likely be a good choice since it would probably break some words into two pieces. When we add the -s (break at spaces) option, we get a much more useful rendering of the text: $ fold -s Lorem_ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Note: Text files will be a little larger when folded with the fold command because they will contain more newline (carriage return) characters. Wrap-Up The fold command can be very useful when you want to turn paragraphs of text into separate lines that don't have as many characters and break on word boundaries. This can make viewing a large text a lot easier. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe