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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWith GNU (for -readable
and -iname
) find:
find . -iname '*.zip' -type f -readable ! -exec unzip -t {} \; -exec rm -i {} \;
unzip -t {}
fails, exec rm -i {}
. It has to be escaped because by default !
is used for history expansion in bash
.
Nov 12, 2012 at 16:02
!
literally when it's followed by a space? I have the habit of writing \!
too, but I thought it was out of habit (because !
has to be quoted in so many places in bash), not out of necessity.
Nov 12, 2012 at 23:35
{}
needs escaping...
Nov 13, 2012 at 2:21
-print0
instead of exec rm -i {} \;
will display the files that are corrupted without deleting them.
Nov 13, 2012 at 3:06
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 '%s\n' "$file"
done
If you wish to remove them, simply replace printf '%s\n' "$file"
with rm -f "$file"
.
./**/*.zip
to avoid problems with file/dir names starting with "-".
Nov 12, 2012 at 15:21