I want to iterate among sub directories of my current location and gzip each file seperately. For zipping files in a directory, I use
for file in *; do gzip "$file"; done
but this can just work on current directory and not the sub directories of the current directory. How can I rewrite the above statements so that It also zips the files in all subdirectories?
No need for loops or anything more than
find
andgzip
: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 invokesgzip
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:I'd prefer
gzip -r ./
which does the same thing but is shorter.