如何找到损坏的存档文件?

如何找到损坏的存档文件?

我有很多 zip 文件。有些未正确下载并且已损坏。我想删除它们。

有没有办法在 bash 中找到损坏的档案?

答案1

使用 GNU(对于-readable-iname)查找:

find . -iname '*.zip' -type f -readable ! -exec unzip -t {} \; -exec rm -i {} \;

答案2

以下命令将打印当前目录及其子目录中所有损坏的 zip 文件的名称:

#!/bin/bash
shopt -s dotglob nullglob globstar
for file in ./**/*.zip; do
    [[ -r $file ]] || continue
    unzip -t "$file" >/dev/null 2>&1 || printf '%s\n' "$file"
done

如果您想删除它们,只需替换printf '%s\n' "$file"rm -f "$file"

答案3

为了在 bash 中查找损坏的档案,我使用以下脚本:

#!/bin/bash

# change myfolder value below fully
myfolder="/Users/nathan/Downloads/some folder"

cd "$myfolder"

rm -f testlog.sh
rm -f testlog.txt

SQ="'"

find . -type f -iname '*.zip' -print | while read line
do
echo "unzip -t ${SQ}${line}${SQ}" | tee -a testlog.sh 2>&1;
done

bash testlog.sh | tee -a testlog.txt 2>&1;

totalcommands=$(wc -l testlog.sh|awk '{print $1}')
totalstatus=$(grep -o "No errors detected in compressed data of " testlog.txt | grep -c "")

echo
if [ $totalcommands -eq $totalstatus ]; then
echo "-------------------------------"
echo "All Tests Returned Success !!!!"
echo "-------------------------------"
else
echo "---------------------------------------------------------------------------"
echo "Some Tests Failed. Please check the ${SQ}${myfolder}/testlog.txt${SQ} file."
echo "---------------------------------------------------------------------------"
fi
echo

希望这可以帮助。

相关内容