Maybe someone can use it. Find all files which were modified within a certain time frame recursively, just run:
find . -type f -newermt "2013-06-01" \! -newermt "2013-06-20"
Assuming a modern release, find -newermt
is powerful:
find -newermt '10 minutes ago' ## other units work too, see `Date input formats`
or, if you want to specify a time_t
(seconds since epoch):
find -newermt @1568670245
For reference, -newermt
is not directly listed in the man page for find. Instead, it is shown as -newerXY
, where XY
are placeholders for mt
. Other replacements are legal, but not applicable for this solution.
From man find -newerXY
:
Time specifications are interpreted as for the argument to the -d option of GNU date.
So the following are equivalent to the initial example:
find -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d '10 minutes ago')" ## long form using 'date'
find -newermt "@$(date +%s -d '10 minutes ago')" ## short form using 'date' -- notice '@'
The date -d
(and find -newermt
) arguments are quite flexible, but the documentation is obscure. Here's one source that seems to be on point: Date input formats
You can also do this without a marker file.
The %s format to date is seconds since the epoch. find's -mmin flag takes an argument in minutes, so divide the difference in seconds by 60. And the "-" in front of age means find files whose last modification is less than age.
time=1312603983
now=$(date +'%s')
((age = (now - time) / 60))
find . -type f -mmin -$age
With newer versions of gnu find you can use -newermt, which makes it trivial.
Given a unix timestamp (seconds since epoch) of 1494500000
, do:
find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)"
To grep those files for "foo":
find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)" -exec grep -H 'foo' '{}' \;
You can find every file what is created/modified in the last day, use this example:
find /directory -newermt $(date +%Y-%m-%d -d '1 day ago') -type f -print
for finding everything in the last week, use '1 week ago' or '7 day ago' anything you want
So there's another way (and it is portable to some extent_
(python <<EOF
import fnmatch
import os
import os.path as path
import time
matches = []
def find(dirname=None, newerThan=3*24*3600, olderThan=None):
for root, dirnames, filenames in os.walk(dirname or '.'):
for filename in fnmatch.filter(filenames, '*'):
filepath = os.path.join(root, filename)
matches.append(path)
ts_now = time.time()
newer = ts_now - path.getmtime(filepath) < newerThan
older = ts_now - path.getmtime(filepath) > newerThan
if newerThan and newer or olderThan and older: print filepath
for dirname in dirnames:
if dirname not in ['.', '..']:
print 'dir:', dirname
find(dirname)
find('.')
EOF
) | xargs -I '{}' echo found file modified within 3 days '{}'
Source: Stackoverflow.com