How to list content of all .zip files in a folder and grep for a specific file?

Solution 1:

Using zipinfo is a fine solution here. However, in general whenever you want to apply a command to a list of files and the command doesn’t accept a list of files, you can use a for loop:

for file in *.zip; do
    unzip -l "$file"
done \
| grep "\.zip\|setup"

If the file you are searching for has spaces in it like: your file, in the grep regular expression you need to escape every space with a backslash like grep "\.zip\|your\ file".

Solution 2:

You can use zipinfo. It is included in the default Ubuntu installation. Check the manual page for more info.

For example, to look for a pattern setup in a bunch of zip files in current directory, use this command:

find ./ -iname *zip 2> /dev/null -print0 | xargs -0 zipinfo | grep setup

Solution 3:

To list the files in a zip archive you can use the following command.

unzip -l

To grep a compressed archive you should use the compressed archive utilities built to work with that type of archive format.

For zip archives:

zipgrep --help  
usage: zipgrep [egrep_options] pattern zipfile [members...]
Uses unzip and egrep to search the zip members for a string or pattern.

For tar archives:

zgrep --help
Usage: /bin/zgrep [OPTION]... [-e] PATTERN [FILE]...
Look for instances of PATTERN in the input FILEs, using their
uncompressed contents if they are compressed.

OPTIONs are the same as for 'grep'.

There are a few other tools that work with archives as well. You can pipe the out put into grep to do the same thing.

zcat
zcat my.archive.zip | grep "some text"

Or you can use the search functionality of these tools

zless
zmore