I am able to gzip every file in a directory (mydirectory
) using the command:
gzip --suffix .gz --recursive mydirectory
But...
I don't want the images (.ico, .jpg, .png, .gif) and PDFs (.pdf) files within the directory to be gzipped.
And those files that are gzipped have the
.gz
extension. I don't want that. For example, I'd like the gzippedindex.html
file beindex.html
itself, and NOTindex.html.gz
.
So, how do I do this, optimally/efficiently?
Here's how I am doing this now (a pretty lengthy process).
Compress all files in mydirectory
but don't delete/replace original files:
cd ~/mydirectory
find . -type f | \
while read -r x
do
gzip -c -9 "$x" > "$x.gz"
done
Remove all gzipped image and PDF files:
find . -type f -iname "*.ico.gz" -exec rm -f {} \;
find . -type f -iname "*.jpg.gz" -exec rm -f {} \;
find . -type f -iname "*.png.gz" -exec rm -f {} \;
find . -type f -iname "*.gif.gz" -exec rm -f {} \;
find . -type f -iname "*.pdf.gz" -exec rm -f {} \;
Rename the existing gzipped files, essentially removing the .gz
extension from their name:
cd
for f in `find mydirectory -iname '*.gz'`; do
mv $f ${f%.gz}
done
What we pretty much have now are gzipped text files (.html, .xml, .css, .js) without .gz extension, and uncompressed/un-gzipped images and PDFs.