The “mv” command is generally used in Linux to rename a single file. But sometimes, it requires to rename a batch of files. Many utilities exist in Linux to do this task. Some of them are “rename”, “imv”, “qmv”, “mmv”, etc. These are not installed in Linux by default. These utilities are required to install to rename a batch of files. The batch of files can also be removed using the built-in “mv” command with the loop or other commands. The different ways to rename a batch of files in Linux are shown in this tutorial.
Rename Multiple Files Using the “MV” Command
Using the “mv” command is the easiest way to rename multiple files because this is a built-in command of Linux and it is not required to install before use. One or more files can be renamed using the “mv” command with a loop. The method of renaming multiple files using the “mv” command is shown in the following example.
Example: Rename the File Using the “For” Loop
Create a Bash file with the following script that renames the extension of all text files into doc files using the “for” loop and “mv” command. The list of all text files is printed first. Next, the “for” loop is used to iterate the list of all text files and change the extension of each text file into a doc using the “mv” command.
#!/bin/bash
#print the list of all text files
ls *.txt
#Iterate the loop to read all text files
for value in `ls *.txt`;
do
#Read the basename of the file
filename=`basename $value .txt`
#Rename all files to doc files
mv $value $filename.doc;
done
#Print all doc files
ls *.doc
Output:
The following output appears after executing the previous command. According to the following output, there are four text files in the current location which are renamed to doc files:
Install the Rename Utility
The rename utility is another option of Linux to rename a batch of files using a regular expression pattern. If the rename utility is not installed by default, run the following command to install it:
Some uses of the “rename” command are mentioned in the following examples.
Example 1: Rename the Extension of the Batch of Files
The method of renaming the extension of doc files into docx files using the “rename” command is shown in this example.
The following command changes the “.doc” extension of all doc files into “.docx”:
Next, the “ls” command checks whether the task is done properly or not.
Output:
The following output appears after executing the previous commands. According to the following output, the extension of four files is changed into a “.docx” extension. These are t1.docx, t2.docx, t3.docx, and t4.docx.
Example 2: Rename the Particular Files with the Extension into Uppercase
The method of changing the name of all files with the extension that starts with the word “test” into all uppercase letters using the “rename” command is shown in this example.
The following command prints the list of all files and folders of the current location:
The following command renames the name of all files into uppercase which starts with the word “test”:
Next, the “ls” command checks whether the task is done properly or not.
The following output appears after executing the previous command. According to the following output, there are four files in the current location that starts with the word, “test”. These are test.txt, testdata.txt, testdata2.txt, and testfile.txt. These filenames are changed into all uppercase letters:
Install the MMV Utility
The mmv is another utility of Linux to rename a batch of files using wildcards. Any part of the filename can be added or removed using the “mmv” command. If the mmv utility is not installed by default, run the following command to install it:
One use of the “mmv” command is mentioned in the following example.
Example: Rename the Extension of the Batch of Files
The method of changing the extension of all text files from uppercase letters to lowercase letters using the “mmv” command is shown in this example.
The following command prints the list of all files and folders of the current location:
The following command changes the “.TXT” extension of all text files into “.txt”:
Next, the “ls” command checks whether the task is done properly or not.
Output:
The following output appears after executing the previous command. According to the output, the extensions of three text files are changed:
Install the Renameutils in Linux
The renameutils is another utility of Linux to rename a batch of files. If the renameutils utility is not installed by default, run the following command to install it:
The “qmv” is one of the commands of the renameutils utility that renames the files of a directory using the editor. The use of the “qmv” command is mentioned in the following example.
Example: Rename the Batch of Files Using the QMV Editor
The temp folder is selected here to rename the files of this folder. The following command prints the list of all files and folders of the temp folder:
The following command opens the files of the temp folder in the editor for editing:
The following similar editor is opened after executing the previous command. Here, the extensions of two text files are changed. These are t2.doc and t4.doc:
Next, the “ls” command checks whether the task is done properly or not.
Output:
The following output appears after executing the previous commands. According to the output, the extensions of two text files are changed:
Conclusion
The different ways of renaming the batch of files in Linux are shown in this tutorial using the “mv”, “rename”, “mmv”, and “qmv” commans. We hope that the Linux users will be able to rename a batch of files after reading this tutorial properly.