Beginners Guide for Rename Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

In Linux, the rename command is an amazing utility that allows you to rename single or multiple files at once, based on a set of rules or regular expressions you specify.

Most of the time, the mv command is used to rename files or folders. However, the rename command has more features, but can be harder for a beginner to use, because it requires knowledge of Perl expressions.

In this article, you will learn how to use the rename command in real life with practical examples.

Tutorial Details

Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Internet RequiredYes

Installing the Rename Utility on Linux

Some Linux distributions come with the rename utility built in, but if yours doesn’t, you can install it with one of the following commands:

$ sudo apt install rename                                                                          #On Debian and Ubuntu
$ sudo dnf install prename                                                                       #On Red Hat and Fedora
$ sudo pacman -S rename                                                                       #On Arch and Manjaro

Rename Command Syntax and Options

The rename command takes three arguments: one is the option; the second is the Perl expressions; and the third is the files.

$ rename [OPTION] perlexpr files

The rename command will rename the β€œfiles” based on the specified β€œperlexpr” regular expression.

For example, the following command will change all the file names with capital letters to names with lowercase letters.

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

Also, check out the following list of options that this command utilizes:

-aReplace all the occurrences of the filename.
-fForce to overwrite the existing file.
-iPrompt before overwriting the existing files.
-lReplace the last occurrence of the filename instead of the first one.
-nDon’t perform the change; only print the names of files to be renamed.
-sRename the source file instead of the symlink.
-vAfter the files have been successfully renamed, print their names.
-hDisplay the help section.

Given that, let’s look at some examples of how to use this command.

Changing the Filenames

The following command will replace all the text files named β€œfile1.txtβ€œ, β€œfile2.txtβ€œ, and β€œfile3.txt” with β€œmyfile1.txtβ€œ, β€œmyfile2.txtβ€œ, and β€œmyfile3.txt” in your current working directory.

$ rename -v 's/file/myfile/' *.txt


Rename the filenames

Changing the File Extension

When you execute the following command, it will search for all the text files in your current working directory and change their extension from β€œ.txt” to β€œ.pdfβ€œ, as shown.

$ rename -v 's/.txt/.pdf/' *.txt


Rename the file extension

Adding an Underscore to the Filenames

The following command will add the β€œ_” underscores in between the filename, like β€œmyfile1.txtβ€œ, β€œmyfile2.txtβ€œ, and β€œmyfile3.txt” will be renamed to β€œmyfile_1.txtβ€œ, β€œmyfile_2.txtβ€œ, and β€œmyfile_3.txt” filenames, respectively.

$ rename -v 's/myfile/myfile_/' *.txt


Adding the underscores in all the text files

Remove a Part from the Filenames

The following command will allow you to remove a specific part (in this case, β€œmyβ€œ) from the referenced filenames.

$ rename -v 's/my//' *.txt


Deleting a specific part from the filenames

Rename the Files Sharing a Similar Name.

If you have multiple files that share similar names like β€œmyfile1.txt” and β€œmy_file1.txtβ€œ, both share the common β€œ*file1.txt” in their name.

So, using these similarities, you can change the names of both of these files to something like β€œtext1.txtβ€œ, as shown.

$ rename -v 's/(my_|my)file/test/' *.txt


Rename the files sharing common name

Replacing Spaces in the Filenames with Underscores

The following command will replace all the spaces in the referenced filenames with β€œ_” underscores, so β€œfile name1.txt” will be renamed to β€œfile_name1.txtβ€œ, as shown.

$ rename -v 'y/ /\_/' *.txt


Replacing the space in the filenames with an underscores

Converting Filenames to Uppercase

The following command will convert the filenames from lowercase to uppercase.

$ rename -v 'y/a-z/A-Z/' *.txt


Converting filenames to uppercase

Converting Filenames to Lowercase

The following command will convert the filenames from uppercase to lowercase.

$ rename -v 'y/A-Z/a-z/' *.TXT


Converting Filenames to Lowercase

Exit Status

The rename command uses five exit statuses, followed by:

  • 0: The operation was completed successfully.
  • 1: Renaming the files failed.
  • 2: A few files failed while renaming.
  • 4: Nothing was renamed.
  • 64: An unanticipated error occurred.

So, that is all about this command that you should know as an informed Linux user.

If you have any interesting examples that should be included in this article, then let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.