Sign up ×
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 – 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
Since you're adding, start at the end and count down, so you don't have to deal with clashes between new and old names. – Peter Cordes Mar 1 at 9:54

1 Answer 1

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

#!/usr/bin/awk -f
    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:


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


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 2: BEGIN: not found 3: regex: not found Warning: unknown mime-type for "mkdir new" -- using "application/octet-stream" Error: no such file "mkdir new" 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
@Kendor: you ran sh on an awk script. If you'd made the file executable (with chmod), and run it, it would have invoked awk through the #! mechanism. Instead, you ran sh on it, and it's not a valid sh script. – Peter Cordes Mar 1 at 9:52

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