rename Command in Linux | Explained

In Linux, renaming any file is easier, but what if there are a bunch of files with the same extension such as “.txt” and the user wants to change all these file names simultaneously? which takes user time. For that purpose, rename command is used. This write-up will cover the usage of rename command in Linux. The content for this write-up is as follows:

What is the rename Command in Linux?

In Linux, rename is a utility for renaming any file name according to the regular expressions. “rename” utility is not pre-defined but can be installed using the following command:

For Ubuntu/Debian:

$ sudo apt install rename

For Fedora/CentOS:

$ sudo yum install prename

For Arch:

$ sudo pacman -S rename

The syntax for the rename command is given below:

Syntax:

$ rename [-options] [Expressions] [File Name]

Type the “rename” keyword, options, and expressions for renaming the file name. The options supported by this command can be seen as follows:

$ rename --help

Let’s move and use the rename command in Linux.

How to Use rename Command in Linux?

To use rename command, open the terminal by pressing “Ctrl+Alt+T” from the keyboard. A few examples are implemented for renaming the files using rename command.

Example 1: Showing the Renaming Files

In this example, we have used the “v” option, which displays the file which is being renamed, “*.jpg”, telling the system to convert all jpg files:

$ rename -v 's/jpg/png/' *.jpg

The above image shows the two files have been converted from jpg to png.

Example 2: Renaming the File Name to Uppercase

Using rename command, the user can also convert the file name to the upper case. For this, the regular expression will be used. Consider the “B-script.sh” file, which is present in our home directory:

$ ls

To convert any file name to the upper case is obtained as follows:

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

In the above image, “B-script.sh” has been converted into “B-SCRIPT.SH”.

Example 3: Renaming the File Name to Lowercase

Similarly, to convert the file name to lowercase, execute the following command:

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

The above image shows that the “B-SCRIPT.SH” file has been converted into “b-script.sh”.

Example 4: Replacing File Name Space with “_”

Using regular expressions, you can also replace the empty spaces with “_”. Consider the “File 1.txt” and “File 2.txt” which are shown in the image:

$ ls

To replace the spaces with “_” in all “.txt” files are obtained as follows:

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

Space between all the .txt file names has been replaced with “_”.

Example 5: Deleting the Specific Portion of the File Name

This command can also omit the file name’s a specific portion. Assume the “example.txt” file:

$ ls

To delete the particular portion of the file name is obtained as follows:

The file name is omitted from “example.txt” to “ex.txt”.

Bonus Tip: How to Rename File using mv Command?

Apart from rename command, the user can also use the “mv” command to change the file name. The “mv” command is used to move the file from one directory to another, but it can e used to move the file in the same directory, which will override the file and replace that file with the given file name. Let’s understand this concept with an example.

We have an “ex.txt” file in our directory which can be seen in the below image:

$ ls

Let’s change this file using the “mv” command by executing the below command in the terminal:

$ mv ex.txt henry.txt

The “ex.txt” file has been renamed to “henry.txt”.

To have a bigger picture of the rename command, check out this mentioned article.

Conclusion

In Linux, rename command is a utility that is used to modify the name of any file name with your desired ones. Various options are available for this command. This post has briefly demonstrated some of the most common usages of rename commands with the help of examples. Apart from this, renaming the file using the “mv” command has also been demonstrated in this post.