如何检查 zip 文件中的文件长度
How to check the length of file which is inside the zip file
我想检查 zip 中的文件是否为空。我知道 unzip -l
命令,但它提供了很多信息。
[abc@localhost test]$ unzip -l empty_file_test.zip
Archive: empty_file_test.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-05-2017 06:43 empty_first_20170505.csv
0 07-05-2017 06:43 empty_second_20170505.csv
--------- -------
0 2 files
我通过命令
从 zip 文件中提取了文件名
file_names="$(unzip -Z1 empty_file_test.zip)
file_name_array=($file_names)
file1=${file_name_array[0]}
file2=${file_name_array[1]}
我尝试使用 -s
选项但没有用
if [ -s $file1 ]; then
echo "file is non zero"
else
echo "file is empty"
fi
它总是打印 file is empty
即使文件不为空。
unzip -l empty_file_test.zip | awk 'NR>=4{if(==0){print }}'
可能适合你,也可以写成
unzip -l empty_file_test.zip | awk 'NR >= 4 && ==0{print }'
您可以格式化 unzip -l 的输出
unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2
解释:
unzip -l
解压缩文件和 returns deisred 信息
awk '{print "\t" }'
打印第 1 列和第 4 列(大小和文件名)
tail -n+4
从输出中去除前几行(删除 header 和不需要的信息)
head -n-2
从输出中删除最后两行(删除不需要的摘要)
编辑:
要将空文件存储到数组中,您可以映射命令的输出:
read -r -a array <<< `unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2 | awk '{if(==0) print }'`
说明
unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2
上面有解释
awk '{if(==0)}{print }'
只是给你空文件的文件名
<<<
将反引号``中命令的输出输入到读取命令中
read -r -a array
将输入读入变量数组
但是
您可以只使用较短的 Sjsam 命令并执行相同的操作:
read -r -a array <<< `unzip -l empty_file_test.zip | awk 'NR>=4{if(==0){print }}'`
read -r -a array
上面有解释
<<<
上面有解释
awk 'NR>=4{if(==0){print }}'
NR>=4
输出每行 > 4(去除 header 和不需要的输出)
if(==0){print }}
如果大小 ($0) 为 0,则执行 {print }
{print }
输出文件名
我想检查 zip 中的文件是否为空。我知道 unzip -l
命令,但它提供了很多信息。
[abc@localhost test]$ unzip -l empty_file_test.zip
Archive: empty_file_test.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-05-2017 06:43 empty_first_20170505.csv
0 07-05-2017 06:43 empty_second_20170505.csv
--------- -------
0 2 files
我通过命令
从 zip 文件中提取了文件名file_names="$(unzip -Z1 empty_file_test.zip)
file_name_array=($file_names)
file1=${file_name_array[0]}
file2=${file_name_array[1]}
我尝试使用 -s
选项但没有用
if [ -s $file1 ]; then
echo "file is non zero"
else
echo "file is empty"
fi
它总是打印 file is empty
即使文件不为空。
unzip -l empty_file_test.zip | awk 'NR>=4{if(==0){print }}'
可能适合你,也可以写成
unzip -l empty_file_test.zip | awk 'NR >= 4 && ==0{print }'
您可以格式化 unzip -l 的输出
unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2
解释:
unzip -l
解压缩文件和 returns deisred 信息
awk '{print "\t" }'
打印第 1 列和第 4 列(大小和文件名)
tail -n+4
从输出中去除前几行(删除 header 和不需要的信息)
head -n-2
从输出中删除最后两行(删除不需要的摘要)
编辑:
要将空文件存储到数组中,您可以映射命令的输出:
read -r -a array <<< `unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2 | awk '{if(==0) print }'`
说明
unzip -l test.zip | awk '{print "\t" }' | tail -n+4 | head -n-2
上面有解释
awk '{if(==0)}{print }'
只是给你空文件的文件名
<<<
将反引号``中命令的输出输入到读取命令中
read -r -a array
将输入读入变量数组
但是
您可以只使用较短的 Sjsam 命令并执行相同的操作:
read -r -a array <<< `unzip -l empty_file_test.zip | awk 'NR>=4{if(==0){print }}'`
read -r -a array
上面有解释
<<<
上面有解释
awk 'NR>=4{if(==0){print }}'
NR>=4
输出每行 > 4(去除 header 和不需要的输出)if(==0){print }}
如果大小 ($0) 为 0,则执行{print }
{print }
输出文件名