2

On linux, using rename to rename multiple directories. I want to rename directories starting with higher case characters to lower case and add a special character, let's say _ before the first letter.

For example I want to rename Myfile to _myfile

The best I came up with so far is :

rename -n 'y/[A-Z]/[a-z]' *

But when I add my character to this regular expression I get the following error :

rename -n 'y/[A-Z]/_[a-z]' *
Replacement list is longer than search list at (eval 90) line 1.

How can I add the special character to the regular expression?

2

Do:

rename -n 's/^([A-Z])/_\L$1/' */
  • ^([A-Z]) matches any uppercase letter at start, and put in captured group 1

  • In the replacement, we are lowercasing (\L) the captured group 1 with perpending a _

  • */ matches only directories

Remove -n for actual renaming action:

rename 's/^([A-Z])/_\L$1/' */
| improve this answer | |

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.