Reputation: 75
I've tried this:
if find filename -mmin +60; then echo banana; fi
but it doesn't seem to work: it echoes banana in any case.
How can I echo banana only if the file is older than 60 minutes?
Upvotes: 0
Views: 1007
Reputation: 13249
You could use both stat
and date
:
((($(date +%s) - $(stat -c +%X file)) < 3600)) && echo banana
date +%s
gives the current time in epoch format.
stat -c +%X
give the last access time of the file filename
in the same format. Use other options like %W
, %Y
for respectively the birth and modification time.
Upvotes: 1
Reputation: 15204
Turning the comment above into something answerish: find
only gives a non-zero exit code if a processing error occurred; the run is still successful, even if your condition isn't met. You need to count the output lines of find for that file, and if it's 1 your condition is met (if there's only one file by that name in the directory you're scanning; otherwise change -eq
to -ge
).
if [[ $( find filename -mmin +60 | wc -l ) -eq 1 ]]; then echo banana; fi
banana
Upvotes: 0