How To Rename Multiple Files At Once In Linux – OSTechNix

In this tutorial, we will learn how to rename multiple files at once in Linux using various tools. All examples provided here are tested in Ubuntu, however they should work on any Linux operating systems.

As you may already know, we use mv command to rename or move files and directories in Linux and Unix-like operating systems.

But, the mv command won't support batch renaming files at once. It can rename only one file at a time. What would you do if you wanted to rename multiple files at a time? Worry not!

There are a few other utilities available to batch rename files in Linux.

There could be many commands and utilities to a bulk rename bunch of files. As of writing this, I am aware of 8 different ways to batch rename files. I will keep updating the list if I come across any new method in future.

Method 1 - Batch rename files using mmv

The mmv utility is used to move, copy, append and rename files in bulk using standard wildcards in Linux and Unix-like operating systems. It is available in the default repositories of Debian-based systems.

To install mmv on Debian, Ubuntu, Linux Mint, and Pop OS, run the following command:

$ sudo apt-get install mmv

Let us say, you have the following files in your current directory.

$ ls
a1.txt a2.txt a3.txt

Now you want to rename all files that starts with letter "a" to "b". Of course, you can do this manually in few seconds.

But just think if you have hundreds of files and want to rename them? It is quite time consuming process. Here is where mmv command comes in help.

To rename all files starting with letter "a" to "b", simply run:

$ mmv a\* b\#1

Let us check if the files have been renamed or not.

$ ls
b1.txt b2.txt b3.txt

As you can see, all files starts with letter "a" (i.e a1.txt, a2.txt, a3.txt) are renamed to b1.txt, b2.txt, b3.txt.

Explanation

In the above example, the first parameter (a*) is the 'from' pattern and the second parameter is 'to' pattern (b#1).

As per the above example, mmv will look for any filenames staring with letter 'a' and rename the matched files according to second parameter i.e 'to' pattern.

We use wildcards, such as ‘*’, ‘?’ and ‘[]‘, to match one or more arbitrary characters. Please be mindful that you must escape the wildcard characters, otherwise they will be expanded by the shell and mmv won’t understand them.

The ‘#1′ in the ‘to’ pattern is a wildcard index. It matches the first wildcard found in the ‘from’ pattern. A ‘#2′ in the ‘to’ pattern would match the second wildcard and so on.

In our example, we have only one wildcard (the asterisk), so we write a #1. And, the hash sign should be escaped as well. Also, you can enclose the patterns with quotes too.

You can even rename all files with a certain extension to a different extension. For example, to rename all .txt files to .doc file format in the current directory, simply run:

$ mmv \*.txt \#1.doc

Here is an another example. Let us say you have the following files.

$ ls
abcd1.txt abcd2.txt abcd3.txt

You want to replace the the first occurrence of abc with xyz in all files in the current directory. How would you do?

Simple.

$ mmv '*abc*' '#1xyz#2'

Please note that in the above example, I have enclosed the patterns in single quotes.

Let us check if "abc" is actually replaced with "xyz" or not.

$ ls
xyzd1.txt xyzd2.txt xyzd3.txt

See? The files abcd1.txt, abcd2.txt, and abcd3.txt have been renamed to xyzd1.txt, xyzd2.txt, and xyzd3.txt.

Another notable feature of mmv command is you can just print output instead of renaming the files using -n option like below.

$ mmv -n a\* b\#1 
a1.txt -> b1.txt
a2.txt -> b2.txt
a3.txt -> b3.txt

This way you can simply verify what mmv command would actually do before renaming the files.

For more details, refer man pages.

$ man mmv

Method 2 - Bulk rename files using rename utility

The rename utility will rename given files by substituting the first occurrence of expression in their name by replacement.

The rename command comes preinstalled in most Unix-like operating systems. If it is not available by default, run the following command to install it on Debian-based systems:

$ sudo apt install rename

For instance, I have the following files in the current directory.

$ ls
abcd1.txt abcd2.txt abcd3.txt

Let us replace the the first occurrence of abc with xyz wherever found. To do so, run:

$ rename 's/abc/xyz/' *

Now, verify if the changes have been made with ls command.

$ ls
xyzd1.txt xyzd2.txt xyzd3.txt

Sometimes, you might to just print output instead of renaming the files. If so, use -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)

As you can see, the above command didn't make any changes, instead just displays which renames would occur.

You can force renaming task even if the operation would overwrite existing files using -f flag like below.

$ rename -f 's/abc/xyz/' *

If you don't want to overwrite the files, you can simply convert them to upper or lowercase letters (and vice versa) to prevent "already exists" errors.

To convert all filenames to lower case, so:

$ rename 'y/a-z/A-Z/' *

Let us check if the changes have been made.

$ ls
ABCD1.TXT ABCD2.TXT ABCD3.TXT

Yes, the letters in the filenames have been changed from lower case to upper case.

Similarly, to convert filenames to lower case, run:

$ rename 'y/A-Z/a-z/' *

We can remove all blank lines in a filename as well. For example, I have the following file.

$ ls
Clouds of Sunset.mp3

