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:

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

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:


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

cd "$myfolder"

rm -f
rm -f testlog.txt


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

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

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

if [ $totalcommands -eq $totalstatus ]; then
echo "-------------------------------"
echo "All Tests Returned Success !!!!"
echo "-------------------------------"
echo "---------------------------------------------------------------------------"
echo "Some Tests Failed. Please check the ${SQ}${myfolder}/testlog.txt${SQ} file."
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.