Bash Finding file by access / modification time


Example

On an ext filesystem, each file has a stored Access, Modification, and (Status) Change time associated with it - to view this information you can use stat myFile.txt; using flags within find, we can search for files that were modified within a certain time range.

To find files that have been modified within the last 2 hours:

$ find . -mmin -120

To find files that have not been modified within the last 2 hours:

$ find . -mmin +120

The above example are searching only on the modified time - to search on access times, or changed times, use a, or c accordingly.

$ find . -amin -120
$ find . -cmin +120

General format:

-mmin n : File was modified n minutes ago
-mmin -n : File was modified less than n minutes ago
-mmin +n : File was modified more than n minutes ago


Find files that have been modified within the last 2 days:

find . -mtime -2

Find files that have not been modified within the last 2 days

find . -mtime +2

Use -atime and -ctime for access time and status change time respectively.

General format:

-mtime n : File was modified nx24 hours ago
-mtime -n : File was modified less than nx24 hours ago
-mtime +n : File was modified more than nx24 hours ago

Find files modified in a range of dates, from 2007-06-07 to 2007-06-08:

find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08  

Find files accessed in a range of timestamps (using files as timestamp), from 1 hour ago to 10 minutes ago:

touch -t $(date -d '1 HOUR AGO' +%Y%m%d%H%M.%S) start_date
touch -t $(date -d '10 MINUTE AGO' +%Y%m%d%H%M.%S) end_date
timeout 10 find "$LOCAL_FOLDER" -newerat "start_date" ! -newerat "end_date" -print  

General format:

-newerXY reference : Compares the timestamp of the current file with reference. XY could have one of the following values: at (access time), mt (modification time), ct (change time) and more. reference is the name of a file whe want to compare the timestamp specified (access, modification, change) or a string describing an absolute time.