Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I verify if the zip file is empty or not ? I cannot use if -s in bash as the zip file is not always zero in size. if the zip file has empty directories in it, I want to delete the file else extract the file.

share|improve this question

migrated from stackoverflow.com Jun 19 '12 at 17:05

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

up vote 2 down vote accepted

If you have the Info-ZIP tools installed (zip, unzip, zipinfo) you can use zipinfo on the zip file and just send the output to /dev/null. The status will be 0 if the file is not empty.


if zipinfo foo.zip > /dev/null; then
    echo not empty
    echo empty
share|improve this answer
this was giving me the same return code but I tweaked the above a little bit. add | grep ^- to look for files and then delete the file if $? is 1. Thank you Laurence –  nlrreddy Jun 18 '12 at 23:53
+1 I'd never heard of zipinfo before. @nlrreddy If Laurence solved your issue, feel free to accept his answer by clicking the checkmark on the left. –  Tim Pote Jun 19 '12 at 0:57
add comment

If you run zipinfo -t foo.zip the exit status (from $?)will give an error if the zip file has nothing in it. In the version I'm running (3.00) this will be 1 but I imagine that's just a general error code, so the ambiguity might cause you other problems.

You'll also have to deal with the text output of the command which is a string with basic info about the file. I believe this answer is similar to one of those above.

Edit: If you want to avoid the ambiguous return value (and you're in the mood for grep) you could grep the output of the above command for "Empty zipfile".

share|improve this answer
add comment

If you have jar installed you can use the -t option:

jar -tvf file.zip

Directores will all have trailing slashes so you can pipe it to grep:

if jar -tvf file.zip | egrep -v '/$' > /dev/null; then # we have some files!
  unzip file.zip
  rm -f file.zip
share|improve this answer
is it possible with pure bash scripting without even using java jar ? –  nlrreddy Jun 18 '12 at 23:10
@nlrreddy See my edits. –  Tim Pote Jun 18 '12 at 23:18
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.