17

I have several RAR archives spread around multiple directories but all under a particular root folder on my Debian based NAS. Could someone help me write a simple script that would recursively go into each folder, unrar the contents, go back to the parent folder and move onto the next directory? So:

cd Photos/Summer/Italy/
unrar e Italy.rar
wait
cd ../France/
unrar e France.rar
wait
etc...

So just point it to root folder "Photos" and it blitzes through it unraring everything on the way...

Eg, directory structure:

*Photos:
 -Summer
  --Italy
   ---Italy.rar
   ---Italy.r01
   ---Italy.r02
  --France
   ---France.rar
   ---France.r01
   ---France.r02
 -Winter
  --Siberia
   ---Siberia.rar
   ---Siberia.r01
   ---Siberia.r02
  --Canada
   ---Snow.rar
   ---Snow.r01
   ---Snow.r02
Hennes
  • 64,768
  • 7
  • 111
  • 168
Touff
  • 388
  • 1
  • 2
  • 12

3 Answers3

28
find Photos/ -name '*.rar' -execdir unrar e {} \; 
Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
6

If you want to move the unrar'd photos to another destination, just enter the destination in the end, like this:

find source_dir/ -name '*.rar' -execdir unrar e -o- {} /new/destination_dir/ \;

phindmarsh
  • 103
  • 2
Dennis
  • 61
  • 1
  • 2
5

unrar has built-in recursion using the -r Recurse subdirectories switch.

unrar x -r <parent directory> Extracts contents of all subdirectories under <parent directory> into each subdirectory, keeping any directory structure that exists in the .rar files. Use e instead of x if directory structure is unwanted.

hmj6jmh
  • 519
  • 2
  • 6
  • 10
  • 1
    This will extract the files from the subdirectory archives into ``. It won't place the extracted files into the subdirectory next to the `.rar` files. So it depends on what outcome is desired. The OP wasn't specific in this case. Sometimes your answer is useful to me but other times I need the `find` answer. – Cliff Aug 09 '19 at 00:05
  • Didn't work on my Raspberry Pi. Used find instead. – Bengt Aug 27 '20 at 22:55