Our Knowledge Base provides step-by-step guides, troubleshooting tips, and expert insights to help you manage VPS, dedicated servers, domains, DDoS protection, and more — all designed to make your experience with us fast, secure, and stress-free.
In this tutorial, we will explain how to use the rename command to batch rename files.
Renaming multiple files and directories with the mv command can be a tedious process as it involves writing complex commands with pipes, loops , and so on.
This is where the rename command comes handy. It renames the given files by replacing the search expression in their name with the specified replacement.
There are two versions of the rename command with different syntax and features. We will be using the Perl version of the rename command.
If this version is not installed on your system, use the package manager of your Linux distribution to install it:
sudo apt updatesudo apt install rename
sudo yum install prename
yay perl-rename
The following is the general syntax for the rename command:
rename [OPTIONS] perlexpr files
The rename command is basically a Perl script. It will rename the given files according to the specified perlexpr regular expression. You can read about Perl regular expressions here .
For example, the following command will change the extension of all .css files to .scss:
rename 's/.css/.scss/' *.css
Let’s explain the command in more details:
Before running the actual command and rename the files and directories it is always a good idea to use the -n option that will perform a “dry run” and show you what files will be renamed:
rename -n 's/.css/.scss/' *.css
The output will look something like this:
rename(file-0.css, file-0.scss) rename(file-1.css, file-1.scss) rename(file-2.css, file-2.scss) rename(file-3.css, file-3.scss) rename(file-4.css, file-4.scss)
By default, the rename command doesn’t overwrite the existing files. Use the -f option which tells rename to overwrite the existing files:
rename -f 's/.css/.scss/' *.css
If you want rename to print the names of files that are successfully renamed, use the -v (verbose) option:
rename -v 's/.css/.scss/' *.css
file-0.css renamed as file-0.scss file-1.css renamed as file-1.scss file-2.css renamed as file-2.scss file-3.css renamed as file-3.scss file-4.css renamed as file-4.scss
Below are a few common examples of how to use the rename command:
rename 'y/ /_/' *
rename 'y/A-Z/a-z/' *
rename 'y/a-z/A-Z/' *
rename 's/\.bak$//' *.bak
rename 's/\.jpe?g$/.jpg/i' *
The rename command allows you to rename multiple files at once, using Perl regular expressions.