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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

65 Comments

Leave a Reply
  1. Hi Everyone,

    I have nearly 500 output files named from ‘xx00‘ to ‘xx 499‘, I want to change it for example to: ‘bb00‘ to ‘bb499‘.

    Please I need your help

    Sincerely
    MRasheed

    Reply
  2. Thanks. I’ve written my own ‘rename‘ utility long ago and find this from your article now.

    Two to correct:

    1. In example “rename ‘s/\.html$/\.php/’ *.html“, the REPLACEMENT part is just a STRING or EXPRESSION(if /e specified), so the backslash for .php is redundant.

    2. “Second argument tells the rename command to substitute all the files with *.php." – a typo, should be "*.html".

    Reply
  3. You showed how to rename file extensions. How do rename multiple file names that are complete different? Like, if I have five files named: tentwo-23.png, goingeasy.png, fear.png, samson(7).png, and support.png. And I want to name them 1.png, 2.png, 3.png, etc.

    Reply
  4. I am switching from windows. I would like to know if there is a way to do something like windows does: Select all files in a folder, right click on one of them, select rename, type something like “aaa” and press enter.

    There could be 2 files or 2,000, but lets say there were 5. The result would be 5 files named as such:
    aaa(1)
    aaa(2)
    aaa(3)
    aaa(4)
    aaa(5)
    This would save a lot of time.

    Reply
  5. Thanks for your post!

    Please add other section to capitalize only first letter of each filename.

    # rename 's/\b(\w)/\U$1/g' *.ext
    
    Reply
        • @Philippe,

          The command rename 's/\b(\w)/\U$1/g' *.ext is is used to rename files with a specific extension in the current directory by modifying their names according to a regular expression pattern.

          Let’s break down the components of the command:

          • s/ – Indicates a substitution operation.
          • \b – Represents a word boundary, ensuring that only the first character of each word is affected.
          • (\w) – Captures a word character and stores it in a capturing group.
          • \U$1 – Converts the captured character to uppercase.
          • /g – Global flag, meaning it applies the substitution globally (to all occurrences).
          • *.ext – Specifies the files to be renamed. In this case, it selects all files with the extension ".ext" in the current directory.
          Reply
  6. Hi,

    I am trying to rename multiple files in Centos7.

    For example: File I have:

    file1.html
    file2.html
    file3.html
    file3.html
    

    I want to rename them to:

    test1.html
    test2.html
    test3.html
    test4.html
    

    How can I achieve this?

    Thank you!

    Reply
    • # rename -n 's/file*/test/' *.html
      

      Then remove the -n switch when you are ready to run it for real

      Here is my validation of running a test for you

      user@host:~/Desktop/Junk/test$ touch file1.html
      user@host:~/Desktop/Junk/test$ touch file2.html
      user@host:~/Desktop/Junk/test$ touch file3.html
      user@host:~/Desktop/Junk/test$ touch file4.html
      user@host:~/Desktop/Junk/test$ rename -n 's/file*/test/' *.html
      rename(file1.html, test1.html)
      rename(file2.html, test2.html)
      rename(file3.html, test3.html)
      rename(file4.html, test4.html)
      user@host:~/Desktop/Junk/test$
      
      Reply
  7. I’m running Linux kernel version 106100LC004 2.6.32-573.el6.x86_64 #1 SMP Wed Jul 1 18:23:37 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux. And, my ‘rename‘ command expressions are like this:

    # rename .html .php *.html
    

    I then made an alias to shorten this command.

    # ren () { rename "$1" "$2" *"$1"; }
    

    So now my version is:

    # ren .html .php
    
    Reply
  8. Hi Ravi,

    This is great post, but in my case I tries nothing happening, can you please help me on this, I’m trying below. Something is wrong ?

    rename -n ‘s/\.txt/\.html/’ *.txt

    Reply
    • @Samim,

      Its due to wrong commas, used in the command, try to use the correct commas as shown in the following command.

      # rename -n 's/\.txt/\.html/' *.txt
      
      Reply
  9. if rename has some problems and different implementations (as suggested in some of the comments), then why is it any better than ‘sed’, which is decades older and very stable and well-documented?

    Reply
  10. I got thousand of files with space in a folder. It is from Windows or something. I hate filename with spaces. What should I type in terminal to remove the space from the filename?

    Reply
  11. Be careful – the version of rename(1) on debian/ubuntu is based on Perl while the version on the red hat family isn’t – therefore the expressions aren’t portable.

    Also to upcase

    rename -n ‘$_ = uc’ *

    To downcase

    rename -n ‘$_ = lc’ *

    Reply
  12. Backing up postfix mail files from mail server to WIN7 PC. The original mail files are named like this:

    1438761535.V902I1d8352eM884071.server.domain.com:2,S

    Windows has an issue with the extention and files are copied with size 0, and recognizes them as .com executable. I discovered that it works fine when renaming the file to _com. I can even copy them over to a new mail server, and they are recognized by the new postfix server.

    My plan now is:

    cp /postfix/files/path/*.server.domain.com:2,S /home/user/mail_backup/.

    next I tried this command, but nothing is happening, so I must be doing something wrong:

    rename -n ‘s/\.com:2,S$/\_com/’ *.com:2,S

    Please your advice.

    Reply
    • @Polar
      Give a try again with the help of following command with little modification.

      # rename -n 's/\.com/\_com/' *.com
      
      Reply
  13. You just saved 4 hours of my time and made me to do the same job of 4 hours in just 2 minutes. :) Amazing tips. This is why Linux is Awesome.
    Thank you very much for your help :)

    Reply
  14. Great! I works in Ubuntu 14.04 LTS. Can you please explain the perl expressions also?
    I don’t get the pattern where and why to use “s/ $/” or “y/” etc. It’ll be helpful if you explain it a little.

    Reply
  15. to rename files use, rename ‘y/A-Z/a-z/’ *

    what does the y do? I’m searching all over perlexpr tutorials to find out. It is doing similar to tr but cannot find a definite answer. Thanks.

    Reply
  16. The rename command used by the slackware family does not support regexes. The rename command via debuntu family does. Redhat family, I dunno. The debuntu rename command is more powerful, but see the manpage or -help option for your OS’ rename command.

    Reply
  17. Hi all. Not sure if this will help or not, but a few years back I wrote a script to deal with the Cygwin environment not having a reasonable implementation of the “rename” command line tool.

    http://nylinuxhelp.com/blogs/command-line/cygwin-rename-command-help

    Feel free to use the script (copy and paste it) and of course there is a “use at own risk” text on the page. I think with small modification you should be able to use it in a Linux distro.

    Cheers,
    Adam

    Reply
  18. For those that are still having issues with the “rename” utility, you must keep in mind that the “rename” utility that is present in Debian distributions is not the same utility found in non-Debian distributions. The one found in Debian distributions is the Perl version.

    I was rather frustrated myself when trying to perform this on my CentOS box. There are workarounds to install the Debian rename utility on non-Debian distributions. I haven’t been able to fine a straightforward way of installing it on non-Debian distros though. Maybe someone can post a quick link for that. Other than that, the syntax works perfectly if you use the correct utility.

    Reply
  19. If the regexp (s///) doesn’t work for you, you probably have an older version of rename. Try this syntax:

    rename ‘,’ ‘.’ [filenames…]

    The above will rename files substituting commas with full-stops.

    Reply
  20. Hi. I have multiple files with a “fn_” prefix in the filename. For example the file is called fn_00_11.jpg and I need to remove fn_ from all the files in the directory so that the filename is like, for example, 00_11.jpg. How can I do this via SSH?? Please help.

    Kindest regards,
    Shaun

    Reply
  21. Hi, can you help me with the expression? How could i change the
    test 1.txt
    test 2.txt
    test 3.txt

    files to

    new 1.txt
    new 2.txt
    new 3.txt

    And I will learn? Should i look for perl exressions?

    Thank you,
    Manolis

    Reply
  22. Hi

    Can you please tell if these command will work at remote FTP server as well.
    I need to rename multiple files there.

    Thank you

    Reply
  23. Tried te same its not working …. please help

    rename ‘s/\.html$/\.php/’ *.html
    You have new mail in /var/spool/mail/root
    [root@sl73psep3v14 temp]# ll -rth
    total 4.0K
    -rw-r—– 1 root root 0 Apr 15 03:50 c.html
    -rw-r—– 1 root root 0 Apr 15 03:50 b.html
    -rw-r—– 1 root root 0 Apr 15 03:50 a.html
    -rwx—— 1 root root 97 Apr 15 04:27 tt.sh

    [some@host temp]# vi /var/spool/mail/root
    [some@host temp]#

    Reply
      • Bro you keep telling people they are wrong, you are wrong.

        I can show you the man output -v -n are not even options, like someone else said it’s a different command and it’s where you said so there is confusion.

        [root@Cent06 tmp]# rpm -qf /usr/bin/rename
        util-linux-ng-2.17.2-12.14.el6_5.x86_64

        The rename command is part of this package and it doesn’t work at all like you said that’s it, nothing to do with quotes, you need to tell people which package and which OS is the one you are talking about and I see from others its maybe Ubuntu but not everyone using Ubuntu as a server.

        I can’t stand seeing someone telling everyone else they are wrong, unfortunately you are probably correct with a version of rename that is less common, does that make sense sir?

        the command that I’m mentioning to you, the /usr/bin/rename works like this

        rename ‘string’ ‘string2’ *.txt

        That’s it, no regex and it sucks, I had to load a project to get the functionality you are mentioning which is easily written to a bash or perl script.

        Reply
  24. I have tried rename ‘s/.,\ //’ * to strip leading “., ” in folder names but in vain. Can anyone please tell me how to do it ?

    Reply
  25. Another possibility as to why it’s not working is because they’re using an old version of rename. On my system, the version it’s running is util-linux-ng 2.17.2.

    This version does not appear to support backreferences, or the ‘s///’ regular expression replacement syntax.

    My system is running centos which I’m not as familiar with as I am with ubuntu.

    Anyone know where I can get the most up to date rename binary installed using yum?

    Reply
  26. i tried your above said CLI but they didn’t work for me below are the results
    [root@monitor noc]# ll
    total 0
    -rw-r–r– 1 root root 0 Nov 4 05:52 file1.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file2.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file3.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file4.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file5.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file6.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file7.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file8.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file9.txt
    -rw-r–r– 1 root root 0 Nov 4 05:51 file.txt
    [root@monitor noc]# which rename
    /usr/bin/rename
    [root@monitor noc]#
    [root@monitor noc]# rename -v ‘s/\.txt$/\.html/’ *.txt
    [root@monitor noc]# ll
    total 0
    -rw-r–r– 1 root root 0 Nov 4 05:52 file1.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file2.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file3.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file4.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file5.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file6.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file7.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file8.txt
    -rw-r–r– 1 root root 0 Nov 4 05:52 file9.txt
    -rw-r–r– 1 root root 0 Nov 4 05:51 file.txt

    Reply
      • Sir i have tried by copying the syntax which you have mentioned but still i face the same error :(

        [kiran@labmac ~]$ touch file{1..9}.txt
        [kiran@labmac ~]$ ll
        total 0
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file1.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file2.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file3.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file4.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file5.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file6.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file7.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file8.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file9.txt
        [kiran@labmac ~]$ rename -v ‘s/\.txt$/\.html/’ *.txt
        [kiran@labmac ~]$ ll
        total 0
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file1.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file2.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file3.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file4.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file5.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file6.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file7.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file8.txt
        -rw-rw-r–. 1 kiran kiran 0 Nov 9 15:11 file9.txt
        [kiran@labmac ~]$

        Reply
          • this command did not show any error but also have not any effect of this command –
            #rename ‘s/\.html$/\.php/’ *.html

            but, after seeing rename man page…
            #rename .html .php *.html
            above command had work

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.