Unix – How to Search Inside Zip Files

findsearchshellzip

I have 100s of directories and within those I have a few zip files. Now there are images named abc.jpg in those zip files. The zip files may be in any folder or in any subfolder so its difficult to extract them all in one place.

I just want to collect those image files. Is this possible?

Best Answer

I once needed something similar to find class files in a bunch of zip files. Here it is:

#!/bin/bash

function process() {
while read line; do
    if [[ "$line" =~ ^Archive:\s*(.*) ]] ; then
        ar="${BASH_REMATCH[1]}"
        #echo "$ar"
    else
        if [[ "$line" =~ \s*([^ ]*abc\.jpg)$ ]] ; then
            echo "${ar}: ${BASH_REMATCH[1]}"
        fi
    fi
done
}


find . -iname '*.zip' -exec unzip -l '{}' \; | process

Now you only need to add one line to extract the files and maybe move them. I'm not sure exactly what you want to do, so I'll leave that to you.

Related Question