Tips

How to Rename Multiple Files at Once in Linux

Rename command can be used to rename multiple files in Linux at once. Here are some practical scenarios in which you can use the rename command.

Abhishek Prakash
Abhishek Prakash

Table of Contents

I believe you already know how to rename files in Linux with mv command. You simply use it in the format:

mv old_file_name new_file_name

There is nothing wrong with this method. That’s the standard way after all.

But what about the situation where you have to rename several files. Using mv command one by one would take plenty of time and should surely be avoided.

You can use the find exec command option with mv command to automate this process. But let me tell you an even simpler way of renaming files based on regex pattern.

Rename command in Linux

There is a command line utility called rename that allows you to rename all the files that match a certain pattern in Perl regex form. The rename command only works on the filename, not the file itself.

This is the syntax rename command follows:

rename [options] perlexpr [files]

This 'rename command' has the following options:

  • -v : Verbose mode.
  • -n : No action. Show the files that would be renamed but don’t rename the files.
  • -o : No overwrite. Don’t overwrite existing files.
  • -f : Force. Overwrite existing files.
  • -s : Do not rename the symlink but its target

Do note that rename command is not a standard utility that are pre-installed on the Linux distributions like mv or cp command. You may have to install it. If you are a sudo user in Ubuntu or Debian, you can use this command to install rename:

sudo apt install rename

Now you must be wondering how to rename files with the rename command. Let me show two practical scenarios where this command will save you a lot of time.

Scenario 1: Replace a Character With Another in all the Filenames in Linux

Imagine if you have white spaces in several filenames. It’s a nightmare to handle spaces in filenames in Linux. This is why no experienced user ever names a file with spaces in it.

Naturally, you would want to remove the spaces and replace them with an underscore (_). The rename command will be pretty handy here. Let’s see how.

To replace the spaces with underscores in the names of all the files in the current directory, all you have to do is to use renam command in this way:

rename 's/ /_/g' *

If you are even remotely familiar with regex, you would understand that s/ /_/g is searching for space and replacing all occurrences of space with _.

The interesting part is the asterisk(*). The asterisk tells you to rename matching files in the current directory.

But what if you want to rename all the matching files in sub-directories as well? In that case, you can use two asterisks like this:

rename 's/ /_/g' **

Scenario 2: Change Extensions of Multiple Files at Once in Linux

Here is another scenario for you. You have several files with names like my_file.xyz and you want to rename these files so that they become something like my_file.abc.

To change the extensions of multiple files at once, you can use the rename command in the following manner:

rename 's/\.xyz$/.abc/' **

Let me explain the above command to you.

‘s/\.xyz$/.abc/’ : This is a regular expression that means replace the .xyz character at the end of the line with .abc. The additional \ is used to escape the . otherwise it would mean to match any character before xyz.

** means look in the current directory and its sub-directories.

Did you like the rename command?

Not many people are aware of the rename command. This may be also because of the fact that rename is not a defacto command on many Linux systems.

However, I find this command pretty handy while dealing with renaming of several files simultaneously.

How about you? Have you used rename command in the past? How do you prefer to rename several files at once?



Join the conversation.