Solved unrar multiple .rar files and several folders

Hi !

I have a lot of folders with lots of packed rar files.
- Is there a way to unpack several folders at once ?

I've just been currently doing : unrar e *.rar and then check after it unpacks if the files are complete and rm *.r*

I want to extract everything in a folder , delete all the packed rar files and move on to next folder, until its nothing left to unpack.

There has to be a better way or maybe a simple script, I've been googling the last half hour but can't seem to find anything.
- Lars
 
You have a directory structure like this, right?

% ls -FR
Code:
dir1/           dir2/           dir3/           unpack.sh*

./dir1:
fil1.rar        fil2.rar        fil3.rar

./dir2:
fil4.rar        fil5.rar        fil6.rar

./dir3:
fil7.rar        fil8.rar        fil9.rar

This is not a fully tested script, but it might be a good starting point:

Bash:
#!/bin/sh

IFS="
"

for dir in *
do
    if [ -d "$dir" ]; then
        cd "$dir"
        echo "extracting all rar files in `pwd`"

        for rarfile in *.rar
        do
                unrar x "$rarfile" && rm -i "$rarfile"
        done

        cd ..
    fi  
done

This script contains an rm command, so please be careful (even though I've included the -i option).
 
Learn how to write scripts. Your description of "extract ..., delete ..., move..." is a little unclear. Here is a rough idea of a script:
Code:
#!/bin/sh
set -e  # Stop on any error, so we don't delete a file if extracting it failed.
for archive in *.rar
do
    unrar $archive
    echo Extracted archive $archive
    rm -f $archive
done
echo All done.

Don't just copy that script, read and understand it and modify it to suit your needs. And before running it, test it on a fake version, or on a copy.
 
bkouhi's script is much more fancy, since it allows for rar files to be in subdirectories. One could extend that concept even further, by using find:
Code:
...
for archive in `find . -name *.rar`
do
    ... do something with $archive
    ... delete $archive
done

The big problem all these scripts have: They will stop working if the names of the archives or directories contain spaces or other special characters. The art of writing scripts is in quoting arguments. You should probably replace $archive with "$archive" everywhere. I see that bkouhi did that correctly.
 
I might have explained a bit wrong.

I have a directory lets say /storage/files , within there you have /storage/files/movie1 , movie2, movie3.

I want to extract everything within /storage/files to their respective folders.

So that movie1 stays in folder movie1 and so on.

The catalog structure remains the same only that I want it extracted within the folder + delete the .rar files afterwards.
 
Tried running the script and modified it a bit to rm *.r* .

It enters the directories but I get :

UNRAR 5.80 freeware Copyright (c) 1993-2019 Alexander Roshal
No files to extract

Not sure what the problem is, the same command works if I manually enter the folder and unrar of course.
 
Nevermind got it to work now :)


Bash:
#!/bin/sh
 
IFS="
"
 
for dir in *
do
    if [ -d "$dir" ]; then
        cd "$dir"
        echo "extracting all rar files in `pwd`"
 
        for rarfile in *.rar
        do
                unrar e "$rarfile" && rm *.r*
        done
 
        cd ..
    fi 
done

Thank you both so much :)

- Lars
 
Back
Top