Intro To 'fold' Command In Linux
2023-12-06 - By Robert Elder
I use the 'fold' command to fold long lines into shorter ones. Here, I have a file called 'lorem.txt' that contains a bunch of text, all written on a single very long line:
But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose. To do so, I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful.
Running the 'fold' command on this file produces the following output:
fold lorem.txt
But I must explain to you how all this mistaken idea of reprobating pleasure and
extolling pain arose. To do so, I will give you a complete account of the syste
m, and expound the actual teachings of the great explorer of the truth, the mast
er-builder of human happiness. No one rejects, dislikes or avoids pleasure itsel
f, because it is pleasure, but because those who do not know how to pursue pleas
ure rationally encounter consequences that are extremely painful.
As you can see form the above output, the single long line of text in this file has been broken up into multiple lines that are no longer than 80 columns.
History Of 80 Column Limit
Here, I have a computer program from 1969:
This program is written on IBM 5081 punch cards:
These punch cards have a maximum column width of 80 characters:
That's why I always force other developers to keep their source code contributions within the 80 character limit, for maximum backward compatibility:
Example Use Case Of 'fold' Command
Here, I have source code contribution that a colleague wants me to merge into our code base:
/*
Can we please just merge this in to fix the rather urgent and ongoing
customer facing issue that's happening right now on production? I
have politely addressed the variable renaming issues, whitespace
preferences, and 'refactoring' work that you requested in the last
6 iterations of this PR. If you're going to request additional
changes PLEASE list then all at once so I can fix them in one go
instead of finding new and unrelated style changes to fix in every
iteration.
I don't mean to be abrasive, and I really appreciate having you review
this code, but can we PLEASE just merge this in ASAP. This is a fix
for an EXTREMELY URGENT remote code execution that's being actively
exploited on the customer's network right now. Thank you very much
for your help and expertise in this matter.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if(argc < 2){
return EXIT_FAILURE;
}else{
/*
NOTE: 2023-11-10 Commenting out printing of raw format string to
fix remote code execution bug.
printf(arg[1]);
*/
printf("%s", argv[1]);
return EXIT_SUCCESS;
}
}
As you can see from the above source code, my colleague is being silly, because this code contains lines that are 85 columns wide. I can use the 'fold' command to fold these long lines into shorter ones:
fold urgent-changes.c
/*
Can we please just merge this in to fix the rather urgent and on
going
customer facing issue that's happening right now on production?
I
have politely addressed the variable renaming issues, whitespace
preferences, and 'refactoring' work that you requested in the la
st
6 iterations of this PR. If you're going to request additional
changes PLEASE list then all at once so I can fix them in one go
instead of finding new and unrelated style changes to fix in eve
ry
iteration.
I don't mean to be abrasive, and I really appreciate having you
review
this code, but can we PLEASE just merge this in ASAP. This is a
fix
for an EXTREMELY URGENT remote code execution that's being activ
ely
exploited on the customer's network right now. Thank you very m
uch
for your help and expertise in this matter.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if(argc < 2){
return EXIT_FAILURE;
}else{
/*
NOTE: 2023-11-10 Commenting out printing of raw format string to
fix remote code execution bug.
printf(arg[1]);
*/
printf("%s", argv[1]);
return EXIT_SUCCESS;
}
}
I can also use the '-s' flag so that the line splits only happen at whitespace positions:
fold -s urgent-changes.c
/*
Can we please just merge this in to fix the rather urgent and
ongoing
customer facing issue that's happening right now on production?
I
have politely addressed the variable renaming issues, whitespace
preferences, and 'refactoring' work that you requested in the
last
6 iterations of this PR. If you're going to request additional
changes PLEASE list then all at once so I can fix them in one go
instead of finding new and unrelated style changes to fix in
every
iteration.
I don't mean to be abrasive, and I really appreciate having you
review
this code, but can we PLEASE just merge this in ASAP. This is
a fix
for an EXTREMELY URGENT remote code execution that's being
actively
exploited on the customer's network right now. Thank you very
much
for your help and expertise in this matter.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if(argc < 2){
return EXIT_FAILURE;
}else{
/*
NOTE: 2023-11-10 Commenting out printing of raw format string
to
fix remote code execution bug.
printf(arg[1]);
*/
printf("%s", argv[1]);
return EXIT_SUCCESS;
}
}
Specifying a Custom Column Width
By default, the fold command folds lines that are longer than 80 characters, but you specify a different width with the '-w' flag:
fold -s -w 70 urgent-changes.c
fold -s -w 60 urgent-changes.c
fold -s -w 50 urgent-changes.c
Using Byte Width Instead Of Column Width
You can also use the '-b' flag to consider byte width instead of column width. This will have different behaviour for multi-column characters like tabs as illustrated by running 'fold' on the following example file 'whitespace-difference.txt':
This line is indented with tabs.
This line is indented with spaces.
Here's the output from running 'fold' without the '-b' flag:
fold -w 40 whitespace-difference.txt
This line is indented wi
th tabs.
This line is indented wi
th spaces.
And here's the output from running 'fold' with the '-b' flag:
fold -w 40 -b whitespace-difference.txt
This line is indented with tabs.
This line is indented wi
th spaces.
And that's why the 'fold' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|