unrar all files in folder

Questions and mods regarding system management may go here
Forum rules
Please note the disclaimer before modifying your Synology Product.

unrar all files in folder

Postby gurmukh » Mon Jun 11, 2012 8:10 pm

Hi Guys

I having a hard time trying to create a simple bash script that would recursively go through all directories in my Downloads folder, and unrar.

Its quite a simple script, but for some reason I keep running into a dead end. At the moment, I'm trying this:

Code: Select all
for x in */*.rar; do unrar x $x; done


However this doesnt work as */*.rar returns jibberish. If I simply replace it with *.rar it works fine, but this only applies to the current directory and not any subdirectories.

Can anyone shed a light onto what I'm doing wrong?

Thanks
gurmukh
I'm New!
I'm New!
 
Posts: 7
Joined: Sat Jun 09, 2012 4:30 pm

Re: unrar all files in folder

Postby maxxfi » Fri Jun 22, 2012 2:56 pm

Instead of running a for loop, use the 'find' command. E.g. something like:
Code: Select all
find . -name "*.rar" -exec unrar x {} \;

"find from current directory . files named "*.rar", and run 'unrar x' command on each item found (= {})"
DS-411 (DSM 4.2-3202) w/ 2x WD10EFRX + 1x HD154UI
DS-106j (DSM 3.0-1357), PATA-to-SATA adapter, 2.5" HM250HI
User avatar
maxxfi
Architect
Architect
 
Posts: 4318
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: unrar all files in folder

Postby Friggo » Tue Nov 06, 2012 3:16 pm

I once did a small script that did something similar.

Code: Select all
#! /bin/bash

if [ $# -lt 1 ]; then
        echo "usage: $0 <folder1> <folder2> ...";
fi

MYPWD=`pwd`;

for i in "$@"; do
    cd "$i" && (unrar e *.part01.rar || unrar e *.part001.rar || unrar e *.rar || unrar e *.000 || unrar e *.001) && rm -fv *.rar *.r[0-9][0-9] *.0*
    cd "$MYPWD";
done


My script also removes the rar-files after a successful extraction. It also handles the cases you have multiple files names xxx.part01.rar xxx.part02.rar etc.
It doesn't recurse into a folder structure, it only tries to unrar files in the folders you give on the commandline.
DS1812+ DSM 4.1
Friggo
Trainee
Trainee
 
Posts: 18
Joined: Fri Nov 02, 2012 6:57 pm


Return to System Managment Mods

Who is online

Users browsing this forum: No registered users and 2 guests