Find/Unrar/Delete Script I'm a PC convert, and quite novice when it comes to shell/bash scripting, but I really want to learn/understand more.

My need: I need to be able to scan a folder, find any .rar files, extract the files in place, and delete the associated .rar afterwards.  Each may also have the .r00, .r01, ... files associated, so those need to be deleted afterwards as well.

Can someone help me figure out (and understand) how to write a script for this?  The more comments, the better.
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.