devops
devops

Reputation: 193

Using find to locate files modified within yesterday

I would like to get a shell command to find the files with a modification date for yesterday — 24 hours only. That means I would like to find the files which were modified yesterday only.

Upvotes: 1

Views: 2189

Answers (2)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

Use find with mtime and daystart, it will find files modified 1*24 hours ago, starting calculations from midnight (daystart):

find dir -daystart -mtime 1

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 754100

This answer assumes you have GNU date and find. It also assumes that if you run the script at any time on 2016-07-14, you want the files modified at or after 2016-07-13T00:00:00 and before 2016-07-14T00:00:00.

If those assumptions are correct, then you can use:

find .  -newermt "$(date -d yesterday +'%Y-%m-%d 00:00:00')" \
    '!' -newermt "$(date -d today     +'%Y-%m-%d 00:00:00')"

The first command substitution generates (on 2016-07-14) the output 2016-07-13 00:00:00 and the second 2016-07-14 00:00:00. The -d today is not needed to get the right result, but shows the symmetry.

The condition overall means 'modified at or after midnight yesterday morning and not modified since midnight this morning'.

It is harder to do this on systems without GNU date to evaluate the different dates.

See Explaining find -mtime command for information on why using -mtime will not meet the assumed requirements.

Upvotes: 1

Related Questions