Time-based find command examples

To find a file that has been modified recently use the -mtime option. With this option you specify time in 24-hour periods. This is the shortest time span you can search with:

find . -mtime 0

And this should round up to one full 24 hour period:

find . -mtime 1

If you want to be more precise, and find a file or directory that has been modified since noon on August 1st you can use this series of commands:

touch 08011200 poop
find . -mnewer poop
rm poop

In that sequence I created a temporary file named "poop", and gave it a very specific time stamp of noon on August 1st. Then I told the find command to find anything below the current directory that had a modification time newer than that file. And then when you're done you don't need "poop" any more.

Although I keep showing modification time, you can do the same things with access time and creation time.

Find something and then execute something

My favorite thing to do with the find command is to find one or more files and then do something to them with one command. Here's how I search every file in or below the "/tmp" directory, looking for a string in all the files named "find me":

find /tmp -type f -exec grep -il "find me" {} \;

This find command uses the grep command, and can be read as "Find all files in or below the '/tmp' directory and use a case-insensitive search to look for the string 'find me' in each of those files. If the string is found, print the file name."

You can do other things using the same find command syntax, such as running the ls command on each file, or even the vi command on those files (if you want to edit them). Here's an example of using it with the ls -l command: