Macos – How to specify a date range for find command on OSX

date timedate-modifiedfindmacos

I need to find files within a date range to the minute. I suppose knowing how to do it to the second might be beneficial one day. Anyhow. I've learned how to use -newermt and even -not -newermt; however, I can't seem to get correct results when I combine them. I found linux examples of using find, but don't think the switches are working on OSX. See the following list of files:

#$ ls -l
total 32
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test1.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test1_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test2.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test2_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test3.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test3_old.swc
-rw-r--r--  1 testuser  staff  1024 Oct 26 20:12 test4.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 test4_old.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 20:11 test5.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 20:13 test6.swc
-rw-r--r--  1 testuser  staff     0 Oct 26 19:00 timestamp.tmp

I would expect the following command to return test1.swc through test4.swc; however, notice that it returned test6.swc:

#$ find . -newermt '2010-10-26 20:11' -a -not -newermt '2010-10-26 20:13'
./test1.swc
./test2.swc
./test3.swc
./test4.swc
./test6.swc

I thought the minute for the -not condition was off so I tried the following, but it returned nothing:

#$ find . -newermt '2010-10-26 20:11' -a -not -newermt '2010-10-26 20:12'
#$ 

I've concluded that I'm not properly combining the -newermt and -not -newermt switches. Any advice on how to correct this?

Best Answer

Your syntax looks correct. How did you create these files. I have seen the wrong return when just using touch to update the date.

In BSD -a is not necessary, and I tend to use ! instead of -not (personal perference)

When searching for command help remember that you want to search BSD, because there are some differences I use FreeBSD man pages

Remember that on the file there is something beyond minutes, so if you want the first second then you need to mark it. I set up a structure like yours and when I added the :00 for the seconds I could get the same results that you got, where it didn't make sense. so I did an ls -lT and was able to see the seconds on the file and then ran the find command and got my expected results it looks like by default the ! or -not sets the seconds to 59 so it includes the entire minute.

#$ find . -newermt "2010-10-26 20:11:00" ! -newermt "2010-10-26 20:13:00"