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
| Description | Rename |
| Difficulty Level | Moderate |
| Root or Sudo Privileges | No |
| OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
| Prerequisites | rename |
| Internet Required | Yes |
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 ManjaroRename 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 filesThe 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:
| Options | Description |
|---|---|
-a | Replace all the occurrences of the filename. |
-f | Force to overwrite the existing file. |
-i | Prompt before overwriting the existing files. |
-l | Replace the last occurrence of the filename instead of the first one. |
-n | Don’t perform the change; only print the names of files to be renamed. |
-s | Rename the source file instead of the symlink. |
-v | After the files have been successfully renamed, print their names. |
-h | Display 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/' *.txtOutput:

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/' *.txtOutput:

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_/' *.txtOutput:

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//' *.txtOutput:

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/' *.txtOutput:

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/ /\_/' *.txtOutput:

Converting Filenames to Uppercase
The following command will convert the filenames from lowercase to uppercase.
$ rename -v 'y/a-z/A-Z/' *.txtOutput:

Converting Filenames to Lowercase
The following command will convert the filenames from uppercase to lowercase.
$ rename -v 'y/A-Z/a-z/' *.TXTOutput:

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.





None of the command sequences listed above regardless of file name or file extension, gave up after almost an hour.
My question is this: “Does anyone on or off the planet Earth have a way to rename anything in Linux with the rename command? And yes, I’ve tried mmv but it just gives me different errors.
I know that I can open the GUI and change them individually, as I have been doing, but with over 500 directories and sub-directories, at the age of 70 I won’t live long enough.