Linux Shell Recursively Remove Whitespace Lines From All Files
Published: 2019-05-29
Intro
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.
Dark Magic
The incantation goes like this.
cmd
find _site/ -type f -name '*.html' -print0 | xargs -0 sed -i '/^[[:space:]]*$/d' $1;Breakdown
- 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
Warning
This command will alter the files in place without creating a backup. Use at your own risk.
Outro
In this post we covered how to remove the white space from all files in a directory tree using find and sed