Rename – A Command Line Tool For Renaming Multiple Files in Linux

Introduction

Write an introduction on Rename – A Command Line Tool For Renaming Multiple Files in Linux

Rename – A Command Line Tool For Renaming Multiple Files in Linux

Rename – A Command Line Tool For Renaming Multiple Files in Linux

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.

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.

This article will guide you through the basics of using rename to efficiently rename multiple files in Linux.

What is Rename?

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.

The rename command is part of a Perl script and it resides under /usr/bin/ on many Linux distributions.

You can run the which command to find out the location of the rename command.

which rename

/usr/bin/rename

Basic Syntax of Rename Command

The basic syntax of the rename command is:

rename 's/old_pattern/new_pattern/' files

Here is the breakdown of the command:

  • s/old_pattern/new_pattern/: This is the substitution command used by rename, that tells rename to replace the old_pattern with the new_pattern.
  • files: This specifies the files you want to rename.

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.

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.

For better understanding of the rename utility, we’ve discussed a few practical examples of this command in the article.

Installing Rename in Linux

Before using rename, you need to ensure it is installed on your system by running the following command.

rename --version

If it is not installed, you can install it using your package manager as shown.

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

Suppose you have a bunch of files with the ".html" extension and you want to rename all ".html" files to ".php" at once.

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.

cd /path/to/your/files
ls -l *.html

Now use the rename command to change the file extensions from .html to .php.

rename 's/\.html$/\.php/' *.html

Explanation of the command:

  • 's/\.html$/.php/': This is a Perl expression where s/ 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.

Now use the ls command to verify that the files have been renamed.

ls -l *.php
Change File Extensions in Linux
Change File Extensions in Linux

Now you can see above that all the html files are renamed to php.

2. Preview Changes Before Renaming 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.

Below is an example of the command:

rename -n 's/\.html$/\.php/' *.html
Dry Run File Renaming
Dry Run File Renaming

Note: The above command only displays changes, but in real the changes are not done, unless you run the command without “-n” switch.

3. View Detailed Rename Information

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.

rename -v 's/\.html$/\.php/' *.html
See Exactly What Changed
See Exactly What Changed

4. Change File Name Case in Linux

In Linux, you can easily change the case of file names, meaning you can convert them from uppercase to lowercase (and vice versa) using the rename command.

Convert Filenames to Uppercase in Linux

To batch rename all files with lowercase names to uppercase. For example, I want to convert all the following files from lowercase to uppercase.

rename 'y/a-z/A-Z/' *.html
Convert Filenames to Uppercase
Convert Filenames to Uppercase

Convert Filenames to Lowercase in Linux

Similarly, you can also convert all uppercase characters to lowercase using the following command.

rename 'y/A-Z/a-z/' *.HTML
Change Filenames to Lowercase
Change Filenames to Lowercase

5. Capitalize First Letter of Filename

To capitalize only the first letter of each filename use the following command.

rename 's/\b(\w)/\U$1/g' *.html
Capitalize First Letter of Filename
Capitalize First Letter of Filename

6. Replacing Spaces with Underscores

To replace all occurrences of whitespace (spaces) with underscores (_) in the filenames of HTML files within the current directory.

rename 's/\s+/_/g' *.html

Explanation of the above command.

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

If you would like to forcefully overwrite existing files, use the “-f” option as shown below.

rename -f 's/a/b/' *.html

If you would like to know more about rename command, type the “man rename” in the terminal.

man rename

The rename command is very useful when dealing with multiple or batch renaming of files from the command line. Give it a try and let me know how useful it is for renaming files.


generate an article with html tags on Rename – A Command Line Tool For Renaming Multiple Files in Linux

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?