Tuesday, May 15, 2007

Take .bak of all files and way to revert back to original name


We will first list all the .cc extn files starting from current dir.

$ find . -name "*.cc" -type f

./as.cc
./dire/cd.cc
./bs.cc

Task1: Take a .bak of all your .cc files

$ find . -name "*.cc" -type f -exec cp {} {}.bak \;

So back-up is been taken
$ find . -name "*.cc.bak" -type f
./dire/cd.cc.bak
./as.cc.bak
./bs.cc.bak

Good!

Task 2: Now the requirement is different. Rename all the .bak s to their original name,

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

Is it really done ? check

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

YES.

One more way , but this is expensive(will take more time,if the input list is huge),

$ find . -name "*.cc.bak" -type f | while read file
> do
> mv $file `echo $file | sed 's/.bak$//g'`
> done

No comments:

© Jadu Saikia www.UNIXCL.com