TIP: How do I rename multiple files to another extension?

Let us say that you want to rename all of your “.php3” files to “.php” files. How you do this with minimal effort? Answer lies in for loop.

for old in *.php3; do cp $old `basename $old .php3`.php; done

Thats all there is to it. Let us say you need to rename index.php3 to index.php. The way above snippet works is that it loops through current directory and finds all the files with extension “.php3” and processes ’em one by one. In our index.php3 file example, it finds index.php3 file, does a cp index.php3 `basename index.php3 .php3`.php <- basename returns “index” so you add .php to it and now you are left with index.php. Of course you can do mv command if you want to just move the file to new name. I would suggest doing cp first to make sure things work the way you want ’em to.

3 thoughts on “TIP: How do I rename multiple files to another extension?

  1. rasham

    thanks man.. i was looking for this.. would be great if i knew how to move the files from dir to dir in this commando as well..

  2. CrazyToon Post author

    if you are talking about moving to another dir while its renaming you would modify as below:
    for old in *.php3; do cp $old /new/dir/name/`basename $old .php3`.php; done

    if you just wanted to move all *.php3 to another dir, you can do that by simply using mv command:

    mv *.php3 /new/dir/name/

    Let me know if thats all you needed.

  3. sara

    I would like to rename all files that I have in this path: /dbaix/v95nst “.del″ files to “.old” files in a scrip how I can do this?


Leave a Reply

Your email address will not be published. Required fields are marked *