Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have numerous zip archives, each of which contains a number of zip archives. What is the best way to recursively extract all files contained within this zip archive and its child zip archives, that aren't zip archives themselves?

share|improve this question
    
what do you mean by extracting things that are not zip files? you wan to copy them to another place? –  phunehehe Nov 27 '10 at 12:30
    
I don't find your requirements clear. I find Shawn J. Goff and my interpretation about equally likely. Could you clarify? –  Gilles Nov 27 '10 at 13:30
    
@Gilles: Sorry, yeah it was a bit unclear. I changed it a bit, hopefully its more clear now. –  oadams Nov 28 '10 at 0:56
    
I was going to post an answer, but I believe it should go as a comment: Nested Archives increase the space you need! You probably mean the Zip file format, not just gzip. every zip file is already compressed, compressing them again, just creates more overhead, effectively increasing needed space. –  polemon Nov 29 '10 at 3:44
    
Yeah, I didn't do it :P. Unfortunately I'm subjected to this bizarre way of distributing files. –  oadams Nov 29 '10 at 5:56

6 Answers 6

up vote 5 down vote accepted

This will extract all the zip files into the current directory, excluding any zipfiles contained within them.

find . -type f -name '*.zip' -exec unzip -- '{}' -x '*.zip' \;

Although this extracts the contents to the current directory, not all files will end up strictly in this directory since the contents may include subdirectories.

If you actually wanted all the files strictly in the current directory, you can run

find . -type f -mindepth 2 -exec mv -- '{}' . \;

Note: this will clobber files if there are two with the same name in different directories.

If you want to recursively extract all the zip files and the zips contained within, the following extracts all the zip files in the current directory and all the zips contained within them to the current directory.

while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done
share|improve this answer

This perl script will extract each .zip file into its own subdirectory. Run the script more than once to handle nested zip files. It does not delete .zip files after extraction but you could make that change by adding an unlink() call.

#!/usr/bin/perl -w

# This script unzips all .zip files it finds in the current directory
# and all subdirectories.  Contents are extracted into a subdirectory
# named after the zip file (eg. a.zip is extracted into a/).
# Run the script multiple times until all nested zip files are
# extracted.  This is public domain software.

use strict;
use Cwd;

sub process_zip {
    my $file = shift || die;
    (my $dir = $file) =~ s,/[^/]+$,,;
    (my $bare_file = $file);
    $bare_file =~ s,.*/,,;
    my $file_nopath = $bare_file;
    $bare_file =~ s,\.zip$,,;
    my $old_dir = getcwd();
    chdir($dir) or die "Could not chdir from '$old_dir' to '$dir': $!";
    if (-d $bare_file) {
        chdir($old_dir);
        # assume zip already extracted
        return;
    }
    mkdir($bare_file);
    chdir($bare_file);
    system("unzip '../$file_nopath'");
    chdir($old_dir);
}

my $cmd = "find . -name '*.zip'";
open(my $fh, "$cmd |") or die "Error running '$cmd': $!";
while(<$fh>) {
    chomp;
    process_zip($_);
}
share|improve this answer

You'll want to be careful automatically unzipping zip files inside of zip files:

http://research.swtch.com/2010/03/zip-files-all-way-down.html

It's possible to concoct a zip file that produces a zip file as output, which produces a zip file as output, etc etc etc. That is, you can make a zip file that's a fixed oint of "unzip" the program.

Also, I seem to recall people making zip files that would "explode", that is a very small zip file would unzip to multi-gigabytes of output. This is a facet of the method of compression.

share|improve this answer

The easiest way is to use atool: http://www.nongnu.org/atool/ It is a very good script that use zip, unzip, tar, rar etc. programs to extract any archive.

Use atool -x package_name.zip to unzip them all or if you wanna use it in directory with many zip files use simple for loop: for f in *; do atool -x $f; fi (you will have to cd into desired directory with zip files before you use this).

share|improve this answer
    
atool's behaviour here doesn't differ significantly from unzip I'd say, it doesn't recursively extract ZIP files either. –  Thomas Themel Nov 29 '10 at 7:50
    
@Thomas Themel: Are you sure that it doesn't recursively extract ZIP files? It can extract from deb files tar.gz recurisvely but i have no time atm to test it with nested zip archives :\ –  user2648 Dec 1 '10 at 22:58

As far as I understand, you have zip archives that themselves contain zip archives, and you would like to unzip nested zips whenever one is extracted.

Here's a bash 4 script that unzips all zips in the current directory and its subdirectories recursively, removes each zip file after it has been unzipped, and keeps going as long as there are zip files. A zip file in a subdirectory is extracted relative to that subdirectory. Warning: untested, make a backup of the original files before trying it out or replace rm by moving the zip file outside the directory tree.

shopt -s globstar nullglob
while set -- **/*.zip; [ $# -ge 1 ] do
  for z; do
    ( cd -- "$(dirname "$z")" &&
      z=${z##*/} &&
      unzip -- "$z" &&
      rm -- "$z"
    )
  done
done

The script will also work in zsh if you replace the shopt line with setopt nullglob.

Here's a portable equivalent. The termination condition is a little complicated because find does not spontaneously return a status to indicate whether it has found any files. Warning: as above.

while [ -n "$(find . -type f -name '*.zip' -exec sh -c '
    cd "${z%/*}" &&
    z=${z##*/} &&
    unzip -- "$z" 1>&2 &&
    rm -- "$z" &&
    echo 1
')" ]; do :; done
share|improve this answer

unzip doesn't do this, because the UNIX way is to do one thing and do that well, not handle all crazy special cases in every tool. Thus, you need to use the shell (which does the job of "tieing things together" well). This makes it a programming question, and since ALL possible programming questions have been answered on StackOverflow, here: How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

share|improve this answer
1  
I would definitely not call "using the shell" a programming question, and "shell scripting" is listed in the FAQ as on-topic –  Michael Mrozek Nov 27 '10 at 18:02
    
I was not meaning to imply that it was off-topic here at all, I just wanted to justify why it's on-topic at StackOverflow. –  Thomas Themel Nov 29 '10 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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