How to batch rename files in bash

bash

I've got some files like this:

database1-backup-01-01-2011.sql
database2-backup-01-01-2011.sql

…etc. I want to rename them to add AM, like this:

database1-backup-01-01-2011-AM.sql
database2-backup-01-01-2011-AM.sql

What's the most concise way to do that from the bash shell?

Best Answer

Another option:

for i in *.sql ; do
    mv -v $i ${i%.sql}-AM.sql
done

This loops through all the .sql files and renames them to end in -AM.sql instead.

PROTIP: Use $(command) instead of `command` in your scripts (and command-lines), it makes quoting and escaping less of a nightmare.

Related Question