Finding a file within recursive directory of zip files

14,045

Solution 1

You can omit using find for single-level (or recursive in bash 4 with globstar) searches of .zip files using a for loop approach:

for i in *.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done

for recursive searching in bash 4:

shopt -s globstar
for i in **/*.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done

Solution 2

You can use xargs to process the output of find or you can do something like the following:

find . -type f -name '*zip' -exec sh -c 'unzip -l "{}" | grep -q myLostfile' \; -print

which will start searching in . for files that match *zip then will run unzip -ls on each and search for your filename. If that filename is found it will print the name of the zip file that matched it.

Share:
14,045
Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on July 07, 2022

Comments

  • Alex Gordon
    Alex Gordon almost 2 years

    I have an entire directory structure with zip files. I would like to:

    1. Traverse the entire directory structure recursively grabbing all the zip files
    2. I would like to find a specific file "*myLostFile.ext" within one of these zip files.

    What I have tried
    1. I know that I can list files recursively pretty easily:

    find myLostfile -type f
    

    2. I know that I can list files inside zip archives:

    unzip -ls myfilename.zip
    

    How do I find a specific file within a directory structure of zip files?