rename all the time. It is pretty simple, but hopefully you know basic regex:
$ rename s/"SEARCH"/"REPLACE"/g *
This will replace the string SEARCH with REPLACE in every file (that is,
/g means global, so if you had a "SEARCH SEARCH.jpg", it would be renamed "REPLACE REPLACE.jpg". If you didn't have /g, it would have only done substitution once, and thus now named "REPLACE SEARCH.jpg". If you want case insensitive, add /i (that would be,
/ig at the end).
With regular expressions, you can do lots more.
Here are a few examples:
$ rename s/'^'/'MyPrefix_'/ * ---> document.pdf renamed as MyPrefix_document.pdf
$ rename s/'$'/'_MySuffix'/ * ---> document.pdf renamed as document.pdf_MySuffix
Also you can remove unwanted strings. Let's say you had 20 MP3 files named like
CD RIP 01 Song.mp3 and you wanted to remove the "CD RIP" part, and you wanted to remove that from all of them with one command.
$ rename s/'^CD RIP '// * ---> CD RIP 01 Song.mp3 as 01 Song.mp3
$ rename s/'\.pdf$'/'.doc'/ * will change Something.pdf into Something.doc (The reason for the backslash is,
. is a wildcard character in regexp so
\.pdf only matches the exact string
.pdf. Also very important to note, if you are not familiar with BASH, you must put backslashes in SINGLE quotes! You may not omit quotes or use double quotes, or bash will try to translate them. To bash
.. (But double-quotes and backslashes are used, for example "\n" for a newline, but since "." isn't a valid back escape sequence, it translates into ".")
Notice the extra space in
'^CD RIP ', without the space all files would have a space as the first character of the file. Also note, this will work without the
^ character, but would match 'CD RIP ' in any part of the filename. The ^ guarantees it only removes the characters if they are the beginning of the file.
Also you can use
/''/ instead of
//, to indicate you want to remove the string, both are equivalent in terms of end results. Actually, you can even enclose the whole string in quotes
's/Search/Replace/g' is the same as
s/Search/Replace/g to BASH. You just have to be careful about special characters (and spaces).
I suggest using the
-n option when you are not positive you have the correct regular expressions. It shows what would be renamed, then exits without doing it. For example:
$ rename -n s/'One'/'Two'/g * will list all changes it would have made, had you not put the
-n flag there. If it looks good, press Up to go back, then erase the
-n and press Enter (or replace it with
-v to output all changes it makes).