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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, 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.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.