Saturday, June 18, 2011

linux rename multiple files

Did you ever have this situation that you have multiple files like a.inc, b.inc, c.inc, and you want to change them to a.php, b.php, c.php? Basically, we want to change extensions of multiple files in linux - bulk rename files.

rename command can get this task done.

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

-v Verbose: print names of files successfully renamed
-n No Action: show what files would have been renamed
-f Force: overwrite existing files

perlexpr: Perl expression.  Well, this might be the headache to most users.

files: specify the pattern of the files that need to be renamed.

So, if we want to rename all files matching *.inc to *.php, we can do:

rename -v 's/.inc/.php/' *.inc

In this command, s means substitute. We want to substitute *.inc with *.php. The last *.inc means do the replacement to all files matching *.inc

No comments: