Sunday 19 May 2013

Copy or Rename multiple files to another extension in Linux

Recently I came across a problem of renaming multiple files to a different extension in linux. Basically what I had was a folder with template files and I wanted to create php files from them while keeping the original ones intact. Here is my folder structure.

/Config
 - Database1.template
 - Database2.template
 - Database3.template

And this is what I wanted to achieve


/Config
 - Database1.template
 - Database2.template
 - Database3.template
 - Database1.php
 - Database2.php
 - Database3.php

It turned out that you can do it using just one simple command ( instead of writing a shell script ). A command called "rename" is what I was looking for.

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

The above command tells linux to look at all the template files in the current folder, search for template text and replace it with php. Hope this helps you as well.

Cheers.




No comments:

Post a Comment