39

I'd like to know what is the command to use to gunzip all files in a target directory recursively? I tried to use the unzip command but it didn't work.

I tried the command from Unzip all zip files in a target folder?

85

gunzip has -r option. From man gunzip :

   -r --recursive
          Travel  the directory structure recursively. If any of the 
file names specified on the command line are directories, gzip 
will descend into the directory and compress all the files it finds
there (or decompress them in  the  case  of gunzip ).

So, if you want to gunzip all compressed files (gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack) inside the directory /foo/bar and all its subdirectories :

gunzip -r /foo/bar

This will handle file names with spaces too.

0
19

Using the commands below. Replace <path_of_your_zips> with the path to your ZIP files and <out> with your destination folder:

  • For GZ files

    find <path_of_your_zips> -type f -name "*.gz" -exec tar xf {} -C <out> \;
    

    or

    find <path_of_your_zips> -type f -name "*.gz" -print0 | xargs -0 -I{} tar xf {} -C <out>
    
  • For ZIP files

    find <path_of_your_zips> -type f -name "*.zip" -exec unzip {} -d <out> \;
    

    or

    find <path_of_your_zips> -type f -name "*.zip" -print0 | xargs -0 -I{} unzip {} -d <out>
    
6
  • 1
    improved, Now yes :)
    – A.B.
    May 7 '15 at 12:42
  • 2
    Why not -exec like find . -type f -name "*.gz" -exec tar xzf {} -C <out> \;? May 8 '15 at 4:01
  • 1
    work only for tarball files, not for only g-zipped ones
    – Covich
    Dec 12 '17 at 19:24
  • 1
    gzipped files which are not tar files give this error: tar: This does not look like a tar archive Oct 12 '18 at 19:50
  • 1
    This method will flatten any folder hierarchy, and is unnecessarily complex. gunzip -r . should be the accepted answer here. Sep 4 '19 at 14:18

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.