Bash script to unrar part01.rar and .rar files recursively inside their subdirectories

I got gatvol of traversing directories and running unrar manually, so I wrote this script. It dives down into subdirectories looking for .rar or part01.rar files and then unrars them in the subdirectory. It also deals with spaces in filenames and directory names. Probably not the cleanest solution, but in Bash scripting TMTOWTDI. With a bit of tweaking it’ll work for unzip and all the others.

cwd=`pwd`
find . -iname '*.rar' | while read FILE
do
        d=`dirname "$FILE"`
        f=`basename "$FILE"`

        # only unrar part01.rar or .rar
        echo $f | grep -q 'part[0-9]*.rar$' 2>&1 > /dev/null
        if [ "$?" == "0" ]; then
                echo $f | grep -q 'part01.rar$' 2>&1 > /dev/null
                if [ "$?" == "1" ]; then
                        continue
                fi
        fi

        cd "$d"
        echo "Unrar $f"
        unrar x -o+ "$f"
        cd "$cwd"
done
2010.02.12