Bash script to find old files based on date in file name

I am developing a bash script that should search for files within the same directory that are “old” based on a variable that indicates how many days should pass before the threshold is exceeded, and the files are marked for action (it can be anything: from moving to the archive to delete, etc.).

The trap is that the file modification time does not matter when determining how old the files should be pre-accepted, since files can rarely change, script execution time may vary, etc ...

The time that determines the file saving is located in the actual file name in the form of YYYY-MM-DD (or% F with a date command). Take, for example, the contents of the file-2011-05-23.txt. What commands can be run in this directory to find all files that exceed a certain number of days (I have a threshold that is currently set for 7 days, can change) and print their file names?

+5
source share
4 answers

The BSD is -jused to prevent the date from being set, and the parameter is -fused to set the input date format.

Firstly, you need to find today the date in the number of days since January 1, 1970:

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

Now you can use this to find out the time seven days ago:

 ((cutoff = $today - 604800))

The number 604800 is the number of seconds in seven days.

. , . (, - 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)

, .

+2

bash script 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 \;
+5

, :

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

0
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

This is one of my longest 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
0
source

All Articles