10

The man page gives two examples:

rename 's/\.bak$//' *.bak
rename 'y/A-Z/a-z/' *

So it's either s or y and then /replaceThis/withThis

What does the leading s and y mean? Are there other options?

wjandrea
  • 14,109
  • 4
  • 48
  • 98
dan gibson
  • 357
  • 1
  • 3
  • 6

1 Answers1

13

In the first case:

rename 's/\.bak$//' *.bak

you are running a regular expression against filenames and replacing the matching part of expressions (.bak at the end of a file name) with the second expression (which is empty).

In the second case:

rename 'y/A-Z/a-z/' *

you are matching against the regular expression pattern space and transliterating to the target. In other words, the range A-Z is changed to the range a-z, making the filenames lower case.

I suggest you look at the man page for sed for more commands and more details. I believe the 's' command is used most often. As well, regex (section 7) and perl documentation may also be of help. In particular, here's a tutorial on perl and regular expressions.


From man sed:

s/regexp/replacement/    
       Attempt to match regexp against the pattern space.  If  success‐
       ful,   replace  that  portion  matched  with  replacement.   The
       replacement may contain the special character & to refer to that
       portion  of  the  pattern  space  which matched, and the special
       escapes \1 through \9 to refer  to  the  corresponding  matching
       sub-expressions in the regexp.

y/source/dest/  
       Transliterate  the  characters in the pattern space which appear
       in source to the corresponding character in dest.
wjandrea
  • 14,109
  • 4
  • 48
  • 98
John S Gruber
  • 13,248
  • 3
  • 37
  • 64
  • Thanks much - I'm just not sure pointing people to 'sed' is the best idea given its multitude of implementations, especially between any given linux distro and OSX – aaaaaa Jul 23 '18 at 20:46
  • @aaaaaa This site is exclusively about Ubuntu. – wjandrea Oct 07 '18 at 23:12