Reputation: 23
I am rather inexperienced in linux, I have to check in bash script if some zip file is empty - i.e zip contains no files. I found this code:
if ! zipinfo ${filetotransfer} | tail -n 1 | grep '^0 ' >/dev/null ; then # we have empty zip file!
echo " zip empty"
rm $filetotransfer
exit 0
fi
But it removes file both if zip is empty or not. Is there any way to check it?
Upvotes: 1
Views: 3032
Reputation: 123821
You can just check file size is 22 with stat or md5sum or zip header
# by checking file size
% stat -f%z a.zip
22
% xxd a.zip
0000000: 504b 0506 0000 0000 0000 0000 0000 0000 PK..............
0000010: 0000 0000 0000 ......
# with md5sum
$ md5sum a.zip
76cdb2bad9582d23c1f6f4d868218d6c a.zip
# or by checking zip header
% [ `head -n22 a.zip | tr -d '\0-\6'` = "PK" ] && echo 1
1
Upvotes: 3
Reputation: 5768
You can check the error status of zipinfo -t
f=test.zip
if zipinfo -t "$f" > /dev/null
then
echo "not empy"
else
echo "empty"
fi
Upvotes: 1