Question

Given a script generating 100 .zip files, of which some are empty. (Even if there is nothing to zip, my script generate 100 .zip files).

Given the command :

unzip *.zip

I get an error on empty files :

unzip:  cannot find zipfile directory in one of ./data/Archive_03.zip or
        ./data/Archive_03.zip.zip, and cannot find ./data/Archive_03.zip.ZIP, period.

How can I bypass these fake .zip files and silent this unzip error ?

Était-ce utile?

La solution

Core solution

Using bash, you can just loop over the zip files instead of using a glob. This allows to prevent the Argument list too long error for rm, cp, mv commands and continue even if one file fail.

dir="/path/to/zip-dir"
cd "$dir"

for zf in *.zip
do
  unzip "$zf"
done

Testing before doing

You can add an extra test, in the for loop, to check if the file exists and has a size greater than zero before unzipping:

if [[ -s "$zf" ]];
then
  unzip "$zf" # greater exist & size>0
else
  printf "file doesn't exists or null-size: %s\n" "$zf"
fi

a shorter version will be: [[ -s "$zf" ]] && unzip "$zf" (unzip only if exists and >0).

Reference

Autres conseils

Option 1: use it with care - http://www.manpagez.com/man/1/unzip/

unzip -fo *.zip

Option 2: This may not work, it works for .gz extension.

tar -zxvf test123.tar.gz

z -- unzip
x -- extract the file
v -- verbose
f -- forcefully done
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top