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.