Removing Files after Successful Extraction unrar rm shopt globstar

bashrarscriptunix

shopt -s globstar
for f in **/*.rar; do
    dir=`dirname "$f"`;
    unrar e "$f" "$dir" && rm -frv "${f::-2}*";
done

From echoing out testing the rm command, the file is the correct path. I don't receive any errors about an incorrect path or anything. I know there are other methods to use find, but that would be pointless and use extra cpu cycles this should be working from everything I found. Any ideas on how to use the rm command properly?

Best Answer

It should work with removing the double quotes:

shopt -s globstar
for f in **/*.rar; do
    dir=`dirname "$f"`;
    unrar e "$f" "$dir" && rm -frv ${f::-2}*;
done

Bash expansion of the star char * doesn't occurs inside quotes.

For example:

echo dir/*.rar might give:

dir/file1.rar
dir/file2.rar
dir/file3.rar

But echo "dir/*.rar" will give:

dir/*.rar

(rm command will try to delete a filename which contains literally a star character.)

Related Question