Today we are going to guide you on how to rename Linux file with some simple methods.
You might already be aware about the mv command used to rename or move folders at Linux. It is also used to rename Linux directory or Unix file.
But the mv command cannot support renaming numerous files at once.
It only supports renaming one file at a time and this can be time consuming if you want to rename multiple files at Linux.
All the methods that we are going to discuss about are tested in Ubuntu and will work on all Linux operating systems.
So, let us begin and understand the process of renaming files at once in Linux.
Read Also: Best Linux Distros for Gaming, Developers and Security in 2020
Method 1 – MMV Command
In Linux, you can move, copy, rename and append files in bulk using the mmv utility.
It is done by the standard wildcards in Unix-like operating systems.
Let us consider an example of the following files whose name you wish to change.
Ex: a1.txt a2.txt a3.txt
If you wish to rename all files that starts with letter “a” to “b” then this can be done manually but if you have 100 of files then it can take to much time in renaming all.
So, in this case we are going to use the mmv command.
Simply run the below command to rename all files starting with letter “a” to “b”.
Command: $ mmv a\* b\#1
Now you can go and check if all the files starting from the letter ‘a’ have been renamed to b1.txt, b2.txt, b3.txt.
So, the main job of mmv command is to look for filenames starting with letter ‘a’ and rename it.
Wildcards such as ‘*’, ‘?’, ‘[]‘ are used to match 1 or more subjective characters.
It is important to remember not to escape the wildcard characters, or else they will be extended by the shell and mmv command will not be able to understand them.
The wildcard index is ‘#1′ in the ‘to’ pattern.
In the above example we only have one wildcard that is the reason we write #1.
Renaming files to different extension is also possible like, .txt files to .doc file format.
Simply run the below command:
$ mmv \*.txt \#1.doc
In case you have the following files:
$ ls
abcd1.txt abcd2.txt abcd3.txt
Here if you want to change the “abc” with “xyz” then follow the below command:
$ mmv ‘*abc*’‘#1xyz#2’
The result should be like this
$ ls
xyzd1.txt xyzd2.txt xyzd3.txt
One more feature of the mmv command is that you can print output instead of renaming the files using -n option.
Example:
$ mmv -n a\* b\#1
a1.txt -> b1.txt
a2.txt -> b2.txt
a3.txt -> b3.txt
So, these were the simple steps to follow while using the mmv command to rename file Linux command.
Method 2 – Pre-Installed Rename Command
In this option we will be using the rename utility for name replacement. This command mostly comes preinstalled in most Unix-like operating systems.
In case it is not available then run the below command to install it on Debian-based systems:
$ sudo apt install rename
Let’s take the names of the following files for example:
$ ls
abcd1.txt abcd2.txt abcd3.txt
Now run the below command to replace the first occurrence of abc with xyz.
$ rename ‘s/abc/xyz/’ *
The result should look like
$ ls
xyzd1.txt xyzd2.txt xyzd3.txt
Many times, you might just need to print instead of renaming files, in that case using the -n flag to display which renames would occur without performing them:
$ rename -n ‘s/abc/xyz/’ *
rename(abcd1.txt, xyzd1.txt)
rename(abcd2.txt, xyzd2.txt)
rename(abcd3.txt, xyzd3.txt)
Notice how the above commands did not make changes, instead just showing you the displays, which renames would occur.
By using the -f flag you can impose renaming task even if the operation would overwrite current files.
Use the below command to use -f flag.
$ rename -f ‘s/abc/xyz/’ *
If you don’t wish to overwrite files, then you can also convert them from upper or lowercase letters or vice versa.
This is done to prevent the “already exists” errors.
Use the below commands to rename filename to lower case:
$ rename ‘y/a-z/A-Z/’ *
The changes made will look somewhat like this:
$ ls
ABCD1.TXT
ABCD2.TXT
ABCD3.TXT
So, these letters have been changed from lowercase to uppercase.
Method 3 – Get Help of Renameutils
The renameutils method is used on set of programs designed to batch rename file command line and directories faster and easier.
The way has the following programs:
- qmv (quick move)
- qcp (quick copy)
- imv (interactive move)
- icp (interactive copy)
- deurlname (delete URL)
We will discuss the first two.
For this first you need to install renameutils.
It is available in the default repositories of most Linux distributions.
Run the following command to install Arch-based system:
$ sudopacman -Syurenameutils
Debian-based systems:
$ sudo apt install renameutils
qmv (quick move)
This will let edit the files by opening them in your default text editor.
Let’s consider the file name as ‘Labtech’
Run the following command:
$ ls Labtech/
abcd1.txt abcd2.txt abcd3.txt
To rename the filenames in the ‘Labtech’ directory, simply run:
$ qmv Labtech/
Now go back and preview the names of the file and if they are changed or not.
You will see two columns, after you open the files.
The left one will show the source filename and the right one will show destination names.
Here you can simply rename all the output names on the right side.
After you are done renaming them, do not forget to save, and quit the file.
qcp (quick copy)
This program is quite like qmv, but instead it copies files rather than renaming them.
But do not worry it also keeps both original and replicate files.
Command:
$ qcp Labtech/
Method 4 – Use Vimv to Rename Multiple Linux Files
In this method we use vimv. It is a command line utility to rename many files using Vim editor.
To install Vimv go to:
$ git clone https://github.com/thameera/vimv.git
Next copy vimv binary to your $PATH, for example /usr/local/bin/.
Command:
$ sudo cp vimv/vimv /usr/local/bin/
Lastly, execute it
$ sudochmod +x /usr/local/bin/vimv
Now you can go to the directory and run the following command to edit the filenames.
Find the filenames in Vi editor.
To switch to interactive mode simply press i button, then edit the filenames as the way you change text in Vi editor.
After you are done with editing press ESC key and type :wq to save and exit.
Check the files inside directory that if they are renamed or not.
Method 5 – Give Try to Emacs
In this method we are going to use Emacs.
If you have an operating system with Emacs editor, then the following steps can be performed.
First open your Emacs editor.
Next press Alt+x and type “dired” then hit the Enter button to switch to wdired-mode.
Enter to the desired directory that has the files to rename and hit ENTER key.
Next to switch to read-write mode, press Ctrl+x and Ctrl+q.
Now you can rename the files and after you are done, press Ctrl+c and Ctrl+c twice to save the changes.
In case you want to abort the changes, press Ctrl+c and Ctrl+k.
Method 6 – Use Thunar File Manager
You can do this method by using Thunar file manager. This manager has a built-in bulk rename option by default.
It is available in the default repositories of most of the Linux distributions.
Run the following to install Arch-based system:
$ sudopacman -S thunar
On RHEL, CentOS:
$ sudo yum install thunar
On Fedora:
$ sudodnf install thunar
On openSUSE:
$ sudozypper install thunar
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install thunar
After the installation, you can start bulk rename utility from the application launcher or the menu.
If you want to launch it from Terminal, run the following command:
$ thunar -B
Now you will see the page Bulk Rename- Rename Multiple Files
With options like, Insert Date or Time, Numbering, Insert or Overwrite, Uppercase / Lowercase, Remove Characters, Search & Replace.
Select the option that you wish to choose and after editing you will be able to preview it also.
Now, click on Rename Files option to save.
Conclusion
So, here was the full guide on how you can rename file Linux command. The whole guide will show you different methods that you can use to rename multiple files at once on Linux.
Also go through all the commands for different methods and use them effectually.
I hope after reading the article you will have enough knowledge for renaming files using all the 6 methods.