Results 1 to 6 of 6

Thread: batch renaming with the rename command

  1. #1
    Join Date
    Oct 2007
    Beans
    62

    Question batch renaming with the rename command

    Hello, I'm trying to use the rename command to batch rename some files and could use some help. I'm trying to rename files that look like this:

    one-file
    another-file


    to this:

    one - file
    another - file


    ie. i want to put spaces around the dash if they don't exist.

    I have tried this:

    rename -n 's/\S-\S/\1 - \2/' *

    but it tells me:

    \1 better written as $1 at (eval 91) line 1.
    \2 better written as $2 at (eval 91) line 1.

    so I tried:

    rename -n 's/\S-\S/$1 - $2/' *

    but that doesn't work either.

    Anyone?

  2. #2
    Join Date
    May 2008
    Location
    Eugene, OR, USA
    Beans
    435

    Re: batch renaming with the rename command

    You need to use parens:

    Code:
    rename -n 's/(\S)-(\S)/$1 - $2/' *
    In a Perl regular expression, parens create what are referred to as "sub-expressions". The sub-expressions are what get assigned to the $1, $2, ... variables.

    Btw, the above expression won't work if there's whitespace on one side of the dash and not the other (e.g. a file named "foo- bar"). You might prefer to use:

    Code:
    rename -n 's/\s*-\s*/ - /' *
    The above code will eat up whatever whitespace is on either side of the dash and replace that with "<space>-<space>". And it will also work for filenames where there is no whitespace around the dash.
    Last edited by HalPomeranz; May 29th, 2008 at 04:30 PM.
    Hal Pomeranz, Deer Run Associates
    [[ Various Linux/Unix related documents ]]
    [[ Command-Line Kung Fu blog ]]

  3. #3
    Join Date
    Mar 2008
    Location
    Melbourne, AU
    Beans
    71
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: batch renaming with the rename command

    Or if you want a graphical way, just install "Bulk Rename" from Add/Remove...

  4. #4
    Join Date
    Jul 2008
    Beans
    52

    Re: batch renaming with the rename command

    Hi everyone,

    I want to rename images in a folder. Their names and their extentions are in different types, and i am trying to rename them to 01.jpg, 02.png, 03.jpg, 04.gif...

    Somebody can help? Thanks.

  5. #5
    Join Date
    Nov 2012
    Beans
    2

    Re: batch renaming with the rename command

    Hi

    To use back-reference, i.e. \1 or $1, one has to define a sub-expression with parentheses as shown by Halpomeranz. However, the fact that it is recommended to use $1 instead of \1 (even though \1 works as well) is surprising to me. When I learned regular expression, back reference was done by \1, \2, ... I guess I just have to keep that in mind.

    Cheers,
    Ye

  6. #6
    Join Date
    Dec 2009
    Beans
    167

    Re: batch renaming with the rename command

    Quote Originally Posted by gaosanyong View Post
    Hi

    To use back-reference, i.e. \1 or $1, one has to define a sub-expression with parentheses as shown by Halpomeranz. However, the fact that it is recommended to use $1 instead of \1 (even though \1 works as well) is surprising to me. When I learned regular expression, back reference was done by \1, \2, ... I guess I just have to keep that in mind.

    Cheers,
    Ye
    This thread is a bit old, but here goes my answer.

    Using backreferences outside of the regex pattern will give you problems in certain situations (see below). $1 and \1 are slightly different concepts. $1 is a matching variable, which can be safely used outside the regex block as a variable, whereas "\1" is just a backreference (see below).

    From perlretut - Perl regular expressions tutorial:

    Backreferences.
    Closely associated with the matching variables $1, $2, ... are the backreferences \1, \2
    Backreferences are simply matching variables that can be used inside a regexp ...

    Although $1 and \1 represent the same thing, care should be taken to use matched variables $1, $2,... only outside a regexp and backreferences \1, \2,... only inside a regexp; not doing so may lead to surprising and unsatisfactory results.

    Also from perlre online manual pages, check this section - "Warning on \1 Instead of $1":

    This is grandfathered (for \1 to \9) for the RHS of a substitute to avoid shocking the sed addicts, but it's a dirty habit to get into. That's because in PerlThink, the righthand side of an s/// is a double-quoted string. \1 in the usual double-quoted string means a control-A. The customary Unix meaning of \1 is kludged in for s///. However, if you get into the habit of doing that, you get yourself into trouble if you then add an /e modifier.

    s/(\d+)/ \1 + 1 /eg; # causes warning under -w
    http://perldoc.perl.org/perlre.html#Warning-on-\1-Instead-of-$1

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •