- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Renaming Linux Files in Batches
Introduction
If we want to rename any file in Linux, we use “mv” command. But mv command can only rename one file at a time, it can not rename multiple files at one time in terminal. If we want to rename multiples files then we have to use mv command in different way . Also there are other commands available like “rename” , “mmv”, “renameutils” etc. Though some of these commands are not installed default in Linux , we need to install them separately.
In this artile, let us understand each command with some example.
Approach 1: Using “mv” command
We already know that mv command is used to rename single file but we can use mv command in bash script to rename multiple files.
Let us create some .txt files in Linux.
$ touch 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt
Then use below bash script to rename all .txt files to .odt files.
Command
ls *.txt #Print all text file names for file in `ls *.txt`; do filename=`basename $file .txt` mv $file $filename.odt; done ls *.odt #Print all odt file names
Run the bash script
$ bash multiple_rename.sh
Output
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.odt 1.odt 2.odt 3.odt 4.odt 5.odt 6.odt 7.odt 8.odt 9.odt
Now, we can see all files are renamed to .odt files.
Command
$ ls
Output
10.odt 1.odt 2.odt 3.odt 4.odt 5.odt 6.odt 7.odt 8.odt 9.odt multiple_rename.sh
Approach 2: Using “rename” command
We can take help from rename command to rename files in batches. If rename command is not present by default in Linux, then we can install it by using below command.
$ sudo apt install rename
Then we can issue “rename” in linux terminal and check below output to confirm if command is installed correctly.
Command
$ rename
Output
Usage: rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E *perlexpr*]*|*perlexpr* [ *files* ]
Let us now rename all .odt files to .docx files using below command.
Command
$ rename 's/\.odt/\.docx/' *.odt
Here is the end result of previous command.
$ ls
Output
10.docx 1.docx 2.docx 3.docx 4.docx 5.docx 6.docx 7.docx 8.docx 9.docx
Approach 3: Using “mmv” command
This command may not be installed by default in Linux.
If we get below message after giving “mmv” command , that means command is not installed
$mmv
The program 'mmv' is currently not installed. You can install it by typing: sudo apt install mmv
Then we should install it.
$ sudo apt install mmv
Confirm the installation by providing command “mmv” in terminal.
Now, let us convert all .docx files to .png files using “mmv” command. We can use below syntax.
Command
$ mmv '*.docx' '#1.png'
Here is the end result of previous command.
$ls
Output
10.png 1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png
Approach 4: Using “qmv” command
To use qmv we need to install renameutils as this command is not be installed by default in Linux.
Command
$ sudo apt install renameutils
Now, let us give only “qmv” command.
$ qmv
Output
10.png 10.png 1.png 1.png 2.png 2.png 3.png 3.png 4.png 4.png 5.png 5.png 6.png 6.png 7.png 7.png 8.png 8.png 9.png 9.png multiple_rename.sh multiple_rename.sh [ Read 11 lines ] ^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos ^Y Prev Page M-\ First Line ^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line ^V Next Page M-/ Last Line
Here we can see two columns, left side is the original files names and right side is the modified files names. We can edit right side column as per our need. For example, we have modified two files to .odt
10.txt to 10.odt 1.png to 1.odt
See the change below
10.png 10.odt 1.png 1.odt 2.png 2.png 3.png 3.png 4.png 4.png 5.png 5.png 6.png 6.png 7.png 7.png 8.png 8.png 9.png 9.png multiple_rename.sh multiple_rename.sh
Use required command to come out from that editor. Then we see below output.
Plan is valid. 10.png -> 10.odt 1.png -> 1.odt Regular rename 10.png -> 10.odt 1.png -> 1.odt
Let us confirm if the change is happened or not.
$ ls
Output
10.odt 1.odt 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png
Conclusion
From this article, we have learned many new commands to rename linux file names in bulk. Based on our requirements we can use any of these commands. This also helps us to do the file renaming Linux in a faster way.