Ranjan Shetty
Ranjan Shetty

Reputation: 43

Find command not working with -mtime and -daystart parameter in linux-bash

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

Upvotes: 4

Views: 2294

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295443

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

Upvotes: 4

codeforester
codeforester

Reputation: 42999

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:

Upvotes: 2

Related Questions