How to rename batches of filenames?

Discussion in 'Command Line' started by bashcommando, Aug 26, 2014.

  1. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    I have over 7000 files I got from a frame extraction in vlc. I want to get rid of all the zeros before the filenames and not get rid of the zeros after the... thing. Example:
    000000100.png -> 1.png <- No
    000000100.png -> 100.png <- Yes
    How do I do this from the command line?

  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,260
    Likes Received:
    428
    Trophy Points:
    83
  3. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    Can you give an example?
  4. unixfish

    unixfish Member

    Messages:
    41
    Likes Received:
    23
    Trophy Points:
    8
    Perl to the rescue. There is a bit assumed here, but the basics are below. Note the single quotes are intentional and required on the split.

    while ($filename = glob("*.png"))
    {
    ($number, $extension) = split '\.', $filename;
    $number = s/^0*(\d+)$/$1/;
    rename($filename, "$number.$extension");
    }
  5. unixfish

    unixfish Member

    Messages:
    41
    Likes Received:
    23
    Trophy Points:
    8
    ...and forgive the formatting - I did not see how to embed multiple spaces to indent the code in the { }.

    You can also add 1 and subtract 1, but that is messy if you get a non-numeric character in the string.
  6. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    And forgive me for not knowing perl.
  7. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    I ran your code and all the files got deleted. T_T
    Edit: *dopeslap*
  8. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,260
    Likes Received:
    428
    Trophy Points:
    83
  9. rstanley

    rstanley Member

    Messages:
    74
    Likes Received:
    44
    Trophy Points:
    18
    First of all, before you run ANY new script on a live system, you need to THOROUGHLY test it on a set of sample files first!

    Although it is not a command line app, you may also want to look at another rename utility called, "gprename" It should be included in your Distro, but if not, it can be downloaded from: http://gprename.sourceforge.net/

    It is a GPL3, Gtk2-perl app. And just for you @bashcommando, it has an undo! ;^)

    I am a firm believer in bash to do a lot, if not most of the administrative work I need to do in Linux, but there are exceptions. This is one. The more complex the changes made, the easier to do when you can visually see the changes before you commit, as opposed to running extensive tests on the command line. You may need to run the utility multiple times as you cannot combine the four different types of changes, Change Case, Insert/Delete, Replace/Remove, Numerical.
  10. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    Its ok, I can always reextract it.

Share This Page