To remove all blank spaces in the above filename, run:

$ rename "s/ *//g" *

Now, the filename doesn't have any blank spaces.

$ ls
CloudsofSunset.mp3

Replace blank spaces with underscores:

$ rename 's/\s+/_/g' *

You might want to change the file extension, but not rename the filenames. It is also possible. The following command would rename all *.txt files to *.doc.

$ rename 's/\.txt$/.doc/' *.txt

Verify the changes using ls command:

$ ls
abcd1.doc abcd2.doc abcd3.doc

To remove extension in all files matching .txt, run:

$ rename 's/\.txt$//' *.txt

For more details, refer man pages.

$ man rename

Method 3 - Rename files using renameutils

The renameutils is a set of programs that is designed to batch renaming files and directories faster and easier.

Renameutils consists of the following five programs:

  1. qmv (quick move),
  2. qcp (quick copy),
  3. imv (interactive move),
  4. icp (interactive copy),
  5. deurlname (delete URL).

Install renameutils in Linux

Renameutils is available in the default repositories of most Linux distributions. To install it on Arch-based systems, enable the community repository and run:

$ sudo pacman -Syu renameutils

On Debian-based systems:

$ sudo apt install renameutils

Now, let us see some examples.

1. qmv

The qmv program will open the filenames in a directory in your default text editor and allows you to edit them.

I have the following three files in a directory named 'ostechnix'.

$ ls ostechnix/
abcd1.txt abcd2.txt abcd3.txt

To rename the filenames in the 'ostechnix' directory, simply do:

$ qmv ostechnix/

Now, change the filenames as you wish. You will see the live preview as you edit the filenames.

Alternatively, you can cd into the directory and simply run 'qmv'.

Once you opened the files, you will see the two columns as shown in the following screenshot.

Bulk rename files using qmv
Bulk rename files using qmv

The left column side displays the source filenames and the right column displays the destination names (the output filenames that you will get after editing).

Now, rename all the output names on the right side as you wish.

Bulk rename files using qmv
Bulk rename files using qmv

After renaming filenames, save and quit the file.

Finally, you will see the following output:

Plan is valid.
abcd1.txt -> xyzd1.txt
abcd2.txt -> xyzd2.txt
abcd3.txt -> xyzd3.txt
   Regular rename
abcd1.txt -> xyzd1.txt
abcd2.txt -> xyzd2.txt
abcd3.txt -> xyzd3.txt

Now, check if the changes have actually been made using 'ls' command:

$ ls ostechnix/
xyzd1.txt xyzd2.txt xyzd3.txt

See? All files are renamed. Not just files, the renameutils will also rename the directory names as well.

Here is a quick video demo of qmv program:

Bulk rename files using qmv
Bulk rename files using qmv

If you don't want to edit the filenames in dual-column format, use the following command to display the destination file column only.

$ qmv -f do ostechnix/

Where, '-f' refers the format and 'do' refers destination-only.

Now, you will see only the destination column. That's the column we make the changes.

Display the destination file column only in qmv
Display the destination file column only in qmv

Once done, save and close the file.

For more details, refer man pages.

$ man qmv

2. qcp

The qcp program works like qmv, but copies files instead of renaming them. In this case, you will get two instances of same file. That means it will keep both original and duplicate files.

$ qcp ostechnix/

Rename the filenames listed on the right side. Save and quit the file. Finally, verify the changes made using ls command:

$ ls ostechnix/
abcd1.txt abcd2.txt abcd3.txt xyzd1.txt xyzd2.txt xyzd3.txt

For more details, refer man pages.

$ man qcp

3. imv

The imv program allows us to interactively rename the filenames. Obviously, it is not for bulk renaming. You could only rename the files one by one.

$ imv ostechnix/abcd1.txt

Edit the filename as you like and hit ENTER to rename it.

Rename files using imv
Rename files using imv

For more details, refer man pages.

$ man imv

4. icp

The icp program is same as imv, but it copies the files instead of moving them.

Example:

$ icp ostechnix/abcd1.txt

For more use cases and commands, please refer man pages.

$ man icp

I don't know why the developers added these two utilities while we can do the same using mv and cp command.

5. deurlname

The deurlname program removes URL encoded characters (such as %20 representing space) from file names. Some programs, for examples w3m, tend to keep those characters encoded in saved files.

You can this tool for cleaning up the filenames you downloaded from the Internet.

Take a look the the following file.

$ ls
omg%20ponnu%20ily%20kannu.mp3

There are some special characters and numbers in the filename. If you clean it up, just run:

$ deurlname omg%20ponnu%20ily%20kannu.mp3

Now, look how the file name is changed.

$ ls
omg ponnu ily kannu.mp3

The filename is clean and readable.

Refer man pages for more details.

$ man deurlname

Also, refer the project's website given at the end of this guide.

Method 4 - Rename multiple files at once using vimv

As the name says, Vimv is a command line utility to bulk rename files using Vim editor. You can, of course, change the editor by changing the value of $EDITOR environment variable.

To install Vimv, git clone the repository:

