28

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

5 Answers 5

34

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?

7

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
3

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

3

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
print -rl -- **/*.php(.e:age 2011/02/08:)
0
-5

In most cases I use basic commands:

ls -l | grep 2017 | grep Apr\ 11
ls -l | grep 2017 | grep Apr\ 1\

Note: add space at the end of second command. Files, mixed with directories, are not problem in case of interactive search on command line.

7
  • 2
    Will probably fail for files that have names that contain either 2017 or Apr 4 as substrings. Also, this will output a lot more than just filenames.
    – Kusalananda
    Apr 7, 2020 at 7:03
  • Is it not an answer of original question? Simple and basic Apr 7, 2020 at 11:48
  • The issue is that it's too simplistic. Any file can contain the strings 2017 and Apr 11 in their names without having been created or modified on that date. Your commands would pick these up. You also, as I pointed out in my last comment, get more data out of this than just the filename. So no, this does not answer the question.
    – Kusalananda
    Apr 7, 2020 at 11:59
  • You are right about "file can contain the strings 2017 and Apr 11 in their names". But why do you decide it is a problem? I'm (php) programmer on Windows desktop and have basic knowledge of linux. Why do you force me browse internet and search command, which I forgot for a years? ls and grep, basic linux commands, fully satisfy me in 99% cases. Do I be forced waste my time by nobody wishes. I do use ls | grep and will use in the future in most cases. Read carefully original question and thing about author needs. Apr 8, 2020 at 5:30
  • Ok, let's do a test: run the command touch 'notes from 2017 Apr 1.txt'. This file is clearly not last modified of April the 1st, 2017 but today. The original questions asks about finding (PHP) files that were modified on a particular date. If the date is April 1st, 2017, then this new file should not turn up in the response. Also, I do not force you to do anything, but I do care about the quality and correctness of answers on this site.
    – Kusalananda
    Apr 8, 2020 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .