[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 21, 2023

How to use Rename command.

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.

 

Installing rename

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
    sudo apt updatesudo apt install rename
  • CentOS and Fedora
    sudo yum install prename
  • Arch Linux
    yay perl-rename

 

Using 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:

  • 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:

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

 

rename Examples

Below are a few common examples of how to use the rename command:

Replace spaces in filenames with underscores

rename 'y/ /_/' *

Convert filenames to lowercase

rename 'y/A-Z/a-z/' *

Convert filenames to uppercase

rename 'y/a-z/A-Z/' *

Remove .bak from the filenames

rename 's/\.bak$//' *.bak

Rename .jpeg and .JPG filenames to .jpg

rename 's/\.jpe?g$/.jpg/i' *

 

Conclusion

The rename command allows you to rename multiple files at once, using Perl regular expressions.

Keep reading
How to use SSHFS to Mount Remote Directories over SSH
May 5, 2023

How to use SSHFS to Mount Remote Directories over SSH

How To Use SSHFS To Mount Remote Directories Over SSH This tutorial will show you how to install the SSHFS client on Linux, macOS, and Windows and how to mount a remote directory. SSHFS (SSH Filesystem) is a filesystem client based on FUSE for mounting remote directories over an SSH connection.…

Read article
How to use Linux Sleep Command (Pause a Bash Script).
May 5, 2023

How to use Linux Sleep Command (Pause a Bash Script).

How To Use Linux Sleep Command (Pause A Bash Script). In this tutorial, we will show you how to use the Linux sleep command. The sleep command is useful when used within a bash shell script, for example, when retrying a failed operation or inside a loop. sleep is a command-line utility that allows…

Read article
How to use Uptime Command.
May 5, 2023

How to use Uptime Command.

How To Use Uptime Command. In this tutorial, we will cover the uptime command. As its name suggests, the uptime command shows how long the system has been running. It also displays the current time, the number of logged-in users, and the system load averages for the past 1, 5, and 15 minutes. How…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00