Thursday, March 13, 2008

rename multiple file or extension in linux or unix

Hello,

There might be many case where you want to rename multiple files or extensions. Like you may want to convert all php3 to php or html to htm.

On freebsd box you have got many option but not mv *.php3 *.php
The main reason for this not working is that the shells are not suppose to do so.

Also there are many ports available to do so.
But here I'll tell you about a simple csh shell script to do so.
Suppose you wan to rename all the file0.php3 file1.php3 .... filen.php3 to file0.php file1.php .... filen.php.

#!/bin/csh
foreach file (*.php3)

mv -iv $file `basename $file php3`php
end

  • Remember that ` is backquote not '.
  • There is no space in php3`php.
  • RTFM for mv -iv.
  • RTFM for basename and foreach.
Happy mv.

1 comment:

  1. very handy. hope to see more of it.

    ReplyDelete