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.

  • find -depth ? – Celada Mar 18 '16 at 8:25
  • @Celada, you mean to run -depth 1, -depth 2 etc. until find finds nothing? – Jori Mäntysalo Mar 18 '16 at 8:27
  • @JoriMäntysalo, you are thinking of -maxdepth – Stig Hemmer Mar 18 '16 at 8:35
  • @StigHemmer, you are right. My mistake. (But the question is still open, see below.) – Jori Mäntysalo Mar 18 '16 at 9:11

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 .


  • I still got the error. Can you give an example of a whole command? I tested with find x -depth -exec rename 's/a/b/g' {} \; where x is a directory. – Jori Mäntysalo Mar 18 '16 at 8:43
  • Your perl expression for rename does change the whole path, it should just change the basename. That is of course never going to work. – Anthon Mar 18 '16 at 10:11
  • Excellent! This should be linked to older questions about renaming files. – Jori Mäntysalo Mar 18 '16 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.