Pages

mardi 20 juillet 2010

copy large volumes of files automatically with bash: string comparison

I had some large volume of data saved in one computer and want to copy some of them to the other disk. All the files are zippfiles, and they are named according to "NO_time.zip" with the "time" being different from files. The criteria of copy is that: I just want to copy the files which have "time" later than time0. This is what I did:

#!/bin/bash
# $(ls *.zip) is to find all the files with zip format
for i in $(ls *.zip);do
# compare the strings: here I found "-gt" does not work with my system
if [[ "$i" > "N0_time0.zip" ]];then
echo "$i"
cp "$i" /media/disk/
echo "copy $i is finished"
fi
done

Note: [[(espace)"$i">"N0_time0.zip"(espace)]], otherwise, it does not work !
Note: it also should work [[(espace)$i>N0_time0.zip(espace)]]
Note: the substring either is a complete string such as NO_time0.zip; otherwize, *N0_time0.zip* is more safe to use....

Here is one link useful for bash if:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

Aucun commentaire:

Enregistrer un commentaire