This is where the rename tool comes in. Every Linux system comes up with the rename tool built-in. The rename tool supports a wide range of features, for example, rename group of files, rename to lowercase/uppercase, even overwrite files but most of all, controlling the behavior using Perl expression!
Let’s have a look at how to rename a batch of files with the rename command.
The rename tool requires Perl installed to perform. It’s a part of Perl. Generally, it’ located under /usr/bin.
The command structure of the rename tool is as follows.
Rename also supports a handful of optional arguments. However, the Perl expression must be present because that’s what rename follows when performing the actions.
Here’s what those arguments mean.
-v: Echo file names that have been successfully renamed
-n: What file would have been renamed
-f: Force overwrite
For demonstration, I’ve created a bunch of demo text files with the file extension “.txt”.
Let’s rename the file extension of all these files into “.random”.
Check out the result.
Here, there are 2 parts of the argument. The first one is a Perl expression and the second one tells rename which file to operate on.
File(s) to be renamed
There is an interesting function rename offers. Instead of renaming, you can check out which file(s) will be renamed if the operation was run for real. For this purpose, use the “-n” flag.
Check file name changes
Want to see rename to display output as it performs its actions? Use the “-v” flag. The behavior is quite similar to the “-n” argument. In this case, however, it actually performs the renaming of the file.
Renaming lowercase to uppercase and vice-versa
In some situations, you may want to batch rename the files from lowercase to uppercase or, uppercase to lowercase. To change the case of the target file names, let’s use the following commands.
The game here is the Perl expression. Run the following command for turning all the lowercase characters of the file names into uppercase.
Note that even the file extension will be changed to uppercase. To change from uppercase to lowercase, run the following command.
Rename with the capitalized first letter
To make only the first letter of the file name to be capital, use the following command instead.
Overwriting existing file(s)
In certain situations, renaming the files will conflict with the pre-existing file(s) in the same directory. If you want to overwrite the old file(s) with the new one, then add the “-f” argument to rename.
Note that in this situation, the file permission comes into play. If you don’t have permission to operate on those files, it won’t succeed.
The functionality of the rename command is quite simple. It’s not a complex tool itself. However, the true magic hides in the power of Perl expression. It’s only up to you how you want rename to perform. No matter whatever crazy expression you come up with, rename got your back.
If you want to know all the available options for rename, check out the help page.
For full in-depth info, check out the man page.