115

I'm trying to remove all the dated logs except the most recent. Before I execute a script to remove the files, I want to of course test my commands to make sure I'm bringing up accurate results.

When executing these commands the date is:

Sep  1 00:53:44 AST 2014

Directory Listing:

Aug 27 23:59 testfile.2014-08-27.log
Aug 28 23:59 testfile.2014-08-28.log
Aug 29 23:59 testfile.2014-08-29.log
Aug 30 23:59 testfile.2014-08-30.log
Aug 31 23:59 testfile.2014-08-31.log
Sep  1 00:29 testfile.log

I thought -mtime +1 was supposed to list all files over a day old. Why isn't the 8-30.log one listed?

find . -type f -mtime +1 -name "testfile*log"
./testfile.2014-08-27.log
./testfile.2014-08-28.log
./testfile.2014-08-29.log

This is the desired effect, but it was just trial and error. What is this 0 saying?

find . -type f -mtime +0 -name "testfile*log"
./testfile.2014-08-30.log
./testfile.2014-08-27.log
./testfile.2014-08-28.log
./testfile.2014-08-29.log
1
  • The question is not exact: "most recent" is as of my understanding something like "latest" or "youngest" - this is something other than "the last 24 hours" what the example shows. Commented Feb 22, 2022 at 7:58

4 Answers 4

165

The POSIX specification for find says:

-mtimen 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.

Interestingly, the description of find does not further specify 'initialization time'. It is probably, though, the time when find is initialized (run).

In the descriptions, wherever n is used as a primary argument, it shall be interpreted as a decimal integer optionally preceded by a plus ( '+' ) or minus-sign ( '-' ) sign, as follows:

+n More than n.
  n Exactly n.
-n Less than n.

Transferring the content of a comment to this answer.

You can write -mtime 6 or -mtime -6 or -mtime +6:

  • Using 6 without sign means "equal to 6 days old — so modified between 'now - 6 * 86400' and 'now - 7 * 86400'" (because fractional days are discarded).
  • Using -6 means "less than 6 days old — so modified on or after 'now - 6 * 86400'".
  • Using +6 means "more than 6 days old — so modified on or before 'now - 7 * 86400'" (where the 7 is a little unexpected, perhaps).

At the given time (2014-09-01 00:53:44 -4:00, where I'm deducing that AST is Atlantic Standard Time, and therefore the time zone offset from UTC is -4:00 in ISO 8601 but +4:00 in ISO 9945 (POSIX), but it doesn't matter all that much):

1409547224 = 2014-09-01 00:53:44 -04:00
1409457540 = 2014-08-30 23:59:00 -04:00

so:

1409547224 - 1409457540 = 89684
89684 / 86400 = 1

Even if the 'seconds since the epoch' values are wrong, the relative values are correct (for some time zone somewhere in the world, they are correct).

The n value calculated for the 2014-08-30 log file, therefore, is exactly 1 (the calculation is done with integer arithmetic), and the +1 rejects it because it is strictly a > 1 comparison (and not >= 1).

6
  • 3
    Simpy, please.. If user want to get file modified 2 days AGO and older- 3 days, 4,.., he has to set mtime to +1? In this case minus case is little nonse- pointer to the future? :D Am I right? :) Commented Dec 12, 2017 at 9:26
  • 23
    @xxxvodnikxxx: I'm not sure I understand what you're asking. However, you can write -mtime 6 or -mtime -6 or -mtime +6. The 6 without sign means "equal to 6 days old — so modified between 'now - 6 * 86400' and 'now - 7 * 86400'" (because fractional days are discarded), while -6 means "less than 6 days old — so modified since 'now - 6 * 86400'" and +6 means "more than 6 days old — so modified on or before 'now - 7 * 86400'" (where the 7 is a little unexpected, perhaps). If that doesn't help (or you can demonstrate it's wrong), please ask again, explaining your problem more clearly. Commented Dec 12, 2017 at 17:55
  • 1
    Aw, thanks mate, that is exactly what didnt made me the sense :) So the number still means the barrier "in the past", ans sign just tells on which side of interval files should be, nice explanation :) Commented Dec 13, 2017 at 8:10
  • 4
    @Jonathan Leffler your comment made more sense then the answer. x). One just want a simple understanding and suddenly... UTC ISO8601... ISO9945...POSIX...1409547224 - 1409457540 = 89684... Comment really helped. Thanks.
    – brat
    Commented Oct 23, 2020 at 14:15
  • Hindi isn't an approved language on this site. Google identifies "kehna kya chahte ho?" as being Hindi for "What do you want to say?" — which is a good question for you to answer, @achhainsan, in English. Commented Aug 27, 2023 at 5:44
26

+1 means 2 days ago. It's rounded.

1
  • 20
    The core point of this answer is correct, but it is missing a good deal of explanation. +1 means 2 or more days ago (or at least 2 days ago). It would be helpful to quote a manual page, or something similar. Commented Sep 1, 2014 at 6:01
12

To find all files modified in the last 24 hours use the one below. The -1 here means changed 1 day or less ago.

find . -mtime -1 -ls
3
  • 6
    or fine tune for just below "-1" e.g. use "-mmin -360" or "-mtime -0.25" for the last 6 hours.
    – blaucuk
    Commented Jun 24, 2021 at 13:30
  • find: -mtime: -0.25: bad unit '.'
    – alper
    Commented Jun 26, 2022 at 22:18
  • @alper sorry don't see a problem here. mtime paramater doesn't have to be an integer necessarily.
    – blaucuk
    Commented Jun 30, 2022 at 8:48
1
#{user} is user-name
#name of script is 'place.{user}'
#used manually or from cron
#moves files that are created by automated job queue at night 
 for the user and identified by find into dated 
 subdirectories in user's home directory, so moves them 
 from"
 /u/home/{user} to /u/home/{user}/2022/05/05 on the 5th of 
 May in 2022.

cd /u/home/{user}/
place=`date '+./%Y/%m/%d/'`;
find ./*.csv -mtime -.6 -exec mv {} $place \;
find ./*.txt -mtime -.6 -exec mv {} $place \;
find ./*.tab -mtime -.6 -exec mv {} $place \;
find ./*.pdf -mtime -.6 -exec mv {} $place \;
cd $place
chmod 666 ./*
chown {user} ./*
chgrp users ./*
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 7, 2022 at 20:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.