Linux – How to unzip multiple zip files into a single directory structure (e.g. Google Drive folder export)

command linelinuxzip

Assume you started with an uncompressed directory A, with the following structure:

$ tree A
A
└── inner_dir
    ├── file1.txt
    └── file2.txt

1 directory, 2 files

Now assume you've been given two zip files 1.zip and 2.zip derived from A. 1.zip contains only file1.txt, and 2.zip contains only file2.txt:

# -l flag simply lists file contents

$ unzip -l 1.zip 
Archive:  1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-09-30 21:49   A/
        0  2017-09-30 22:27   A/inner_dir/
        0  2017-09-30 21:49   A/inner_dir/file1.txt
---------                     -------
        0                     3 files

$ unzip -l 2.zip 
Archive:  2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-09-30 21:49   A/
        0  2017-09-30 22:27   A/inner_dir/
        0  2017-09-30 22:27   A/inner_dir/file2.txt
---------                     -------
        0                     3 files

What command can I use to programmatically reconstitute the original unzipped directory A from the combination of 1.zip and 2.zip?

Context: when exporting a large directory from Google Drive, the contents are split across a set of 2 or more zip files. Each zip file contains some subset of the files, which when taken collectively, reconstitute the original directory. Which files are contains in which zip appears to be more or less random. I'm looking for a solution which generalizes to n > 1 zip files, not just the special case of exactly 2 files.

Best Answer

Move the set of files you want to unzip to a dedicated directory, and then use unzip:

$ unzip '*.zip' -d combined

Archive:  1.zip
   creating: combined/A/
   creating: combined/A/inner_dir/
 extracting: combined/A/inner_dir/file1.txt  

Archive:  2.zip
 extracting: combined/A/inner_dir/file2.txt  

2 archives were successfully processed.

Check:

$ tree combined
combined
└── A
    └── inner_dir
        ├── file1.txt
        └── file2.txt

2 directories, 2 files
Related Question