When we want to rename a file (with Linux or Mac OS), we generally go to the directory of the file and we rename the file. Or for those who prefer doing with the terminal, we use mv
And how about multiple file ? How about renaming all episode of Game of Throne ?
- How can we remove [TOTO] for every file name ?
- How can we rename secretFile for every file name ?
- How can we remove the extension for every file name ?
- How can we upper case every file name ?
- and so on …
we generally do
- one by one
- or a bash script
To rename multiple files, we will use rename. But what’s rename ?
Rename is a program that renames files according to modification rules specified on the command line.
Install rename program
To install rename, we will use homebrew (a package manager for OSX. With homebrew we can install a lot of applications and libraries that would require manual compilation)
- open the terminal
- enter “brew install rename”
Or use Cakebrew (The Mac App for Homebrew)
Now let’s rename:
Append the string argument you supply to every filename: rename -a ‘ toto’ *
(-a: append the string argument you supply to every filename)
Prepend the string argument you supply to every filename: rename -A ‘ toto’ *
(-A: prepend the string argument you supply to every filename)
Convert file names to all lower case: rename -c -f *
(-c: convert file names to all lower case)
(-f: rename even when a file with the destination name already exists)
Convert file names to all upper case: rename -C -f *
(-c: convert file names to all upper case)
Remove the last extension from a filename, if there is any: rename -x *
Replace all sequences of whitespace in the filename with single underscore characters: rename –nows *
Replace each underscore in the filename with a space: rename –rews *
Perform a simple textual substitution of “from” to “to”:
rename -s ‘want to change’ ‘to what we want’ *
Add increment number for each file in the list from 1: rename -N …01 -X -e ‘$_ = “$_-$N”‘ *
(-X stand for keep the extension)
(-e stand for expression)
($_ is the name of the file in the expression)
($N is the incrementation number in the expression)
Add increment number for each file in the list from 3: rename -N …03 -X -e ‘$_ = “$_-$N”‘ *
Throw away all the existing filenames and simply number the files from 1: rename -N …01 -X -e ‘$_ = “FileName-$N”‘ *
That was only the surface of the iceberg. To see other parameter use:
Cool isn’t it ?
So let rename it !