Want to use the find command with a number of rename commands. How can I do it.

This is the sequence I want to execute with the find command

rename "s/ /-/g" * ; rename 'y/A-Z/a-z/' * ; rename 's/[.]-/-/g' * ; rename 's/--/-/g' *

1 Answer 1


You don't need separate rename's here. The first argument to rename can be any perl code that does the modifications you want on the file path (as stored in $_), you don't have to use s/pattern/replacement/flags let alone only one of them.

Here, you could just do:

find . -depth -exec rename -n -d '
  $_ = lc $_; # lowercase
  s/[ .-]*[ -]+/-/g' {} +

Or if your rename doesn't support -d:

find . -depth -mindepth 1 -exec rename -n '
  my ($dirname, $basename) = m{(.*)/(.*)}s;
  $basename = lc $basename; # lowercase
  $basename =~ s/[ .-]*[ -]+/-/g;
  $_ = "$dirname/$basename"' {} +

here turning all <0-or-more-spaces-or-dot-or-dash><1-or-more-spaces-or-dash> to a single dash, but you could do all your s/.../.../s instead, separated with ;s:

find . -depth -exec rename -n -d '
  s/ /-/g;
  s/--/-/g' {} +

In any case, you want to make sure the substitutions are only applied to the basename (hence the -d) and that leaves are renamed before the branches they're on (hence the -depth).

(-ns above are for dry-run).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .