Oct 20, 2023 4 min read

Rename Command in Linux (Rename Multiple Files)

Learn to Rename Command in Linux with our step-by-step tutorial. Rename Command renames multiple files based on a pattern.

Rename Command in Linux
Table of Contents

Introduction

Before we discuss Rename command in Linux, let's briefly understand-What is Rename Command ?

The rename command is used in Linux to rename multiple files simultaneously based on a specified pattern or regular expression. It provides a convenient way to rename files in bulk, saving time and effort compared to renaming them individually.

In this tutorial, we'll show you how to rename many files at once using the rename command. We will also address a few FAQs on Rename Command in Linux (Rename Multiple Files).

Installing rename

The rename command comes in two flavors, each with its syntax and set of options. The rename function in Perl will be used.

Use the package manager for your Linux distribution to install this version if it is not already present:

  • Ubuntu and Debian
sudo apt update
sudo apt install rename
  • CentOS and Fedora
sudo yum install prename

Arch Linux

yay perl-rename

Using rename

The rename command often has the following format:

rename [OPTIONS] perlexpr files

To put it simply, the rename command is a Perl script. Inputting a perlexpr regular expression will cause it to rename the files you provide. Find out more about regular expressions and how they work in Perl here.

The following command, for instance, will convert all .css files to .scss format:

rename 's/.css/.scss/' *.css

Let's get into more depth about the command:

  • The search pattern replacement operator is denoted by the notation s/search_  pattern/replacement/ - The substitution operator.
  • .css - The search syntax is in the .css file. The substitution operator's initial argument. If the specified pattern is present in the file name, the rename command will use it to locate the file and replace it with the replacement argument.
  • .scss - A suitable alternative is the .scss.   The substitution operator is its second argument.
  • *.css - All files that end in .css.   The wildcard (*) sign may be used to represent 0, 1, or more characters.

It is usually a good idea to use the -n option that will do a "dry run" and show you what files will be renamed before executing the real command and renaming the files and directories. This option will show you what files will be changed as follows:

rename -n 's/.css/.scss/' *.css

The final product will resemble this:

Output

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)

When using the rename command, files are not overwritten unless explicitly specified. To force rename to replace any existing files, use the -f option.

rename -f 's/.css/.scss/' *.css

The -v (verbose) option causes rename to report the names of successfully renamed files.

rename -v 's/.css/.scss/' *.css
Output

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 is some popular use of 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' *

FAQs to Rename Command in Linux

What is the syntax of the rename command? 

The basic syntax of the rename command is rename [options] <pattern> <replacement> <files>. The pattern is the part of the filename you want to match, and the replacement is the new value you want to assign.

Can I use the rename command to rename files in subdirectories? 

Yes, the rename command can be used to rename files in subdirectories. You can specify the path to the files and use wildcards or regular expressions to match multiple files under the specified directory.

What are some common options used with the rename command? 

Some commonly used options with the rename command are -n (or --dry-run) to perform a trial run without making actual changes, and -v (or --verbose) to display verbose output, showing the renaming operations.

Can I preview the changes before renaming the files with the rename command?

Yes, you can use the -n option to perform a dry run and preview the changes that would occur without actually renaming the files. It helps ensure that the renaming pattern is correct.

What kind of patterns or regular expressions can I use with the rename command? 

You can use regular expressions (regex) or simple patterns to match parts of the filenames. Regular expressions provide more advanced matching capabilities, enabling complex renaming tasks.

Can the rename command be used to rename file extensions?

Yes, the rename command is commonly used to rename file extensions. You can specify the pattern to match the extension and the replacement to assign the new extension.

Does the rename command have an "undo" feature if I make a mistake? 

No, the rename command does not have built-in undo functionality. It is always recommended to preview the changes using the -n option before executing the actual renaming to avoid mistakes.

Conclusion

To rename numerous files at once using Perl regular expressions, use the rename command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.