Find the answer to your Linux question:
Results 1 to 7 of 7
Hello, I would like to rename several files like "recto 041.jpg" to "recto1 041.jpg" "recto 042.jpg" to "recto1 042.jpg" "recto 043.jpg" to "recto1 042.jpg" I know the rename command but ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    6

    [SOLVED] Rename multiples files (using rename and regexp)

    Hello,

    I would like to rename several files like
    "recto 041.jpg" to "recto1 041.jpg"
    "recto 042.jpg" to "recto1 042.jpg"
    "recto 043.jpg" to "recto1 042.jpg"

    I know the rename command
    but I'm not very good about regular expressions...

    I 've try something like

    rename 's/^recto ???.jpg$/recto1 ???.jpg' *.jpg

    but it doesn't work...

    What is the good command for this

    Kind regards

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    You actually just need a fairly simple regular expression here. All that you want to do is change the "recto" to "recto1", correct? Therefore, the following will work:
    Code:
    rename 's/recto/recto1/' *.jpg
    It's that simple .
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Aug 2008
    Posts
    6
    That's right it works fine with my examples
    but the problem with your pattern is that

    it can also match for name like
    recto 041 recto.jpg
    that will be converted to
    recto1 041 recto1.jpg
    instead of
    recto1 041 recto.jpg

    (In fact, I ask this in order to increase my knowledges about regexp)

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Okay. So to be more specific, you only want to change "recto" if it's at the very beginning of the filename?
    Code:
    rename 's/^recto/recto1/'
    The '^' in the regular expression means "beginning of the string". Therefore, it will only match "recto" if it occurs at the very beginning.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Aug 2008
    Posts
    6
    Thanks I know "^"
    but how could I say

    begin with "recto"
    follow with a space
    and containing 3 numbers
    and ".jpg"

    I think that it is something like :

    ^recto\ [0-9]{3}.jpg$

    but I don't know what I should say to "rename" to
    change this "recto" to "recto1"

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Alrighty. I didn't realize that you wanted such a specific pattern .

    Code:
    rename 's/^recto (\d{3}.jpg)$/recto1 $1/' *.jpg
    This uses the Perl-specific \d (which means [0-9]), and "captures" the group "\d{3}.jpg". In the replacement, it then says "recto1", followed by a space, followed by the first captured group.

    Does this all make sense?
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Aug 2008
    Posts
    6
    All right ! it is exactly what I was looking for !

    I didn't know the \d

    Thanks a lot !

Posting Permissions

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