Many Linux distributions have a bulk rename utility to rename many files at once.
It’s simply named rename and is a Perl program. You can use it like a command (but it isn’t a command).
The syntax of rename is very simple:
rename [-v] [-n] [-f] perlexpr [filenames]
- v,n,f: options:
- v: verbose, prints everything it does in terminal
- n: no act (dry-run), simulates the result of the command. I recommend using it before you run it without “n” option.
- f: force, force overwrite existing files
- perlexpr: regular expression to indicate the string to substitute and with which string
- filenames: indicates which files to process
To understand better, look at the following examples of bulk rename.
rename -v 's/.sh/.sh.bak/g' *.sh
in this way you rename the file extension of any files contain the string .sh with the new string .sh.bak.
The meaning of every part of the regular expression s/.sh/.sh.bak/g is the following:
- s = substitute / replace
- .sh = string to replace
- .sh.bak = new string
- g = global, run the replacement for any occurency (if you don’t use “g”, only the first occurency will be replaced).
rename -nv 's/Image/Italy - /' Image*.
in this way you replace the string “Image” with “Italy – “* in every files that start with “Image”
ATTENTION: the “-n” option simulates the action, it’s a dry-run. If you run the same command without the “-n” option the result will be the following:
Image01.png renamed as Italy - 01.png Image02.png renamed as Italy - 02.png Image03.png renamed as Italy - 03.png Image04.png renamed as Italy - 04.png Image05.png renamed as Italy - 05.png Image06.png renamed as Italy - 06.png Image07.png renamed as Italy - 07.png Image08.png renamed as Italy - 08.png Image09.png renamed as Italy - 09.png Image10.png renamed as Italy - 10.png Image11.png renamed as Italy - 11.png Image12.png renamed as Italy - 12.png
rename -v 's/Film - //' Film\ -\ *.
in this way you trim the string “Film – “ in all files that start with “Film – “*. You replace the string with nothing.
Converting UPPER CASE in lower case and the opposite
You can use rename utility to convert filenames in lower case or in UPPER CASE using the command in this following way:
rename 'y/A-Z/a-z/' BLOG\*.\*
with this command you convert in lower case every filenames that start with “BLOG”.