linux - Bash 脚本检查 zip 文件是否为空

标签 linux bash

我对 linux 比较缺乏经验, 如果某个 zip 文件为空,我必须检查 bash 脚本——即 zip 不包含任何文件。 我找到这段代码:

if ! zipinfo ${filetotransfer} | tail -n 1 | grep '^0 ' >/dev/null ; then # we have empty zip file!
   echo " zip empty"
   rm $filetotransfer
   exit 0
fi

但是无论 zip 是否为空,它都会删除文件。 有什么方法可以检查吗?

最佳答案

您可以使用 stat 或 md5sum 或 zip header 检查文件大小是否为 22

# 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

关于linux - Bash 脚本检查 zip 文件是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814121/

相关文章:

linux - 在 bash 中使用 awk 命令从记录中提取单个字段

linux - 查找文本文件的编码

linux - 如何将终端输出中的 'specific' 行保存到文件中?

Bash - 添加包含变量的新行

linux - 如何知道我的终端的默认 shell 进程?

linux - 什么时候关闭 atime 和 diratime 是个好主意?

c - sched_yield 在这种情况下做了什么?

c++ - GCC 的 -Wpsabi 选项到底有什么作用?压制它有什么影响?

linux - 在 shell 脚本中使用查找

python - 在 Bash 中使用 Python Shell