bash script to find old files based off date in file name

前端 未结 4 659
天命终不由人
天命终不由人 2021-02-10 13:15

I\'m developing a bash script that needs to search out files within a single directory that are \"old\" based off a variable that specifies how many days need to pass before the

4条回答
  •  一向
    一向 (楼主)
    2021-02-10 13:44

    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
    

    That's one of my longest one liners :-) Here it is again wrapped:

    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
    

提交回复
热议问题