您的位置:首页 > 运维架构 > Shell

linux中通过shell脚本复制文件,并用时间戳命名

2017-11-01 14:40 1306 查看
其中/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: