1. 程式人生 > >linux中通過shell指令碼複製檔案,並用時間戳命名

linux中通過shell指令碼複製檔案,並用時間戳命名

其中/var/log/ha/ha.log該檔案是被複制檔案,var/log/ha/log/$currentTimeStamp時間戳生成的檔案複製的檔案,/var/log/ha/cp_info.log是實行本shell的操作記錄,同時本currentTimeStamp時間戳是長整型的

#!/bin/bash

#當前時間 <span style="font-family: Arial, Helvetica, sans-serif;">對應的毫秒時間戳</span>  
current=`date "+%Y-%m-%d %H:%M:%S"`  
timeStamp=`date -d "$current" +%s`   
currentTimeStamp=$((timeStamp*1000+`date "+%N"`/1000000)) #將current轉換為時間戳,精確到毫秒  



if [ ! -d "/var/log/ha/log/" ];then
    mkdir -p /var/log/ha/log #-p為父類不存在則生成,存在不影響
fi

if test -s /var/log/ha/ha.log; then #是否為 空或存在
        echo "is not empty $currentTimeStamp" >/var/log/ha/cp_info.log
        cp /var/log/ha/ha.log  "/var/log/ha/log/"$currentTimeStamp
        echo -n "" >  /var/log/ha/ha.log  # -n為指定為全空,不留第一行

else
        echo "empty or not exist $currentTimeStamp" >/var/log/ha/cp_info.log
fi