Friday, May 11, 2007

Backing up files


If the instruction is to take a .bak of all .cpp extn files in the current dir as well as its sub dirs, here is the best way of doing (I know you already know this :-) )

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

one more way: (expensive, but can be used as an example of while loop in Shell Scripting Classes!!)

$ find . -name *.cpp -type f | while read input
> do
> cp $input $input.bak
> done

How about this method ?

Here I am taking a .bak of the .out files in my present dir.

$ ls -l *.out
-rw-r--r-- 1 jsa mkgroup-l-d 12 Dec 16 10:54 lso.out
-rw-r--r-- 1 jsa mkgroup-l-d 11 Dec 16 10:54 net.out
-rw-r--r-- 1 jsa mkgroup-l-d 57 Dec 8 21:41 testda.out

$ ls -l *.out | awk '{print $NF}'
lso.out
net.out
testda.out

$ ls -l *.out | awk '{print $NF}' | sed 's/.*/cp & &.bak/'
cp lso.out lso.out.bak
cp net.out net.out.bak
cp testda.out testda.out.bak

$ ls -l *.out | awk '{print $NF}' | sed 's/.*/cp & &.bak/' > /tmp/out.tmp ; sh /tmp/out.tmp ; rm /tmp/out.tmp

No comments:

© Jadu Saikia www.UNIXCL.com