user4288514
user4288514

Reputation: 75

Know if last file in a folder is older than 1 hour

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

Answers (3)

pjh
pjh

Reputation: 8064

Try

find filename -mmin +60 -printf 'banana\n' -quit

Upvotes: 0

oliv
oliv

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

tink
tink

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

Related Questions