3

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
  • Do you need to extract to the current directory? If not then make a new directory and change into it, extract the original rar files. extract from any rar files. 5 lines. – icarus Oct 19 '19 at 1:15
  • I think I do not, and I had not thought about that. Will definitely give it a try tomorrow if nothing else. Thank you! – Viktor Oct 19 '19 at 1:25
  • @icarus I saw your edit, would you care to explain what set -- *.rar and [ -e "$1" ] does do exactly? – Viktor Oct 19 '19 at 14:21
  • This handles the case where there are no rar files inside the original rar files. The set -- *.rar sets $1 to the first rar file, $2 to the second and so on. If there are none then it sets $1 to the string *.rar. The [ -e "$1" ] tests to see if $1 exists, and the && says "run the thing on the left and if it works then run the thing on the right". – icarus Oct 19 '19 at 15:31
  • @icarus Hm, apparently the -r option does not seem to work for me, I thought it did. I only find and extract top-level RAR files. – Viktor Oct 20 '19 at 14:00
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.