Linux下的cp -u能实现只在源文件比目标文件新时才拷贝,但此命令在MinGW下不好使,我通过在网上查方法,写了以下同样功能的脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | clearBinary() { local destDir=$1 local fileName=$2 local ext=$3 rm "${destDir}/${fileName}.${ext}" 1>/dev/null 2>&1 for ((j = 1; j <= 20; j++)); do if [ -f "${destDir}/${fileName}_$j.${ext}" ] ; then rm "${destDir}/${fileName}_$j.${ext}" fi done for ((j = 1; j <= 20; j++)); do if [ -f "${destDir}/${fileName}.${ext}" ] ; then mv "${destDir}/${fileName}.${ext}" "${destDir}/${fileName}_$j.${ext}" fi done } copyFile() { local srcDir=$1 local destDir=$2 local fileName=$3 local ext=$4 local srcPath=${srcDir}/${fileName}.${ext} local destPath=${destDir}/${fileName}.${ext} if [ ! -f "${srcPath}" ] ; then return fi local timeFrom=`stat -c %Y "${srcPath}"` local timeTo=`stat -c %Y "${destPath}"` if [ "$timeFrom" = "$timeTo" -a "x$forceReplace" != "xforce" ]; then return fi clearBinary "${destDir}" ${fileName} ${ext} cp "${srcPath}" "${destPath}" local timeFromStr=`date -d "@$timeFrom" "+%Y-%m-%d %H:%M:%S"` touch -m -d "$timeFromStr" "${destPath}" echo "[${srcPath}]->[${destPath}] copied." } copyFile /e/source /c/dest hello docx |