How To Use Rename Command.
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:
- Ubuntu and Debian
- CentOS and Fedora
- Arch Linux
The following is the general syntax for the rename command:
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:
Let’s explain the command in more details:
- s/search_pattern/replacement/ – The substitution operator.
- .css – The search pattern. It is the first argument in the substitution operator. The rename command will search for this pattern in the given file name and if found it will replace it with the replacement argument.
- .scss – The replacement. The second argument in the substitution operator.
- *.css – All files with “.css” extension. Wildcard (*) is a symbol used to represent zero, one or more characters.
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:
The output will look something like this:
By default, the rename command doesn’t overwrite the existing files. Use the -f option which tells rename to overwrite the existing files:
If you want rename to print the names of files that are successfully renamed, use the -v (verbose) option:
Below are a few common examples of how to use the rename command:
Replace spaces in filenames with underscores
Convert filenames to lowercase
Convert filenames to uppercase
Remove .bak from the filenames
Rename .jpeg and .JPG filenames to .jpg
The rename command allows you to rename multiple files at once, using Perl regular expressions.
Comments are closed.