Convert Order for find … -exec Command

findrename

I want to rename files and directories. Let's suppose that I want to convert every ä to a, and I have Järjestelmä/Säätimet. So I say something like find -exec rename 's/ä/a/g'. Now find will convert Järjestelmä to Jarjestelma and then complain that it did not found Järjestelmä anymore.

If I could convert the order of exec-commands to run it would solve this. Is it possible? If not, what is the right way to do this?

This is not a big problem, as I can run the command again until it makes no changes. Anyways this irritates me.

Best Answer

Use find's -depth option, from the man page:

-depth Process each directory's contents before the  directory  itself.
          The -delete action also implies -depth.

That way it will process Säätimet before Järjestelmä and will not complain about not being able to descend into Järjestelmä because you just renamed it.

To prevent rename from handling the whole path use -execdir present in a few find implementations like BSDs and GNU (which changes to the directory and hands just the final part to the command argument {} (with a ./ prefix with some implementations)):

mkdir Järjestelmä
touch Järjestelmä/Säätimet
find . -depth -execdir rename 's/ä/a/g' {} \;
find .

gives:

./Jarjestelma
./Jarjestelma/Saatimet
Related Question