Bulk Rename Files with the Rename Command via the Terminal

5 min read
08 April 2022

Renaming a file, that's simple. It is perhaps one of the first things we learn when we start using a computer. Simply left-click on the name of a file, wait for it to enter edit mode, and type away. Easy. But what if we want to rename thousands or millions of files all at once? It would be an excruciating or even impossible task to complete manually.

Well, fear not! Leveraging the power of the command line, we can easily rename many files all at once. Specifically, we can use the rename command to help us achieve this goal.

Pre-requisites

First things first, here are a few requirements:

  1. Access to the Linux terminal.
  2. Have the rename package installed.

If we are using a Linux or MacOS operating system, we can skip step 1. If we are on Windows and would like to have access to the Linux terminal, setting up the Windows Subsytem for Linux is an alternative option. Although it is possible to bulk-rename with commands in the Windows terminal as well, we will focus on the Linux-based approach in this post.

Once we have access to the Linux command-line, we need to ensure we have the rename package installed. The installation process is quite straight-forward, simply enter the following in the terminal:

										
											
sudo apt update
sudo apt install rename

Bulk Renaming

Let's suppose we have thousands of files to reorganize with the naming convention shown below:

										
											
[DNA] - COL-14(XX51).txt
[DNA] - MYP-21(XX23).txt
[DNA] - SIR-65(XX76).txt
...
[DNA] - BAM-04(XX77).txt
[DNA] - COL-06(XX89).txt
[DNA] - GEN-51(XX09).txt

Example 1: Replacing Substring with Today's Date

Assuming that all of these files contain information on DNA samples, there would be no need to keep the prefix of [DNA] in the file name. Instead, it would perhaps be better if we can add today's date at the beginning with the following command:

										
											
rename -n "s/\[DNA\]/$(date +%F)/" *.txt

NOTE: Ensure to use double quotes to allow the shell to parse the date value properly. Single quotes will not work as expected.

Let's dissect the above. The rename command is accompanied by several arguments. We can use the -n option to simply output the potential file name changes without making any actual modifications, which comes in handy for testing purposes. We define the expression, which in this case consists of finding an exact match for the substring [DNA] (notice that the square brackets are escaped). We then specify to replace the first matched occurrence with today's formatted date $(date +%F). Lastly, we instruct the command to be applied to all .txt files in the directory.

Output:

										
											
2022-04-08 - COL-14(XX51).txt
2022-04-08 - MYP-21(XX23).txt
2022-04-08 - SIR-65(XX76).txt
...
2022-04-08 - BAM-04(XX77).txt
2022-04-08 - COL-06(XX89).txt
2022-04-08 - GEN-51(XX09).txt

Example 2: Removing Substring Based on Pattern

Great! Now let's say the information within the parentheses at the end of the file names should be removed. The same command can be used by simply adjusting the expression and replacement arguments:
										
											
rename -n "s/\(.*/.txt/" *.txt
Here, we match the first parenthesis and every character that follows by using the expression \(.*, replacing all of it with .txt (the extension of the file, which should not be omitted). Essentially, this command would remove the parentheses and the content found inside.

Output:

										
											
2022-04-08 - COL-14.txt
2022-04-08 - MYP-21.txt
2022-04-08 - SIR-65.txt
...
2022-04-08 - BAM-04.txt
2022-04-08 - COL-06.txt
2022-04-08 - GEN-51.txt

Conclusion

The purpose of this post was to provide a quick step by step guide to using the rename command to modify file names in batches. However, we barely scratched the surface in terms of the incredible things we can automate with regular expression. Whether we need to rename a large volume of files for work or we simply want to reorganize the pictures we took on our vacation, this method would definitely be an efficient and time-saving solution.