0

I was reading about rename and came across this example to change the file extension from htm to html:

rename -v 's/\.htm$/\.html/' *.htm

and it said: The $ means the end of the string. \.htm$ means that it will match .htm but not .html.

I was a bit confused by the use of $ here. Since we already specified *.htm at the end of command line, rename would only select out the htm files (instead of html). So why was it necessary to use the $ still? In another words, what's wrong with no using $?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
Geng
  • 11
  • 3
  • I'd have to test this to confirm but presumably without the anchoring `$` that would find the *first* occurrence of `.htm` in the file name and not the extension (i.e. So `foo.htm.htm` for example would become `foo.html.htm` instead of `foo.htm.html`). – Etan Reisner Mar 27 '15 at 21:25
  • 1
    Because it is a regular expression. Look it up; expect to spend all weekend if you want to learn it properly at once. – tripleee Mar 27 '15 at 21:28
  • The original `rename` command on Unix systems did not allow regular expressions, but many distributions have replaced it over the past few years with Larry Wall's `rename` Perl script. If you're lucky, your system might have the original `rename` command available as `rename.ul`. Then you won't have to deal with regular expressions. – dg99 Mar 27 '15 at 23:59

1 Answers1

2

Anchor $ matches the end of the source file name and it is still required in your regex and dot should also be escaped otherwise abc.htm.htm will be renamed to abc.html.htm instead of abc.htm.html.

Correct command is:

rename -v 's/\.htm$/.html/' *.htm
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Now it might look same but initially question had `rename -v 's/.htm$/.html/' *.htm` In any case my point was why `$` will still be needed even when filename pattern is `*.htm` – anubhava Mar 27 '15 at 21:31