How to unrar nested RAR files

archiverar

I have a recursive directory structure which contains a lot of RAR files. I have been using this unrar command to extract all contained files into the current working directory:

unrar e -r -o- *.rar

This is fine. How ever, some RAR files contain other RAR files, which are extracted by the above command. The problem is that the contents of these extracted RAR files do not get extracted, as the extracted RAR's are not part of the initial search.

How to go about this? I do not want to delete the first part of RAR files and do a new search. I'm not very fluent in Bash (or #!/bin/sh -xu which is the shebang of the script) but I'm thinking I want to save the file names of the first search and exclude them in the second command. (Max depth is RAR in RAR.) If there is not any more elegant solution?

Thank you in advance, Viktor.

Edit:

Trying the solution proposed by @icarus:

mkdir unrar
cd unrar
unrar e -r -o- ../*.rar
set -- *.rar
[ -e "$1" ] && unrar e -r -o- *.rar

Best Answer

Use the shell (or find) to find the rar archives. Unpack the first set in a new directory, then unpack any that are in that directory.

#!/bin/bash
shopt -s nullglob globstar
mkdir unrar
cd unrar
for i in ../**/*.rar ; do unrar e -o- "$i" ; done
for i in **/*.rar ; do unrar e -o- "$i" ; done
Related Question