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


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


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


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