I am learning how to use the command-line Unix utility rename (see, for example, https://www.tecmint.com/rename-multiple-files-in-linux/).

The manual for rename says that you can combine multiple renaming rules (notice, it is a synopsys of the usage where I remove other arguments for clarity):

rename [ -e|-E perlexpr]*|perlexpr [ files ]

I try to do this on the following ariticial example. I have a directory with files:

$ ls
01.php  02.php  03.php

I apply two transformations using one rename invokation:

rename -n -v -e "s/\.php$/\.htm/" "y/a-z/A-Z/" *

The expected behavior is that I will have:

$ ls
01.HTM  02.HTM  03.HTM

However, the output of rename is

rename(01.php, 01.htm)
rename(02.php, 02.htm)
rename(03.php, 03.htm)

that is, the second transformation (lowercase to uppercase characters) is not applied.

How to combine multiple expressions together?

  • 1
    What should work is to put -e before each substitution, ie rename -n -v -e "s/\.php$/\.htm/" -e "y/a-z/A-Z/" *, but this gives an error. The documentation (man rename) is confusing, implying that you need -E to use the semicolon separator, but Toto's answer works, as do rename -n -v -E "s/\.php$/\.htm/" -E "y/a-z/A-Z/" * and rename -n -v -e "s/\.php$/\.htm/;" -e "y/a-z/A-Z/" *, as well as the quirky rename -n -v -e "s/\.php$/\.htm/" -e ";" -e "y/a-z/A-Z/" *. I have concluded that -E adds an implied semicolon between expressions, whereas -e requires explicit semicolons. – AFH Jun 26 '18 at 12:43

To combine multiple expressions, separate expressions with a semicolon (instruction separator in Perl):

rename -n -v -e "s/\.php$/\.htm/; y/a-z/A-Z/" *

Additional variants based on the comments by @AFH

Another way (no need to use -e, just combine all expressions with ;):

rename -n -v "s/\.php$/\.htm/; y/a-z/A-Z/" *

Yet another way (prepend each expression with -e; notice ; in the end of the first expression):

rename -n -v -e "s/\.php$/\.htm/;" -e "y/a-z/A-Z/" *

Yet one more way (prepend each expression with -E, which adds ; implicitly):

rename -n -v -E "s/\.php$/\.htm/" -E "y/a-z/A-Z/" *
  • I've extended your answer based on the comment by @AFH and accepted it. Thanks! – Dmitry Kabanov Jun 27 '18 at 13:20

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.