brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -daystart makes -mtime count calendar days instead of 24 hours

Find
~/tips/tip-find-daystart-calendar-days

find /var/log/app -daystart -type f -mtime 0 # changed today

Makes find's day tests start at midnight, so yesterday means yesterday on the calendar.

find counts days as blocks of 24 hours back from right now. At 9 in the morning, -mtime 0 includes yesterday evening too. That’s rarely what I mean by today.

-daystart moves the starting point to the beginning of today, so the day tests follow the calendar:

find /var/log/app -daystart -type f -mtime 0     # changed today
find /var/log/app -daystart -type f -mtime 1     # changed yesterday

Take a file with a time of 23:00 yesterday. Plain -mtime 0 matches it, because it is less than 24 hours ago. With -daystart, it is gone from -mtime 0 and shows up at -mtime 1, which is what you’d expect from a list of yesterday’s files.

It works for -amin, -mmin and the other time tests too, and for ranges: -daystart -mtime -7 gives everything since midnight six days ago.

Watch the position. -daystart is an option that affects the tests that come after it, so it has to be in front of -mtime in the command. Put it at the end and nothing changes. Also, “today” is based on the local time zone of the machine, which on a server is often UTC.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link