Solução:
cat backup.sh
#!/bin/sh
for i in 'find ./{a,b,c} -type f'
do
FILE='basename $i'
FTIME='stat -c %x $i | awk -F"." '{print $1}' | sed 's/[-: ]//g''
NEWNAME=${FILE}_${FTIME}
echo "cp $i result/${NEWNAME}"
cp -p $i result/${NEWNAME}
done
cd result
for i in 'ls -1 | awk -F"_" '{print $1}' | sort -u'
do
TNAME='ls -1t ${i}* | head -1'
echo "mv ${TNAME} ${i}"
mv ${TNAME} ${i}
done
Backup de arquivos no diretório "result".
Como está funcionando (exemplo):
$ ls -l {a,b,c,result}
a:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileB.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileC.txt
b:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileC.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileD.txt
c:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt
result:
total 0
~/tmp/backup $ ./backup.sh
cp ./a/fileA.txt result/fileA.txt_20130606162832
cp ./a/fileB.txt result/fileB.txt_20130606162836
cp ./a/fileC.txt result/fileC.txt_20130606162846
cp ./b/fileA.txt result/fileA.txt_20130606162857
cp ./b/fileD.txt result/fileD.txt_20130606162910
cp ./b/fileB.txt result/fileB.txt_20130606162900
cp ./b/fileC.txt result/fileC.txt_20130606162906
cp ./c/fileA.txt result/fileA.txt_20130606162920
cp ./c/fileB.txt result/fileB.txt_20130606162923
mv fileA.txt_20130606162920 fileA.txt
mv fileB.txt_20130606162923 fileB.txt
mv fileC.txt_20130606162906 fileC.txt
mv fileD.txt_20130606162910 fileD.txt
~/tmp/backup $ ls -l {a,b,c,result}
a:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileB.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileC.txt
b:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileC.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileD.txt
c:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt
result:
total 0
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileA.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt_20130606162832
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileA.txt_20130606162857
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileB.txt_20130606162836
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileB.txt_20130606162900
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileC.txt
-rw-r--r-- 1 test test 0 Jun 6 16:28 fileC.txt_20130606162846
-rw-r--r-- 1 test test 0 Jun 6 16:29 fileD.txt
~/tmp/backup $