1

What is the difference between the following -

find . -daystart -mtime +5

and

find . -mtime +5

It produced the same output when I tested it.

Is there any advantage of using -daystart? I am a beginner in this field so requesting to kindly advise.

2
  • 1
    @ilkkachu On my FreeBSD system, GNU find with -daystart seems to measure from the beginning of tomorrow, i.e. from the upcoming midnight.
    – Kusalananda
    Jul 8, 2022 at 7:47
  • @Kusalananda, yyeaah, that does read somewhat oddly when I think about it.
    – ilkkachu
    Jul 8, 2022 at 8:29

1 Answer 1

1

It changes the way file ages are compared so that the reference point is midnight instead of the current time (when find runs).

As an example, it's about 11:12 in the morning on 2022-07-08 right now. Consider the following files:

$ touch -d '2022-07-08 09:00' this_morning                #       2 h old
$ touch -d '2022-07-07 23:55' yesterday_before_midnight   #      11 h old
$ touch -d '2022-07-07 09:00' yesterday_morning           # 1 d + 2 h old

-mtime 0 gives files that are zero full days old. By default it compares against the current time, so gives the last 24 h period:

$ find . -type f -mtime 0
./yesterday_before_midnight
./this_morning

With -daystart it more like looks at the calendar date. -daystart -mtime 0 gives files that are "zero days old", i.e. from today:

$ find . -type f -daystart -mtime 0
./this_morning

Similarly for files that are at least "one day old", i.e. more than 24 h old:

$ find . -mtime +0
./yesterday_morning

and ones that are from yesterday or before:

$ find . -daystart -mtime +0
./yesterday_before_midnight
./yesterday_morning

Another way to look at it would be to say that -daystart counts the number of midnights between the file timestamp and the current time.

The +N (-N) modifiers mean "strictly more than" ("less than"), and the rules for -atime/-mtime, are that find calculates the number of 24-hour periods in the file's age, and drops any fractional part. Hence, +0 ends up meaning "at least one", not "at least zero".

The POSIX description for -mtime is:

-mtime n
The primary shall evaluate as true if the file modification time subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n.

So, the default timeline (without -daystart) is something like this:

...-|-- age 2 days --|-- age 1 day ---|-- age 0 days --|
    ^                ^                ^                ^
  72 h ago        48 h ago         24 h ago          current time

And with -daystart, the reference points move to midnights:

...-|-- age 2 days --|-- age 1 day ---|-- age 0 days --|
    ^                ^                ^                ^
  start of         start of        last midnight /    next midnight /
  the day before   yesterday       start of today     end of today
                  

(Files from the future could get negative times, but those can't be specified directly, since the minus means something different. But you could use -mtime -0 and it'd give files modified in the future, or -daystart -mtime -0 which would give files modified tomorrow or later.)

As it happens, -daystart works with -amin/-mmin too, they similarly count from the end of the day:

$ touch -d '12:00' today_1200.txt   # noon today
$ touch -d '23:00' today_2300.txt   # late evening today
$ find . -type f -daystart -mmin +120 
./today_1200.txt
$ find . -type f -daystart -mmin -120
./today_2300.txt
$ find . -type f -daystart -mmin -$((13*60))
./today_2300.txt
./today_1200.txt

(23:00 is less than 120 mins from the end of the day, and 12:00 is more than 120 mins from the end of the day. Both are within 13*60 minutes from the end of the day.)

The man page says about -daystart:

Measure times from the beginning of today rather than from 24 hours ago.

Which sounds like a rather odd phrasing. What they're giving appears to be the cutoff for 1 full day, but the phrasing sounds like they're indicating the zero reference point. That would be the current time by default, and moves to the end of today with -daystart as mentioned and shown above. So, a clearer phrasing, IMO, would be "measure times from the end of today, rather than from the current time".

5
  • 1
    @QuartzCristal, yes, "NextMidnight - FileTime" gives the file age with -daystart. Age increases when the file timestamp goes farther in the past, i.e. it increases backwards. That's just how the math works, like you said. I'm not sure why you'd insist on repeating things like "you don't get it", or "you got fooled" when you then describe the very same logic. You can count forward from the previous midnight if you like, but I'm not sure how it's so important, since if one uses -daystart -mtime +1, the previous midnight isn't there in the logic at all.
    – ilkkachu
    Jul 12, 2022 at 8:43
  • @QuartzCristal, how would you describe it works with -mmin then? E.g. -daystart -mmin -120 gives files that are dated today from 22:00 to 24:00 (plus any future files), and -daystart -mmin +120 gives files that are older than 22:00 today. Or go with e.g. -mmin -$((24*60 + 120)) for files from yesterday 22:00 and newer etc. if you dislike the idea of timestamping files to the future.
    – ilkkachu
    Jul 12, 2022 at 8:52
  • @QuartzCristal, oh, btw, if I was asked to list files based on their age, yes, I would list the newest/youngest ones first, so today, then yesterday, then the day before etc. If that makes me (talk like a) non-human, so be it. Though note that you yourself wrote e.g. "... from now to 24 hours ago", and "... between 24 and 48 hours ago", both going in the order of increasing age, or going backward in time, not forward as you say you humans would talk.
    – ilkkachu
    Jul 12, 2022 at 9:23
  • 1
    @QuartzCristal, Yes, I can see you insist, you brought it up again last night, after the comments were last cleared. I didn't see you answer the points I made, though, a thing that I'd expect is a part of meaningful technical discourse. You don't have to, of course. And FWIW, I'm happy with these comments remaining here, at least that way there'd be less need to repeat the same points yet again later.
    – ilkkachu
    Jul 12, 2022 at 10:51
  • Let us continue this discussion in chat. Jul 12, 2022 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .