2

I'm using the following to find all files that are older than a reference file...

find /home/testuser -name "dummyfiles*" ! -newer referencefile.txt

... which works as expected.

However, I want to add a bit of leniency to the time, so that it only finds files that are older than the reference file - a few hours.

For example, if I have a reference file with modification date May 26 11:26, I want to find files that are older than May 26 9:26 (ie, with a 2-hour leniency on the reference file).

In this example, the following would be correct matches...

May 26 7:00
Apr 15 14:00

... and the following should not match...

Jun 13 9:00
May 26 10:00

Ultimately I want to delete all the files that I find, so would love it if find was able to handle this requirement? Otherwise, are there any other alternatives that will allow me to delete these files without being too convoluted?

2
  • Please, can you clarify if you are looking for 1) files older than the reference file 2hour OR more than 2hour (>=) 2) files that are older that the reference file exactly 2 hour. (=)
    – lese
    Nov 6, 2015 at 8:52
  • I'm looking for files that are older that the reference file less 2 hours. See the example in my question - in the example, I would want all files that are older than May 26 9:26, so it should also match files with dates May 26 7:00, Apr 15 14:50, ... but not match dates Jun 13 9:00 or May 26 10:00. Nov 9, 2015 at 2:30

2 Answers 2

1

On a GNU system, you could do something like:

TZ=UTC0 find /home/testuser -name "dummyfiles*" ! -newermt "$(
  TZ=XXX2:00:00 date -r referencefile.txt +%FT%T.%N)"

That is, get date to print the modification of the file in a timezone 2 hours behind UTC (so if the file was last modified at 15:00 UTC, date will print 13:00) and get find to interpret that timestamp as UTC time (so in our example, will try to find files last modified before 13:00 UTC).

That's limited to offsets of up to 24 hours (or 48 hours if you use TZ=XXX-24 for find). For arbitrary offsets, you can do:

(
export TZ=UTC0
find /home/testuser -name "dummyfiles*" ! -newermt "$(
  date -d "$(date -r referencefile.txt +%FT%T.%N) - 2 hours" +%FT%T.%N)"
)

Or you could use perl:

perl -MFile::Find -le '
  $min_age = (-M "referencefile.txt") + 2/24;
  find (sub {
    print $File::Find::name if $_ =~ /^dummyfiles/ && (-M$_) > $min_age
    }, @ARGV)
  ' -- /home/testuser

Note that contrary to find (but like GNU date -r), for symlinks perl's -M considers the age of the file it links to, not the age of the symlink itself. The subsecond part of the timestamp is also not considered.

-2

Try out with this :

find /home/testuser -name "dummyfiles*" ! -newer referencefile.txt -mmin +120 -print

adding the parameter -mmin +120 find command will return only the files older than 2-hour in comparison to referencefile.txt modification date.

test:

mkdir find
touch find/dummyfiles1 find/dummyfiles2 find/dummyfiles3 find/dummyfiles4 find/referencefile.txt
touch -t 8001031305 dummyfiles1 dummyfiles2
find find/ -name "dummyfiles*" ! -newer find/referencefile.txt -mmin +120

output:

find/dummyfiles1

find/dummyfiles2

4
  • 1
    You should explain why your solution is different to what the OP is already doing, and how it solves their problem.
    – cas
    Oct 29, 2015 at 6:33
  • Improved the answer, It was so late in my country : )
    – lese
    Oct 29, 2015 at 8:53
  • 4
    No, this doesn't do what you claim. This command matches files that are older than referencefile.txt and at least two hours old. Nothing here says “two hours older than the reference file”. Oct 31, 2015 at 23:00
  • This answer do exaclty what I claim (don't call me storyteller) : "return only the files older than 2-hour"(it's what I'm claiming). Anyway, maybe I missed understood the question. Waiting for a clarification from the OP
    – lese
    Nov 6, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.