Alex Gordon
Alex Gordon

Reputation: 60721

Finding a file within recursive directory of zip files

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?

Upvotes: 10

Views: 18428

Answers (3)

laktak
laktak

Reputation: 60003

I wrote zfind for the same reason. In addition to zip in can also search inside tar, rar and 7z archives with SQL-where syntax.

zfind 'name="myLostfile"'

This will actually match all files with that name in your directory. To just match it inside a zip:

zfind 'name="myLostfile" and archive="zip"'

To match partial names use like (SQL syntax):

zfind 'name like "%myLostfile%" and archive="zip"'

You can also match date and size ranges.

Upvotes: 1

Eric Renouf
Eric Renouf

Reputation: 14500

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.

Upvotes: 10

David C. Rankin
David C. Rankin

Reputation: 84551

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

Upvotes: 11

Related Questions