shell 查找目录下最新和最旧文件[时间戳]

今天同事做mysql双机切换测试,说需要将切换时产生的mysql日志文件名记录下来,让我想办法写一个脚本记录日志文件名,随即动手写了一shell脚本。如下:

#/bin/bash
declare -a files=(`ls -l | awk '/mysql-bin/ {print $9}'`)
min=${files[0]}
max=${files[1]}
if [ "$min" = "$max" ];then
        echo `date +%Y%m%d-%T`  'newlog && oldlog is null'>>logfile.txt
        exit
fi
for i in `ls -l|awk '/mysql-bin/ {print $9}'`; do
        if [ "$max" = "" ];then
        echo `date +%Y%m%d-%T`  'newlog && oldlog is null='"$min">>logfile.txt
        exit
        fi
        if [ $i -ot $max ];then
                max=$i
        elif [ $min -ot $i ];then
                min=$i
        fi
done
echo `date +%Y%m%d-%T`  oldlog="$max" newlog="$min" >>logfile.txt

你可能感兴趣的:(shell)