显示目录中所有文件的年龄的脚本

Script to show how old are all the files in a directory

我几个月前创建了这个脚本,我每天都在使用它来找出哪些备份文件已经超过 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='3[0;0m' -vC1='3[0;32m' -vC2='3[0;31m' -vC3='3[0;33m' \
        '{printf "%+10s %+1s %-5s %+4s %+4s %+3s %+2s %5s %-20s \n", ,,,,,," " C1  C0," " ," " }'
        actual=$(echo $result | awk '{ print  }')
        partition=$(echo $result | awk '{ print  }')
        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

就像我说的,这只会在当前日期接近月底或月初时发生。

我不太擅长编程或数学,所以这就是我在这里寻求帮助的原因。我很确定这项任务可以用更好、更简单的方式完成,我的代码真的很乱。任何帮助将不胜感激。

您正在使用一个月中的某一天来执行绝对项目差异,但如您所见,当 月的当天小于之后的一天时,该操作将失败 个月。您应该改为使用 UNIX 时间戳,它测量自 1970 年一天以来的秒数,以计算自上次修改文件以来经过的时间。此外,我建议使用 stat 而不是 ls 来获取此信息。 (假定为 GNU stat;您的本地实现可能有所不同。)

#!/bin/bash
#Backup
now=$(date +%s)        #Current time in seconds since Jan 1 1970
a=3                     #Number of the past days
b=0                     #No need to verify it because it has a backup from today
echo
for f in *; do      
  actual=$(stat -c '%Y')
  rest=$(( (now - actual) / 84600 ))
  if (( rest < a && rest != b )); then
    printf '\t The Backup for 3[33m%s3[0m was done 3[m%d3[0m days ago\n' "$f" "$rest"
done

在@chepner 帮助我之后,我能够增强脚本,现在它以更好的方式执行任务。

#!/bin/bash

#Backup

time=$(date +%s)        #Current time in seconds since Jan 1 1970
a=3                     #Number of the past days

echo
    printf '\t \t 3[32mBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS \tBACKUPS 3[0m \n \n'
for f in /home/admin/Documents/*;
do
  actual=$(stat -c '%Y' $f )
  normal=$(stat -c '%y' $f )
  rest=$(( (time - actual) / 84600 ))
  if (( rest > a )); then
    printf '\t Backup for 3[33m%-40s3[0m was done 3[1;31m%d3[0m days ago. \t Created Date: 3[0m%-30s \n' "$f" "$rest" "$normal"
 else
    printf '\t Backup for 3[33m%-40s3[0m was done 3[1;32m%d3[0m days ago. \t Created Date: 3[0m%-30s \n' "$f" "$rest" "$normal"
fi
done
echo

输出更有条理和清晰,更容易识别任何问题:

                 BACKUPS        BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS         BACKUPS

         Backup for /home/admin/Documents/backup_full.sh     was done 36 days ago.       Created Date: 2017-01-30 15:26:46.217390547 -0500
         Backup for /home/admin/Documents/backup.sh          was done 0 days ago.        Created Date: 2017-03-07 15:07:17.066182193 -0500
         Backup for /home/admin/Documents/English.xlsx       was done 36 days ago.       Created Date: 2017-01-30 15:25:37.592965894 -0500
         Backup for /home/admin/Documents/MySQL.docx         was done 36 days ago.       Created Date: 2017-01-30 15:25:37.604965969 -0500
         Backup for /home/admin/Documents/accounts.txt        was done 36 days ago.       Created Date: 2017-01-30 15:25:37.628966117 -0500
         Backup for /home/admin/Documents/SGID.docx          was done 36 days ago.       Created Date: 2017-01-30 15:25:37.604965969 -0500
         Backup for /home/admin/Documents/sticky_bit.docx    was done 36 days ago.       Created Date: 2017-01-30 15:25:37.616966043 -0500
         Backup for /home/admin/Documents/SUID.docx          was done 36 days ago.       Created Date: 2017-01-30 15:25:37.576965795 -0500
         Backup for /home/admin/Documents/test.sh            was done 1 days ago.        Created Date: 2017-03-06 12:23:46.872273977 -0500