How to gzip all files in all sub-directories in bash How to gzip all files in all sub-directories in bash linux linux

How to gzip all files in all sub-directories in bash


No need for loops or anything more than find and gzip:

find . -type f ! -name '*.gz' -exec gzip "{}" \;

This finds all regular files in and below the current directory whose names don't end with the .gz extension (that is, all files that are not already compressed). It invokes gzip on each file individually.


Edit, based on comment from user unknown:

The curly braces ({}) are replaced with the filename, which is passed directly, as a single word, to the command following -exec as you can see here:

$ touch foo$ touch "bar baz"$ touch xyzzy$ find . -exec echo {} \;./foo./bar baz./xyzzy


I'd prefer gzip -r ./ which does the same thing but is shorter.


find . -type f | while read file; do gzip "$file"; done