Getting all files that have been modified on a specific date

56,172

Solution 1

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?

Solution 2

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

Solution 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:)

Solution 4

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

Share:
56,172

Related videos on Youtube

Elitmiar
Author by

Elitmiar

Updated on September 17, 2022

Comments

  • Elitmiar
    Elitmiar over 1 year

    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

  • Kusalananda
    Kusalananda about 4 years
    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.
  • user1855805
    user1855805 about 4 years
    Is it not an answer of original question? Simple and basic
  • Kusalananda
    Kusalananda about 4 years
    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.
  • user1855805
    user1855805 about 4 years
    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.
  • Kusalananda
    Kusalananda about 4 years
    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.
  • terdon
    terdon almost 4 years
    On a Linux system, try: touch -d "April 11 2017" "a file with"$'\n'"a newline" and then try your approach: ls -l | grep 2017 | grep Apr\ 11. You will see that only the first part of the file name will be returned, and you will therefore not get the right name.