Search code examples
bashunzip

Bash: How to check if a zip file contain a specified path/file.ext?


I've to handle 46 .zip file according to their internal structure.

The first usefull check to branch the elaboration is check if a specific file is presente in the .zip.

Actually i'm unzipping and testing the existence of unzipped file.

But, I ask you, is there a way to check if a file is inside a zip file without extract it entirely, using only bash commands?


Solution

  • To check for specific file, you can combine unzip -l with grep to search for that file. The command will look something like this

    unzip -l archive.zip | grep -q name_of_file && echo $?
    

    What this does is it lists all files in archive.zip, pipes them to grep which searches for name_of_file. grep exits with exit code 0 if it has find a match. -q silences the output of grep and exits immediatelly with exit code 0 when it finds a match. The echo $? will print the exit code of grep. If you want to use it in if statement, your bash script would look like this:

    unzip -l archive.zip | grep -q name_of_file;
    if [ "$?" == "0" ]
    then
        ...
    fi;