This is the old SliTaz forum - Please use the main forum.slitaz.org

[Perl] rename.pl : RegExp renaming
  • babaorumbabaorum March 2010
    Here is a Perl script to rename some specified filenames doing a regular expression substitution, using advanced regexp syntax used by Perl. It has *not* been written by me, but I'm unable to point out who is the real author.
    Don't forget to enable execute mode after putting the script in a $PATH directory:
    su
    chmod +x rename.pl

    As far as I know, there is no license for it, here is the code:
    #!/usr/bin/perl
    #rename.pl - rename filenames with Perl regexp substitution
    ($regexp = shift @ARGV) || die "Usage: rename perlexpr [filenames]\n";
    if (!@ARGV) {
    @ARGV = <STDIN>;
    chomp(@ARGV);
    }
    foreach $_ (@ARGV) {
    $old_name = $_;
    eval $regexp;
    die $@ if $@;
    rename($old_name, $_) unless $old_name eq $_;
    }
    exit(0);

    Example:
    for three files named: "a1.jpg", "b2.jpg", "cde3.jpg" that you would want to rename "example1.jpg", "example2.jpg", "example3.jpg":
    rename.pl 's/^[^\d]+/example/' a1.jpg b2.jpg cde3.jpg
  • asimoasimo March 2010
    This is a good script. Perl solves some shell script difficulties.
    In this case is filenames including spaces.
  • babaorumbabaorum March 2010
    Absolutely right, Asimo. That's the trick.

    I've updated it a little bit, it seems to work fine.
    I've added a constant verbose mode and a "test mode" to see if the regexp is not aggressive: you can see a "Test mode" warning and nothing is done, just displayed on &1 output.
    #!/usr/bin/perl
    $testmode = 0; #false
    if ($ARGV[0] eq '-t') {
    $testmode = 1; #true
    shift;
    }
    ($regexp = shift @ARGV) || die "Usage: rename [-t] perlexpr [filenames]\n-t : option to test action (nothing really done)\n";
    if (!@ARGV) {
    @ARGV = <STDIN>;
    chomp(@ARGV);
    }
    print "Test mode\n" if ($testmode);
    foreach $_ (@ARGV) {
    $old_name = $_;
    eval $regexp;
    die $@ if $@;
    unless ($old_name eq $_) {
    print "$old_name -> $_\n";
    rename($old_name, $_) unless $testmode;
    }
    }
    exit(0);

  • babaorumbabaorum March 2010
    Mistyping: $oldname is wrong --> $old_name ;=)
  • asimoasimo March 2010
    Hi babaorum

    I edited a bit in line 9: @ARGV = <>;
    I tried the new script in test mode. Nice idea! It works fine.
    The script is possible to use recursively.
    find ! -type d -print0 | xargs -0 ./rename.pl 's/\.jpg$/.jpeg/'
  • babaorumbabaorum March 2010
    Line 9 is right now, thanks ;)

    Yup, I mainly use it recursively.
    I've posted another script that use rename.pl in a limited recursive mode: renup.pl .
    Maybe it can be useful to you too.
    I could have used find + xargs in it but I prefered to get a whole Perl solution rather than mixing both shell and perl tricks.
    Why don't you use the -exec option of find rather than xargs ? (I do love xargs though, very nice tool)
    But I'm sure that you can do the same just w/ shell and rename.pl

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

SliTaz Social