Updated April 18, 2023
Introduction to Linux Rename Command
In the Linux operating system, the rename command will rename the multiple files by replacing the first occurrence of expression. While using rename command if the user is not providing the file name to the rename command then it will consider the standard input file name.
Syntax:
rename [options] expression file...
- rename: using the rename keyword in the command | syntax. It will take the set of argument as an option, expression, file name and rename the file.
- option: we can provide the different flags as an option to the rename command.
- expression: we need to provide the regular expression in the rename command.
- file: input file name.
How Linux Rename Command works?
As the name suggests, the rename command is useful to rename the multiple files. In the content, we are looking of pearl version of rename command. If the rename command is not present in your machine please download the necessary packages to run the rename command on your Linux Environment. Rename command is basically working on the give option value and expression format. With the help of option and expression values rename command will rename the multiple files.
Examples of Linux Rename Command
Given below are the examples:
Example #1 – To Change File Extension.
With the help of rename command, we are able to change all the file extension to other file extension format.
Code:
rename 's/.html/.orc/' *.html
Explanation:
We are having a data directory. In the same directory, we have multiple files in it with different file extensions. With the help of the rename command, we are able to rename all the file extension to different file extension. As per the above rename command, we are renaming “.html” extension to “.orc” extension.
Output:
Example #2 – To Rename File Name in Uppercase.
With the help of rename command, we are able to rename all the file names from lower case letter to upper case letters.
Code:
rename 'y/a-z/A-Z/' *
Explanation:
We are creating one data directory, in the same directory. We are having multiple / number of files in it. The file name is in lower case letter. With the help of rename command, we are able to rename the file name from lower case to upper case. In the same directory, we are able to do that on all files present in it. No need to select indivisible files.
Output:
Example #3 – To Rename File Name in Lowercase.
In rename command, we are having the functionality to rename the files name from upper case letter to lower case letter.
Code:
rename 'y/A-Z/a-z/' *
Explanation:
In the data directory, we are having the number of files that are in upper case letter name. In the above rename command, we are able to rename all the files from upper case letter to lower case letter present in the working directory.
Output :
Example #4 – Final Change.
In rename command, we are having the functionality to see the final changes of all the multiple files without changes of actual file names. We can use the “-n” option with the rename command to check the final changes happen on the file name.
Code:
rename -n 's/.orc/.png/' *.orc
Explanation:
In the data directory, we are having multiple files with the file extension of “.orc”. Before doing any changes on file level we need to check how the file will look like after changing to any other file extension. As per the above command, we are changing the multiple file extension from “.orc” to “.png”. Before doing any file extension changes rename command displays the file extension output.
Output:
Example #5 – Delete Part of Filename.
In the linux rename command, there is the facility to remove or delete the unwanted part of the file name. As per the requirement, we can remove the specific part of the filename from multiple files.
Code:
rename 's/ff_//' *.txt
Explanation:
In the data directory, we are having a number of files. The filename is starting with some special format “like ff_”. We do not need the special format of the file. We need to remove all the special character name form the multiple files. In the above rename command, we are able to rename all the files with the desire file name.
Output:
Example #6 – Remove Space from Filename.
In rename command, we are able to remove all the “space” from the file name. With the help of the same command, we are replacing the space in filename name with any character or special character or number as per the requirement.
Code:
rename 'y/ /_/' *
Explanation:
In the data directory, three different files contain the space in the filename. While processing such files, the linux application or shell job is not recognizing the files correctly. Mandatory we need to rename the files with a valid name. We are replacing the space from filename with an underscore.
Output:
Conclusion
We have seen the uncut concept of “Linux Rename Command” with the proper example, explanation and command with different outputs. The rename command is used to rename the multiple files. It will really help when we need to process the number of files but the file nomenclature is not compatible.
Recommended Articles
We hope that this EDUCBA information on “Linux Rename Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.