Tuesday, June 12, 2007

Renaming files


Suppose you have to rename all your .cpp files to .cc in a directory and corresponding sub-dirs, the following one liners will help you doing that.

$ find . -name "*.cpp" -type f -print

./as.cpp
./dire/cd.cpp
./bs.cpp

$ find . -name "*.cpp" -type f -print -exec rename 's/\.cpp$/\.cc/' {} \;

$ find . -name "*.cc" -type f -print
./as.cc
./bs.cc
./dire/cd.cc

WAHOO Its done!

The above example is a recursive one , if you just want to rename files in a directory and not in its sub-dirs, then

$ ls *.cpp | while read file
> do
> mv $file `basename $file .cpp`.cc
> done

1 comment:

Unknown said...

Simple, but very useful tip. This is one thing, which I normally do (manually on multiple files individually twice a week)...

Now I have your tip to use. :-)

-Nitin

© Jadu Saikia www.UNIXCL.com