给定一个生成100个.zip文件的脚本,其中一些是空的。 (即使没有任何内容可以压缩,我的脚本会生成100个.zip文件)。
鉴于命令:
unzip *.zip我在空文件上收到错误:
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.我怎样才能绕过这些假的.zip文件并沉默这个unzip错误?
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 *.zipI 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 ?
最满意答案
核心解决方案
使用bash ,你可以循环遍历zip文件而不是使用glob。 这允许防止rm,cp,mv命令的参数列表太长错误,并且即使一个文件失败也会继续。
dir="/path/to/zip-dir" cd "$dir" for zf in *.zip do unzip "$zf" done在做之前测试
您可以在for循环中添加额外的测试,以在解压缩之前检查文件是否存在且大小是否大于零 :
if [[ -s "$zf" ]]; then unzip "$zf" # greater exist & size>0 else printf "file doesn't exists or null-size: %s\n" "$zf" fi较短的版本将是: [[ -s "$zf" ]] && unzip "$zf" (仅当存在时解压缩且> 0)。
参考
请参阅man bash (查找-s或CONDITIONAL EXPRESSIONS部分)。 问题rm,cp,mv命令的参数列表太长错误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" doneTesting 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" fia shorter version will be: [[ -s "$zf" ]] && unzip "$zf" (unzip only if exists and >0).
Reference
see man bash (look for -s or CONDITIONAL EXPRESSIONS section). Question Argument list too long error for rm, cp, mv commands更多推荐
发布评论