Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Moab, Sathya Apr 30 '12 at 7:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Try bulkrename...bulkrenameutility.co.uk/Screenshots.php –  Moab Apr 30 '12 at 1:25
    
I'll have to play around with this tomorrow, but you should be able to easily pull something off with a for loop in bash. –  MaQleod Apr 30 '12 at 2:55
add comment

1 Answer

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.

share|improve this answer
    
This looked like the easiest approach, but there maybe a syntax issue. I'm using Ubuntu 11.10. Here's what was displayed after I ran the script: myname@X201:~/Dropbox/Eye Rhymes Web/Audio$ sh rename.sh rename.sh: 2: BEGIN: not found rename.sh: 3: regex: not found Warning: unknown mime-type for "mkdir new" -- using "application/octet-stream" Error: no such file "mkdir new" rename.sh: 5: Syntax error: "}" unexpected myname@X201:~/Dropbox/Eye Rhymes Web/Audio$ –  Kendor Apr 30 '12 at 14:23
    
@Kendor: It might be due to the location of the awk interpreter on your system. I created this script on a shell account off of my shared hosting account, and on that system, awk was located at /usr/bin/awk. However, the usual location for awk is /bin/awk, I think. You can check on your system by running which awk, then change the first line in the script to match the location displayed. –  Lèse majesté Apr 30 '12 at 15:38
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.