Find files which are created a certain time after or before a particular file was created
Try the following shell code:
file=</PATH/TO/FILE>
date=$(perl -le '$s = (stat($ARGV[0]))[9]; print $s;' "$file")
now=$(date +%s)
seconds=$((now - date))
mins=$((seconds / 60))
find . -mmin -$((mins + 60 )) -mmin +$((mins - 60)) -print
Another complicated option:
- get
test.txt
's modification time (mtime
) - calculate
"before delta" = now + hour - mtime
(assumingmtime
is in the past) - calculate
"after delta" = now - hour - mtime if now - mtime > hour else 0
- run
find -type f -mmin -"before delta" -mmin +"after delta"
It finds all files that are modified less than "before delta" minutes ago and greater than "after delta" minutes ago i.e., +/- hour around test.txt
's modification time.
It might be simpler to understand if you draw now
, mtime
, "before"
, "after"
times on a line.
date
command allows to get now
and mtime
.
As a one-liner:
$ find -type f -newermt "$(date -r $file) -1 hour" -a \
\! -newermt "$(date -r $file) +1 hour"