Linux Shell Recursively Remove Whitespace Lines From All Files
25th July 2020
This is just a quick post to outline how to remove the lines with only white space from all files in a directory tree. I found another method to do this with the application I am using but I wanted to keep this handy one liner in my back pocket for future reference.
The incantation goes like this.
find _site/ -type f -name '*.html' -print0 | xargs -0 sed -i '/^[[:space:]]*$/d' $1;
find _site/- Recursively traverse the _site directory tree
-type f -name '*.html'- Mathing all files of the html file type
-print0- Print the full file name to standard output
| xargs -0- Pipe the result to xargs which passes the filenames to sed one by one.
sed -i- Use sed to replace pattern in place with no backup
'/^[[:space:]]*$/d'- Matching lines in the file with only whitespace
$1- In the file fed in from xargs
This command will alter the files in place without creating a backup. Use at your own risk.
In this post we covered how to remove the white space from all files in a
directory tree using