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.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article|
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:
|Replace all the occurrences of the filename.|
|Force to overwrite the existing file.|
|Prompt before overwriting the existing files.|
|Replace the last occurrence of the filename instead of the first one.|
|Don’t perform the change; only print the names of files to be renamed.|
|Rename the source file instead of the symlink.|
|After the files have been successfully renamed, print their names.|
|Display the help section.|
Given that, let’s look at some examples of how to use this command.
Also Read: How to Rename Files and Directories in Linux
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
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
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
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
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
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
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 Lowercase
The following command will convert the filenames from uppercase to lowercase.
$ rename -v 'y/A-Z/a-z/' *.TXT
Also Read: What is Exit Status Code ($?) of Last Command in Linux
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.