3

I have a large collection of gz files. I want to extract them all. Here is what I was trying to do:

find . | grep .gz | gunzip

However, gunzip does not accept list of files from stdinput. How can I decompress them all(in place)?

6

If what you are after is to call gunzip on every file with a name ending in .gz anywhere within your current directory, this should do it:

find . -type f -name '*.gz' exec gunzip {} +

The more general way to turn what is on standard input into arguments to a command is to use xargs, but there are a few gotchas to be aware of with that command.

3

Perhaps a better approach would be to use find's -name and -exec options? This way grep would be unnecessary, and it could hand each matching file to gunzip, individually.

This would look something like:

find . -name '*.gz' -exec gunzip '{}' +

All files within the $CWD (including within subdirectories), with names ending in .gz would be handed to gunzip for decompression. Also, the decompressed files would end up in the $CWD without taking further measures.

  • Good tip, unless the set of files found would result in a command line length error. – roens Dec 15 '15 at 23:19
  • 1
    It wouldn't. If the limit is reached, find will break up the command, just like xargs does but minus the quoting issues. – Gilles 'SO- stop being evil' Dec 16 '15 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.