'rename' on files with apostrophes

4

1

I'm trying to batch rename some files using the rename utility (specifically the perl version, i.e. prename). Unfortunately, the file names contain apostrophes, and it's messing things up. I'm not sure how to proceed.

Here's what I've tried:

rename -n '/.*(\d\d).jpg/Foo's Excellent Photo - $1.jpg/'  # fails due to end of string
rename -n '/.*(\d\d).jpg/Foo\'s Excellent Photo - $1.jpg/' # fails due to end of string
rename -n "/.*(\d\d).jpg/Foo's Excellent Photo - $1.jpg/"  # fails due to shell expansion

What is the correct syntax?

nomen

Posted 2013-10-26T03:48:59.100

Reputation: 153

What version of rename are you using? None of my research suggests using regexp type transformations. Also, your regexp suggests the name with the apostrophe is what you're converting ~to.~ Is that mandatory? Can you go without the apostrophe? – dafydd – 2013-10-26T04:17:01.287

1

It's the Perl rename utility, which is packaged for Debian/Ubuntu. See: http://stackoverflow.com/questions/14327613/rename-multiple-files-from-command-line for an example. Keeping the apostrophe is the point of the question.

– nomen – 2013-10-26T04:21:20.460

Answers

4

  1. Your last variant is the correct one to use single inside double quotes -- but you have to escape also the $1 otherwise the shell will expand it:

    "/.*(\d\d).jpg/Foo's Excellent Photo - \$1.jpg/"
    
  2. However, I still get the error

    Bareword found where operator expected at (eval 1) line 1, near "/.*(\d\d).jpg/Foo's"
        (Missing operator before Foo's?)
    syntax error at (eval 1) line 1, near "/.*(\d\d).jpg/Foo's Excellent "
    

    But this is not because of wrong quoting, but because perl-rename expects a perl regex. And you obviously want to search and replace, so use s/.../.../, not only /.../.../.

  3. So, summing up, this command works flawlessly:

    $ rename -n  "s/.*(\d\d).jpg/Foo's Excellent Photo - \$1.jpg/" *
    PIC44.jpg renamed as Foo's Excellent Photo - 44.jpg
    PIC45.jpg renamed as Foo's Excellent Photo - 45.jpg
    

mpy

Posted 2013-10-26T03:48:59.100

Reputation: 20 866

Thanks! I was using s///, but forgot to put it in my question. – nomen – 2013-10-26T16:38:57.697