Did You Know?
Donate to TecMint

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

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

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

We often use “mv” command to rename a single file in Linux. However, renaming multiple or group of files quickly makes it very difficult task in a terminal.

Linux comes with a very powerful built-in tool called rename. The rename command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions.

Rename Multiple Files In Linux

The “rename” command is a part of Perl script and it resides under “/usr/bin/” on many Linux distributions. You can run “which” command to find out the location of rename command.

$ which rename
/usr/bin/rename
The Basic Syntax of Rename Command
rename 's/old-name/new-name/' files

The rename command comes with few optional arguments along with mandatory perl expression that guides rename command to do actual work.

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
  1. -v: Print names of files successfully renamed.
  2. -n: Show what files would have been renamed.
  3. -f: Force overwrite existing files.
  4. perlexpr: Perl Expression.

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

1. A Basic Rename Command Example

Suppose you’ve bunch of files with “.html” extension and you want to rename all “.html” files to “.php” at one go. For example, first do a “ls -l” to check the list of files with “.html” extension.

# ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

Now, you want to change the extension of all these files from “.html” to “.php“. You can use the following “rename” command with perl expression as shown below.

ravisaive@tecmint:~$ rename 's/\.html$/\.php/' *.html

Note: In the above command we’ve used two arguments.

  1. First argument is a perl expression that substitute .html with .php.
  2. Second argument tells the rename command to substitute all the files with *.php.

Let’s verify whether all files are renamed to “.php” extension, doing ls -l on the prompt.

ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.php
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.php
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.php
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.php
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.php
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.php
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.php

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

2. Check Changes Before Running Rename Command

While doing critical or major renaming tasks, you can always check the changes by running rename command with “-n” argument. The “-n” parameter will tell you exactly what changes would take place, but the changes are not done for real. Here, is the example of the command below.

ravisaive@tecmint:~$ rename -n 's/\.php$/\.html/' *.php

cricket.php renamed as cricket.html
entertainment.php renamed as entertainment.html
health.php renamed as health.html
lifestyle.php renamed as lifestyle.html
news.php renamed as news.html
photos.php renamed as photos.html
sports.php renamed as sports.html

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

3. Print Rename Output

We saw that the rename command didn’t displayed any information of changes it does. So, if you want to get the details of rename command (like we did using “-n” option), here we use “-v” option to print the complete details of all the changes done by rename command successfully.

ravisaive@tecmint:~$ rename -v 's/\.php$/\.html/' *.php

cricket.php renamed as cricket.html
entertainment.php renamed as entertainment.html
health.php renamed as health.html
lifestyle.php renamed as lifestyle.html
news.php renamed as news.html
photos.php renamed as photos.html
sports.php renamed as sports.html

4. Convert all Lowercase to Uppercase and Vise-Versa

To batch rename all files with lower case names to upper case. For example, I want to covert all these following files from lower to upper case.

Lower to Upper Case
ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

Just, use the following command with perl expression.

ravisaive@tecmint:~$ rename 'y/a-z/A-Z/' *.html

Once you’ve executed the above command, you can check the changes by doing “ls -l“.

ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 CRICKET.HTML
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 ENTERTAINMENT.HTML
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 HEALTH.HTML
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 LIFESTYLE.HTML
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 NEWS.HTML
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 PHOTOS.HTML
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 SPORTS.HTML

You can see that the above command actually renamed all the lower case file names (with .HTML extension) to upper case.

Upper to Lower Case

Similarly, you can also convert all upper case characters to lower case using the following command.

ravisaive@tecmint:~$ rename 'y/A-Z/a-z/' *.HTML
ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

5. Overwrite Existing Files

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

ravisaive@tecmint:~$ rename -f 's/a/b/' *.html

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

The rename command is very useful, if you are dealing with multiple or batch renaming of files from the command line. Do give a try and let me know, how far is useful in terms of renaming of files.

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Your name can also be listed here. Work as a Paid freelancer/writer at TecMint.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

26 Responses

  1. Handuel says:

    Sorry to be that guy but GNU/linux comes with rename, linux is just a kernel.

  2. numerodix says:

    Also see nametrans, another regular expression based rename tool, which supports these features and more: http://nametrans.sourceforge.net/

  3. Cherry says:

    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

    • Ravi Saive says:

      Dear Kirantej,

      You’ve used wrong command. The actual command is:

      # rename -v 's/\.txt$/\.html/' *.txt
      
      • Cherry says:

        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 ~]$

  4. Mike says:

    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?

  5. vignesh says:

    how can i rename this file is it possilble or not
    for ex:
    fact,sh to fact.sh

    • Ravi Saive says:

      Yes it’s possible, follow the guide properly, we’ve already shown you how to rename files in the write up.

  6. biz says:

    rename -v ‘s/,/./’ fact,sh

    Thanks for sharing. I’m loving this command.

  7. user says:

    Thanks for the post! Keep doing this post!

  8. barney says:

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

  9. newbie says:

    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]#

    • Ravi Saive says:

      You are using wrong quotes. The correct command is:

      # rename -v 's/\.html$/\.php/' *.html
      
      • Jason says:

        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.

  10. Subha Laad says:

    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

  11. Manolis says:

    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

  12. Temper says:

    How to rename from a list?
    Example (mv) : http://stackoverflow.com/questions/2893909

  13. Shaun says:

    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

  14. Shrenik says:

    I am not understanding related to expressions,from where should i get basics of expressions like used above.

  15. Billybob says:

    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.

  16. Jim says:

    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.

  17. Adam says:

    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

  18. Roland says:

    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.

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.