$ git clone https://github.com/thameera/vimv.git

Copy the vimv binary to your $PATH, for example /usr/local/bin/.

$ sudo cp vimv/vimv /usr/local/bin/

Finally, make it executable:

$ sudo chmod +x /usr/local/bin/vimv

Now go to the directory and run the following command to edit the filenames.

$ vimv

You will see the filenames in Vi editor. Press i to switch to interactive mode and edit the filenames as the way you edit text in Vi editor. Once done, press ESC key and type :wq to save and exit.

The files inside the directory should be renamed now. Here is a short video demo.

Bulk rename files using Vimv
Bulk rename files using Vimv

For more details, refer the project's GitHub repository given at the end of this guide.

Method 5 - Batch rename files using Emacs

If you have a system with Emacs editor installed, you can do batch renaming easily by following these steps.

1. Open your Emacs editor.

2. Press Alt+x and type the following and hit ENTER to switch to wdired-mode (short for "writable directory editor mode").

$ dired

3. Enter the path to the directory (E.g. /home/sk/ostechnix) which contains the files to rename and hit ENTER key.

4. Then, press Ctrl+x and Ctrl+q to switch to read-write mode.

5. Now, rename the files. Once done, press Ctrl+c and Ctrl+c (two times) to save the changes. To abort the changes, press Ctrl+c and Ctrl+k.

Watch the demo video:

Emacs demo - Bulk rename files using Emacs
Bulk rename files using Emacs

See? It is very simple to rename multiple files at once.

Method 6 - Bulk rename files with Thunar file manager

The Thunar file manager has built-in bulk rename option by default.

Thunar is available in the default repositories of most Linux distributions.

To install it on Arch-based systems, run:

$ sudo pacman -S thunar

On Fedora, RHEL, CentOS, AlmaLinux, Rocky Linux:

$ sudo dnf install thunar

Or,

$ sudo yum install thunar

On openSUSE:

$ sudo zypper install thunar

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install thunar

Once installed, you can launch bulk rename utility from menu or from the application launcher. To launch it from Terminal, use the following command:

$ thunar -B

This is how bulk rename looks like.

Thunar bulk rename utility
Thunar bulk rename utility

Click the plus sign and choose the list of files you want to rename. Bulk rename can rename the name of the files, the suffix of the files or both the name and the suffix of the files.

Thunar currently supports the following Bulk Renamers:

  • Insert Date or Time
  • Insert or Overwrite
  • Numbering
  • Remove Characters
  • Search & Replace
  • Uppercase / Lowercase

When you select one of these criteria from the picklist, you will see a preview of your changes in the New Name column, as shown in the below screenshot.

Bulk rename files using Thunar
Bulk rename files using Thunar

Once you choose the criteria, click on Rename Files option to rename the files.

You can also open bulk renamer from within Thunar by selecting two or more files. After choosing the files, press F2 or right click and choose Rename.

Suggested read:

Method 7 - Rename list of files with KRename

KRename is a batch file renamer which can rename a list of files based on a set of expressions. It allows you to rename large numbers of files by simply adding a sequence of numbers or changing case.

KRename is specially designed for and installed by default in KDE distributions. You can also install it on other Linux distributions as well.

For example, to install KRename on Fedora, simply run:

$ sudo dnf install krename

Launch KRename application from application launcher or menu and add all files to be renamed in the Files section.

Add files in KRename
Add files in KRename

In the Destination section, make sure you've selected "Rename input files" option..

Select rename input files option
Select rename input files option

In the Plugins, you can choose any plugin(s) of your choice, which provides various tags when renaming files.

KRename plugins section
KRename plugins section

In the last section section, you can choose the rename scheme to use. You will see a preview at the bottom based on the scheme you've chosen. Once you're OK with selected options, click the Finish button to rename files.

Bulk rename files with Krename
Bulk rename files with Krename

The renamed files will be saved in the same location.

KRename is a feature-rich renamer utility. It has so many options when renaming files. Most options are self-explanatory.

Method 8 - Rename multiple files and folders using Smart file renamer

Smart File Renamer is a graphical program to rename multiple files at once. Using Smart File rename, you can easily and quickly rename files and directories all at once with couple mouse clicks. It is cross-platform application that supports Linux, macOS and Windows. Unlike the other programs, it is propriety. The free version has only limited functionality.

Smart File Renamer is available as snap package. You can install it using the following command:

$ sudo snap install smart-file-renamer

Once installed, launch it from menu or application launcher. Add the files and directories you want to rename and click the Rename button at the bottom.

Smart file renamer
Smart file renamer

It supports many rules and filters for batch renaming. Just choose them on the right side of the application window and hit the Rename button.

There you go! In this guide, we have discussed 8 methods to bulk rename files in Linux. We have provided both command line and graphical programs to rename multiple files in one go. Just pick that one that suits you and get the job done.

And, that's all for now. Hope this was useful. Do you know any other method to add in this list? Please mention them in the comment section below. I will check and update this guide accordingly.

Resources:

版权声明:
作者:admin
链接:https://www.techfm.club/p/30660.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>