几个月前我创建了这个脚本,我每天都在使用它来查明哪些备份文件超过3天或更长时间。它可以帮助我快速识别所有具有3天或更长时间创建的文件。脚本显示目录中的所有文件的年龄
#!/bin/bash
#Backup
time=$(date +%d) #Current time in days
a=3 #Number of the past days
b=0 #No need to verify it because it has a backup from today
echo
ls -l | tail -n +2 | while read result;
do
echo $result | awk -vC0='\033[0;0m' -vC1='\033[0;32m' -vC2='\033[0;31m' -vC3='\033[0;33m' \
'{printf "%+10s %+1s %-5s %+4s %+4s %+3s %+2s %5s %-20s \n", $1,$2,$3,$4,$5,$6," " C1 $7 C0," " $8," " $9}'
actual=$(echo $result | awk '{ print $7 }')
partition=$(echo $result | awk '{ print $9 }')
rest=$(($time-$actual))
if [[ $rest -le $a && $rest -ne $b ]]; then
echo -e "\t The Backup for \e[33m$partition\e[0m was done \e[33m$rest\e[0m days ago"
fi
done
它将与评论一起显示在CLI LS的结果-l命令怎么老在一个更加人性化可读的方式的文件。例如:
-rw-r--r-- 1 root root 98756181 Mar 7 23:59 server005.Mon.tgz
The Backup for server005.Mon.tgz was done 3 days ago
-rw-r--r-- 1 root root 23663925 Mar 3 18:00 server006.Fri.tgz
The Backup for server006.Fri.tgz was done 3 days ago
-rw-r--r-- 1 root root 23663925 Mar 3 18:00 server009.Mon.tgz
The Backup for server009.Mon.tgz was done 3 days ago
我使用这个脚本每天每天早上,这样有助于我快速确定备份正在每3天做了不少,脚本将显示在每个文件显示文件几岁是评论使用文件的当前日期(以天为单位)减去文件的创建日期(以天为单位),然后如果结果大于数字3,它将显示带有文件天数的评论,但事实是当真实日期接近每月的第29,30或第一天时,我遇到一些问题,因为脚本显示负值。例如:
The Backup for backupserver001.Thu.tgz was done -11 days ago
The Backup for backupserver002.Wed.tgz was done -10 days ago
The Backup for backupserver003.Mon.tgz was done -21 days ago
就像我说的,这只发生在当前日期接近月底或月初时。
我对编程或数学不太擅长,所以这就是我在这里寻找帮助的原因。我很确定这个任务可以以更好的方式完成,更简单,我的代码非常混乱。任何帮助将非常感激。
'time'和'actual'是该月的两日;当'actual'发生在* previous *月份,但不满一个月前时,你会得到'rest'的负值。 – chepner