Using find to locate files modified within yesterday

限于喜欢 提交于 2021-01-20 12:07:15

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/38380079/using-find-to-locate-files-modified-within-yesterday

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!