Is it possible to find all php files within a certain directory that have been modified on a certain date

I'm using

find /var/www/html/dir/ -mtime -28 | grep '\.php' 

to get files modified within the last 28 days, but I only need files that have been modified on the following date: 2011-02-08


On recent versions of find (e.g. GNU 4.4.0) you can use the -newermt option. For example, to find all files that have been modified on the 2011-02-08

$ find /var/www/html/dir/ -type f -name "*.php" -newermt 2011-02-08 ! -newermt 2011-02-09

Also note that you don't need to pipe into grep to find php files because find can do that for you in the -name option.

Take a look at this SO answer for more suggestions: How to use 'find' to search for files created on a specific date?


Annoyingly, there isn't any direct way with standard find. Recent versions of find on GNU systems (e.g. non-embedded Linux, Cygwin) and some *BSDs have options such as -newermt to compare a file date with a spelled-out date.

With standard find, all you can do is compare the file date with the current date (-mtime) or with a fixed file. The current date is usually not useful in this case (it counts back from the time you run the find command, whereas most applications require a calendar date). That leaves you with the kludge of creating temporary files to define a range.

touch -t 201103070000 start.tmp
touch -t 201103080000 stop.tmp
find . -newer start.tmp \! -newer stop.tmp -print
rm start.tmp stop.tmp

You have almost the right command already, for versions of find that won't let you use dates:

find /var/www/html/dir/ -mtime 27 | grep '\.php'

In general, for find -n means fewer than, n means equal, +n means "more than". Traditional find has some exceptions, but GNU find and other newer versions such as on BSD/Mac OS X corrected those. (Keep this in mind if you ever find yourself on a Solaris or other commercial Unix system.)


With zsh you could use the function age to print only the names of files that have been modified on a certain date:

autoload age
print -rl -- *.php(.e:age 2011/02/08:)

or, if you want to search recursively:

autoload age
setopt extendedglob
print -rl -- **/*.php(.e:age 2011/02/08:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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