The task is to extract files from multiple password-protected archives stored in “IN” directory with 7zip package, passwords stored in list “PASS”. I have a problem: when the password is wrong 7zip creates empty folders and files. Are there any ways to skip wrong password and go to next iteration? Here’s my bash script
FILES=./IN/*
PASS=(‘1’ ‘2’ ‘3’)
for f in $FILES
do
for psd in $(PASS[@])
do
if 7z x -y -p”$psd” -o./OUT $f | grep ‘ERROR’; then
break
fi
done
done
Here's a suggestion : use 7zz t
(test archive integrity) to determine if a given password in your list is valid before attempting extraction.
Example:
#! /bin/sh
WORKDIR='path/to/your/work/dir'
PW='1 2 3'
for File in "$WORKDIR"/*.zip
do test -r "$File" || continue
for Password in $PW
do 7zz t -p"$Password" "$File" >/dev/null 2>&1 \
&& { 7zz x -p"$Password" "$File"; break; }
done || echo "warning: could not extract file \"$File\" (no valid password found)"
done
if you've installed package "7zip", the archiver command name is 7zz
.
if you've installed package "p7zip-full" (the Unix CLI port of 7-Zip), the archiver command name is 7z
.
Tested on Linux Debian 11.
Hope that helps.