How do I find corrupted archive files?

I have a lot of zip files. Some are not downloaded correctly and are corrupted. I want to remove them.

Is there a way to find the corrupted archives in bash?

Asked By: Omid

||

The following will print the name of all corrupted zip files in the current directory and its subdirectories:

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

If you wish to remove them, simply replace printf '%sn' "$file" with rm -f "$file".

Answered By: Chris Down

With GNU (for -readable and -iname) find:

find . -iname '*.zip' -type f -readable ! -exec unzip -t {} ; -exec rm -i {} ;
Answered By: Stéphane Chazelas

To find the corrupted archives in bash, I use the following script:

#!/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

Hope this helps.

Answered By: Nathan SR
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.