4

I am trying to find the files which are created 10 mins ago and with the -daystart parameter (i.e created/modified more than 10 mins but less than daystart) in find command, but -mmin is taking the priority and -daystart is ignored. Any suggestions and comments to fix this issue is appreciated.

Below output shows -daystart is being ignored and ideally only the test file should be listed:

[rshetty@xxx ~]$ date 
Thu Feb 23 12:06:14 CST 2017 
[rshetty@xxx ~]$ find . -maxdepth 1 -type f -daystart -mmin +10 -exec ls -lrt {} \; 
-rw-r--r--. 1 rshetty users 18 Jan 11 2015 ./.bash_logout 
-rw-r--r--. 1 rshetty users 0 Feb 23 11:50 ./test 
-rw-r--r--. 1 rshetty users 231 Jan 11 2015 ./.bashrc 
-rw-r--r--. 1 rshetty users 193 Jan 11 2015 ./.bash_profile
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Are you sure that `-daystart` is what you need? Try setting a time range with anoter `-mmin` or `-mtime` parameter, like `-mmin +10 -mtime -1`. – Murphy Feb 23 '17 at 19:42
  • @Murphy This is a pretty cool suggestion. Thanks! I am still wondering why it is not working with daystart. – Ranjan Shetty Feb 24 '17 at 22:15
  • (Aside: Not really a bash question; you'd have precisely the same behavior starting find from ash/dash/ksh/zsh/fish, or running it with no shell at all (ie. `subprocess.Popen(['find', '.', '-maxdepth', '1', '-type', '-f', '-daystart', '-mmin', '+10', '-exec', 'ls', '-lrt', '{}', ';'])`). – Charles Duffy Feb 24 '17 at 22:25
  • ...another aside: `-exec ls -lrt {} \;` means you're running `ls` once for every file found, which means it can't do any sorting (`-t` doesn't do anything if it's given only one file!) – Charles Duffy Feb 24 '17 at 22:32
  • @CharlesDuffy That's correct I have same problem with other shell too. By the way I am not bothered about -exec ls -lrt {} \; part because sorting is not my requirement - but thank you for mentioning it though. – Ranjan Shetty Feb 24 '17 at 22:43

2 Answers2

4

-daystart modifies the meaning of -mtime, -mmin and related predicates that operate based on time relative (by default) to the present; it isn't a predicate in and of itself, so unless you use one of these other operations, it has no effect on your find command's results.

Thus, if you want to filter mtime relative to the start of the current day, that needs to be specified, as in -mtime -1, after -daystart (whereas your filters relative to the current time should be before -daystart):

find . -type f -mmin +10 -daystart -mtime -1 -exec ls -lrt {} +

Note that we're specifying -mmin +10 before -daystart to make that relative to the current time, but specifying -mtime -1 after -daystart to make that relative to the start of the day.

Note the + instead of the ; -- ls -t has no meaning if you only pass one filename per instance of ls, since sorting a list of size one will always come back with that exact same list. See BashFAQ #3 for discussion of more robust and reliable ways to sort a list of files returned from GNU find by time.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • If I use -mtime -1, find will list the files from last for 24hrs, but I need it from the daystart. – Ranjan Shetty Feb 24 '17 at 23:07
  • That's what `-mtime -1` does when you **don't** also specify `-daystart`. Using `-daystart` changes the meaning of subsequent instances of `-mtime` (and the `-{a,c,m}{min,time}` predicates in general).. – Charles Duffy Feb 24 '17 at 23:11
  • BTW, if the goal of `-mmin +10` is to avoid operating on files that are still being written, there are better ways to do that -- for instance, if you had a incron job you could immediately trigger an operation on *any* file as soon as (but not before!) the process that's creating or modifying it closes its handle. – Charles Duffy Feb 24 '17 at 23:18
  • @RanjanShetty, ...I've made some improvements to this answer that hopefully make its applicability more clear. – Charles Duffy Feb 25 '17 at 14:25
  • Your answers suffix my requirement. Thanks a lot. The need of -mmin +10 is not to filter the files that are current being written, there is a different requirement at my work. – Ranjan Shetty Feb 26 '17 at 03:17
2

Since you need files which were modified in the last 10 minutes or less, you need to pass -10 to -mtime argument, not +10:

find . -maxdepth 1 -type f -daystart -mmin -10 -exec ls -lrt {} \;

See this page:

codeforester
  • 39,467
  • 16
  • 112
  • 140