Find and -exec and why doesn’t basename work in there?

Categories: Uncategorized

Ever wonder why this doesn’t work to rename all your isos to .nrg?

find ./ -iname *iso -exec rename {} `basename {} .iso`.nrg \;

The thing is, the backticks are a bash construct, and whatever you specify to -exec does *not* get shell-expanded. So it’s being passed verbatim, with the only substitution being the {} for the found filenames.

But this works! (though quoting may pose some challenges). This works because it *is* being processed through bash, so all the normal expansion and shell tricks will work.

find ./ -iname *iso -exec bash -c "rename {} `basename {} .iso`.nrg"  \;