Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I recursively unzip nested ZIP files?

Given there is a secret file deep inside a nested ZIP file, i.e. a zip file inside a zip file inside a zip file, etc...

The zip files are named 1.zip, 2.zip, 3.zip, etc...

We don't know how deep the zip files are nested, but it may be thousands.

What would be the easiest way to loop through all of them up until the last one to read the secret file?

My initial approach would have been to call unzip recursively, but my Bash skills are limited. What are your ideas to solve this?

like image 956
AdHominem Avatar asked Dec 10 '15 18:12

AdHominem


People also ask

How do I unzip a recursive file in Linux?

-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory.


2 Answers

Thanks Cyrus! The master wizard Shawn J. Goff had the perfect script for this:

while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done
like image 187
AdHominem Avatar answered Sep 21 '22 05:09

AdHominem


Here's my 2 cents.

#!/bin/bash

function extract(){
  unzip $1 -d ${1/.zip/} && eval $2 && cd ${1/.zip/}
  for zip in `find . -maxdepth 1 -iname *.zip`; do
    extract $zip 'rm $1'
  done
}

extract '1.zip'
like image 28
silverdrop Avatar answered Sep 18 '22 05:09

silverdrop