1

Possible Duplicate:
How can I mass rename files from the command line or using a 3rd party tool?

I have a group of 47 .mp3 files that are being used for a multimedia project that are named 01.mp3 - 47.mp3 Every file should really have a name that is that number + 5, so that 02.mp3 should really be 07.mp3 The renamed files can have some sort of prefix + the number, or can be written to a new folder as to not interfere with the existing files.

I have Ubnutu and Windows at my disposal.. Can someone suggest an approach to this?

Kendor
  • 831
  • 3
  • 12
  • 21

1 Answers1

1

You could do this in awk with a script like this:

#!/usr/bin/awk -f
BEGIN {
    regex = "[0-9]+"
    print "mkdir new"
}
{
    if (match($8,regex)) {
        before = substr($8,1,RSTART-1);
        pattern = substr($8,RSTART,RLENGTH);
        newnumber = pattern + 5
        after = substr($8,RSTART+RLENGTH);
        printf("mv %s new/%s%.2d%s\n", $8, before, newnumber, after);
    }
}

Assuming you had a folder containing the following files:

foo01
foo02
foo03
foo04
foo05
bar10.txt
bar11.txt
bar12.txt
bar13.txt
rename.awk

If you executed the command ls -l|./rename.awk|sh you would then have:

new/foo06
new/foo07
new/foo08
new/foo09
new/foo10
new/bar15.txt
new/bar16.txt
new/bar17.txt
new/bar18.txt
rename.awk

You can of course modify the script or shell command to only rename files fitting a certain pattern or increment by a different amount.