As you already know, we use the command mv for renaming or moving files and directories on Unix-like operating systems. But the mv command will not support renaming multiple files at the same time. Do not worry. In this tutorial, we will learn how to rename multiple files at the same time using the command “Mmv” on Linux. This command is used to move, copy, add and rename files using standard wildcards on Unix-like operating systems. Renaming multiple files at once in Linux
$ sudo apt-get install mmv
Let’s say you have the following files in your current directory.
$ ls a1.txt a2.txt a3.txt
Now you want to rename all files starting with the letter “a” to “b”. You can of course do it manually after a few seconds. But consider, do you have hundreds of files and want to rename them? This is a rather laborious process. Here’s the command mmv comes to the rescue.
To rename all files from the letter “a” to “b”, simply run:
$ mmv a * b # 1
Let’s check if the files have been renamed or not.
$ ls b1.txt b2.txt b3.txt
As you can see, all files start with the letter “a” (ie a1.txt, a2.txt, a3.txt) are renamed to b1.txt, b2.txt, b3.txt.
In the above example, the first parameter (a *) is the pattern “from” and the second parameter is “to” (b # 1). As per the above example, mmv will look for any filenames with the letter “a” and rename the associated files according to the second parameter, which is the “to” pattern. We use wildcards like ‘*’, ‘?’ and ‘’to match one or more arbitrary characters. Remember that you must avoid wildcards, otherwise they will be expanded by the shell and mmv will not understand them.
“# 1” in pattern “to” is a wildcard index. It matches the first pattern found in the “from” pattern. “# 2” in the “to” pattern will match the second wildcard, and so on. In our example, we only have one pattern (asterisk), so we write # 1. And, the hash mark must also be escaped. Alternatively, you can enclose patterns with quotes.
You can even rename all files with a specific extension to a different extension. For example, to rename everything .txt- files in format .doc in the current directory, just run:
$ mmv *. txt # 1.doc
Here’s another example. Let’s say you have the following files.
$ ls abcd1.txt abcd2.txt abcd3.txt
Do you want to replace the first occurrence abc on xyz in all files in the current directory. What would you do?
$ mmv '* abc *' '# 1xyz # 2'
Note that in the example above, I have included the patterns in single quotes.
Let’s check if “abc” is actually replaced with “xyz” or not.
$ ls xyzd1.txt xyzd2.txt xyzd3.txt
See? Files abcd1.txt , abcd2.txt and abcd3.txt were renamed to xyzd1.txt , xyzd2.txt and xyzd3.txt …
Another notable feature of the mmv command is that you can simply print the output instead of renaming the files with the option -n, as shown below.
$ mmv -na * b # 1 a1.txt -> b1.txt a2.txt -> b2.txt a3.txt -> b3.txt
This way you can just check what the mmv command actually did before renaming the files.
For more information, see the man pages.
$ man mmv
Thunar File Manager has a built-in option bulk rename default. If you are using thunar it is much easier to rename files than using the mmv command.
Thunar is available in the default repositories for most Linux distributions.
To install it on Arch based systems, run:
$ sudo pacman -S thunar
On RHEL CentOS:
$ sudo yum install thunar
$ sudo dnf install thunar
$ sudo zypper install thunar
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install thunar
Once installed, you can launch the bulk rename utility from the menu or from the application launcher. To run it from the terminal, use the following command:
$ thunar -B
This is what a bulk rename looks like.
Click the plus sign and select the list of files you want to rename. Bulk rename can rename file name, file suffix, or file name and suffix. Thunar currently supports the following bulk renames:
- Insert date or time
Paste or overwrite
Find and replace
Upper case Lower case
When you select one of these criteria from the selection list, you will see a preview of the changes in the “New Name” column, as shown in the following screenshot.
After selecting the criteria, click “Rename Files“, to rename the files.
You can also open the bulk renamer from Thunar by selecting two or more files. After selecting the files, press F2 or right-click and select “Rename“ …
And that’s all for now. Hope this was helpful. Be in touch!