I'm dealing with a large amount of password protected .rar files which need to be repacked to remove the password. (The password is known.) I was wondering if there was a script to batch/recursively extract & repack them while keeping the same name and directory structure that they had before.
1 Answer
I would split this task up in two elements, the first is that you need a script rerar
that extracts and builds the rar and takes the name of a rar as parameter:
#!/bin/bash
R="$PWD"/"$1" # if realpath is available you can use R=$(realpath "$1")
tmpdir=$(mktemp -d --suff rerar)
pushd "$tmpdir"
# extract preserving directory structure of the archive
# replace YOUR_PASS_WORD in the next line, with no space after "-p"!
unrar x -pYOUR_PASS_WORD "$R"
# backup the rar file, optional
mv "$R" "$R".org
# re-create recursively going over the files here
rar a -r "$R" .
popd
rm -rf "$tmpdir"
Now you only have to run this on all the rar files involved e.g. by using find
find . -name "*.rar" -exec ./rerar {} \;
It is not as efficient as calling the script with multiple parameters, but here the timeconsuming action is recreating the rar archive, that is why I went for the simple solution.
-
Thanks for your response! I tried your method out, however this is all I am getting for output:
./rerar.sh: line 3: realpath: command not found
Then:/tmp/tmp.XXCpTKpWX9rerar ~/gctest
It then asks for a password and errors out with:Cannot create . Is a directory
(This happens for each .rar that it finds.) Jun 1, 2015 at 2:47 -
@AwesomeMarioFan
realpath
can be installed on a Debian system from the packagerealpath
, I never realised this was not part of bash. There should be no space between -p and your_pass_word, I could not test that part of the script as I have no encrypted .rar files. I updated the script. You have to replace YOUR_PASS_WORD with your password for these .rar files.– AnthonJun 1, 2015 at 3:05 -
Thank you! It seems to be working now except I need to input the password every time it extracts a file. Its not a big deal though (What's odd is even when I search up the syntax, the password flag is the same as the one you had). Jun 3, 2015 at 3:01
-
@AwesomeMarioFan That is very strange, I just created a password protected
.rar
file to test with and it re-rarred it) (and it also worked on a non-protected file with the-p123
password that I used. Does:touch abc; rar m -p123 xxx abc; unrar x -p123 xxx
work for you?– AnthonJun 3, 2015 at 4:56 -
Yep there's no issues with that - I end up with a file called abc and a xxx.rar file. Does that mean it is only a small syntax issue with the original script? Jun 3, 2015 at 21:11