Find/Unrar/Delete Script

I assume you only want to scan the current folder (and not all other folders beneath it):

for rarfile in *.rar; do
    unrar x "$rarfile"
done

Key thing is to put the file name into "" when passing it to unrar to avoid any problems with spaces in the name.

Now if you want to have this as a script you can run, you can do the following

 cd ~
 mkdir .bin 
 echo 'PATH=$PATH:$HOME/.bin' >> .profile
 echo 'export PATH' >> .profile
 . ./.profile
 nano .bin/extract_all_rars

This gives you a simple editor for text files, essential commands are displayed at the bottom. Type

 #!/bin/bash

followed by the code block at the top, save the file and exit. Then (in the shell again) type

 chmod +x .bin/extract_all_rars

to mark it executable (so the shell recognizes it as a command).

Automatic deletion has one caveat: unrar doesn't return an error status if things go wrong so you may loose your rar files. If this is not an issue, adding

 rm -f "$rarfile" ${rarfile%%.rar}.r{0..9}{0..9} 

after the unrar in the loop above will do the job. The second parameter is used to create all possible .r04 suffixes by first stripping away the suffix (${rarfile%%.rar}) and then iterating from 0 to 9 twice to get all possible combinations (run echo foo{0..9} in bash to see how it works). As most of these file names do not exist, I've added -f as an option to avoid error messages.

If you are fairly sure that no other files with a .rXX suffix are in the same directory, a simple

rm -${rarfile%%.rar}.r??

does the trick as well.


If you don't have rar/unrar already:

  • Download RAROSX 4.2 from rarlab.com
  • In Finder, open ~/Downloads and double click the downloaded file to unpack. A rar folder will be created
  • Open Terminal and run the following commands

    cd ~/Downloads/rar
    sudo install -d rar unrar /usr/local/bin
    

    to install the binaries (executables)

  • We must also make sure that the shell afterwards finds the binaries

    cd ~
    echo 'PATH=$PATH:/usr/local/bin' >> .profile
    echo 'export PATH' >> .profile
    . ./.profile
    

you could do something like this:

find . -iname '*.rar' | xargs unrar x

find . -iname '*.r*' | xargs rm -f

the script works as follows: it searches recursively for rar files in the directory you are in (the dot) it pipes all results to an argument list (xargs) that is passed through to the command 'unrar'

Same in the second line, but now it is passed to remove (rm). The -f flag makes sure that the files are deleted instantly, you won't be asked for your permission. The rm -f is a powerful command that you should use carefully, so think about what it would remove if you would execute it before you delete it, or just test this by doing:

find . -iname '*.r*'

One thing about the unrar command: unrar is not a standard command in Mac OSX (not sure if this is the case in other Linux/Unix distributions), so make sure you have it installed on your machine.


Note: There is a fine but important difference between find . -iname *.r* and find . -iname '*.r*' relating to the way Unix shells handle wildcards:

  • In the first form, wildcards are expanded by the shell (so the shell will look for all files/folders matching *.r* in the current directory and pass these as parameters to find. This will work ok if there is one file to match anyway but will fail if there are several (because then the call is find . -iname foo.rar bar.rar but -iname only takes one argument).
  • In the second form the pattern (without the '') is passed to find and the wildcard expansion is done by find (which then tries to match every file it finds).

So in general patterns passed to find should be enclosed in '' to produce the desired result.


I use unar, which is a command line version of The Unarchiver.

These also work with paths that contain spaces:

find . \( -name \*.rar -o -name '*.r[0-9][0-9]' \) -print0 | xargs -0 unar
find . -name '*.r[a0-9][r0-9]' -exec unar {} \+
find . -name '*.r*' -delete

Or if you install Bash 4 and add shopt -s globstar to ~/.bash_profile:

unar **/*.r*
shopt -s extglob; printf %s\\n **/*.@(rar|r[0-9][0-9])
rm **/*.r[a0-9][r0-9]