Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

unzip

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?

like image 800
realtebo Avatar asked Sep 04 '17 09:09

realtebo


People also ask

How to check if a file and directory exist in Bash?

You can now use bash to check if a file and directory exist. You can also create simple test scripts as you now understand the functions of a basic bash script file. Next, you should check out our post on Bash Function.

How do I check if a file is empty in Bash?

Bash/Shell: Check if file exists (is empty or not empty) To check if the file exists and if it is empty or if it has some content then we use " -s " attribute Check if file exists and empty or not empty using double brackets [ [..]] Check if file exists and empty or not empty using double brackets [..]

How to check if a file exists or not in Linux?

To check if the file exists and if it is empty or if it has some content then we use " -s " attribute Check if file exists and empty or not empty using double brackets [ [..]] Check if file exists and empty or not empty using double brackets [..]

What is Zipzip command in Linux?

zip is a command-line utility that helps you create Zip archives. The zip command takes the following syntax form: To create a Zip archive in a specific directory, the user needs to have write permissions on that directory. Zip files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command.


2 Answers

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;
like image 55
campovski Avatar answered Nov 15 '22 22:11

campovski


on command-line you can try:

$ if [[ `unzip -Z1 audio.zip | grep help.mp3` ]];then echo 'yes';fi

if the help.mp3 is found the output would be yes

see:

help [[  

on bash

like image 43
Shakiba Moshiri Avatar answered Nov 15 '22 21:11

Shakiba Moshiri