【问题标题】:bash script to find old files based off date in file namebash脚本根据文件名中的截止日期查找旧文件
【发布时间】:2011-05-24 04:45:33
【问题描述】:

我正在开发一个 bash 脚本,该脚本需要根据一个变量来搜索单个目录中“旧”的文件,该变量指定在超过阈值之前需要经过多少天,并且文件被标记为可操作(可以是从移动到存档到删除等任何内容。

要注意的是,文件的修改时间与确定文件在采取行动之前需要多长时间无关,因为文件可能很少更改,脚本的执行时间可能会有所不同,等等......

确定保存文件的时间是实际文件名,格式为 YYYY-MM-DD(或带有日期命令的 %F)。以文件名 contents-2011-05-23.txt 为例。可以在此目录中运行哪些命令来查找所有超过一定天数的文件(我的阈值当前设置为 7 天,可以更改)并打印出它们的文件名?

【问题讨论】:

    标签: bash


    【解决方案1】:

    像这样创建一个 bash 脚本 isOld.sh:

    #!/bin/bash
    
    fileName=$1
    numDays=$2
    
    fileDt=$(echo $fileName | sed 's/^[^-]*-\([^.]*\)\..*$/\1/')
    d1=$(date '+%s')
    d2=$(date -d $fileDt '+%s')
    diff=$((d1-d2))
    seconds=$((numDays * 24 * 60 * 60))
    [[ diff -ge seconds ]] && echo $fileName
    

    然后通过运行给上面的文件赋予执行权限:

    chmod +x ./isOld.sh
    

    最后从你的目录顶部运行这个 find 命令来打印超过 7 天的文件:

    find . -name "contents-*" -exec ./isOld.sh {} 7 \;
    

    【讨论】:

      【解决方案2】:

      在 BSD 中,-j 用于防止设置日期,-f 参数用于设置输入日期的格式。 :

      首先,您需要在距离 1970 年 1 月 1 日的天数中找到今天的日期:

       today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
      

      现在,您可以使用它来找出 7 天前的时间:

       ((cutoff = $today - 604800))
      

      数字 604800 是 7 天的秒数。

      现在,对于目录中的每个文件,您需要找到字符串的日期部分。我不知道更好的方法。 (也许有人知道一些 Bash 魔法)。

      find . -type f | while read fileName
      do
           fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/')
           yadda, yadda, yadda #Figure this out later
      done
      

      一旦我们有了文件日期,我们就可以使用 date 命令来确定该日期是否以秒为单位小于(因此比截止日期更早)

      today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
      ((cutoff = $today - 604800))
      find . -type f | while read fileName  #Or however you get all the file names
      do
           fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/')
           fileDateInSeconds=$(date -j -f "%Y-%m-%d" $fileDate +%s)
           if [ $fileDateInSeconds -lt $cutoff ]
           then
                rm $fileName
           fi
      done
      

      在 Linux 中,您使用 -d 参数来定义日期,该日期必须为 YYYY-MM-DD 格式:

      today=$(date +"%Y-%m-%d)
      

      现在,您可以计算秒数:

      todayInSeconds=(date -d $today +%s)
      

      其他一切都应与上述大致相同。

      【讨论】:

        【解决方案3】:

        如果你每天运行命令,你可以这样做:

        echo *-`date -d '8 days ago' '+%F'`.txt
        

        当然可以添加其他通配符

        【讨论】:

          【解决方案4】:
          find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt -exec bash -c 'dt=`echo $0 | sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; file_time=`date -d $dt +%s`; cutoff_time=`date -d "31 days ago" +%s` ; test $file_time -lt $cutoff_time ' {} \; -print
          

          这是我最长的一个衬里之一 :-) 它再次被包裹起来:

          find *[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]*.txt \
            -exec bash -c ' dt=`echo $0 | \
                            sed -re "s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/"`; \
                            file_time=`date -d $dt +%s`; \
                            cutoff_time=`date -d "31 days ago" +%s` ;\
                            test $file_time -lt $cutoff_time \
                          ' {} \; -print
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-22
            • 1970-01-01
            • 1970-01-01
            • 2020-11-02
            • 1970-01-01
            • 2015-01-21
            • 2017-05-12
            相关资源
            最近更新 更多