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?
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.
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 [..]
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 [..]
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.
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With