rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
-v
: Print names of files successfully renamed.-n
: Show what files would have been renamed.-f
: Force overwrites existing files.perlexpr
: Perl Expression.
rename -n ‘s/.html$/.php/’ *.html
which rename
/usr/bin/rename
Basic Syntax of Rename Command
sudo apt install rename [On Debian, Ubuntu and Mint]
sudo yum install prename [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/rename [On Gentoo Linux]
sudo apk add rename [On Alpine Linux]
sudo pacman -S rename [On Arch Linux]
sudo zypper install rename [On OpenSUSE]
sudo pkg install rename [On FreeBSD]
1. Changing File Extensions in Linux
rename ‘y/A-Z/a-z/’ *.HTML
5. Capitalize First Letter of Filename
man rename
rename -f ‘s/a/b/’ *.html
rename –version
rename ‘s/b(w)/U/g’ *.html
6. Replacing Spaces with Underscores
You can run the which command to find out the location of the rename command.
Installing Rename in Linux
ls -l *.php
cd /path/to/your/files
ls -l *.html
Before using rename, you need to ensure it is installed on your system by running the following command.
rename ‘s/s+/_/g’ *.html
If you would like to forcefully overwrite existing files, use the “-f” option as shown below.
rename is a command line utility that allows you to rename multiple files at once using regular expressions, which are patterns used to match character combinations in strings. This tool is particularly useful for batch renaming files based on specific patterns or rules.
's/.html$/.php/'
: This is a Perl expression wheres/
indicates substitution. The.html$
matches the.html
extension at the end of the filename, and/.php/
replaces it with.php
.*.html
: This specifies that the command should be applied to all files with the.html
extension.
rename ‘s/.html$/.php/’ *.html
rename ‘y/a-z/A-Z/’ *.html
Convert Filenames to Lowercase in Linux
This article will guide you through the basics of using rename to efficiently rename multiple files in Linux.
Below is an example of the command:
To batch rename all files with lowercase names to uppercase. For example, I want to convert all the following files from lowercase to uppercase.
3. View Detailed Rename Information
The rename command is part of a Perl script and it resides under /usr/bin/ on many Linux distributions.
Now use the ls command to verify that the files have been renamed.
Convert Filenames to Uppercase in Linux
The rename command doesn’t display information about the changes it makes by default. If you want to see details about the renames (similar to using the -n
option for dry runs), use the -v
option, which will print the complete details of all the changes made by the rename command.
To do so, first change to the directory containing your .html
files and Use the ls command to list all the files with the .html
extension.
rename -v ‘s/.html$/.php/’ *.html
4. Change File Name Case in Linux
rename ‘s/old_pattern/new_pattern/’ files
The rename command also comes with a few optional arguments along with a mandatory perl expression that guides the rename command to do actual work.
s+
: Matches one or more whitespace characters._
: Replaces whitespace with underscores.g
: Global replacement, affecting all matches in each file name.
7. Overwrite Existing Files
When undertaking critical or major renaming tasks, you can always check the changes by running the rename command with the -n
argument, which will show you exactly what changes would take place, but the changes are not executed for real.
Linux comes with a very powerful built-in tool called rename, which is used to rename multiple files or groups of files, convert filenames to lowercase, convert filenames to uppercase, and overwrite files using Perl expressions.
We often use the mv command to rename a single file in Linux. However, renaming multiple or groups of files quickly makes it a very difficult task in a terminal.