Recursively find all files newer than a given time
Given a time_t:
⚡ date -ur 1312603983
Sat 6 Aug 2011 04:13:03 UTC
I'm looking for a bash one-liner that lists all files newer. The comparison开发者_运维知识库 should take the timezone into account.
Something like
find . --newer 1312603983
But with a time_t
instead of a file.
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
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"
NOTE:
the first date is included, the second date is excluded, so that commands like
find . -type f -newermt "2013-06-20" \! -newermt "2013-06-20"
always return 0
This is a bit circuitous because touch
doesn't take a raw time_t
value, but it should do the job pretty safely in a script. (The -r
option to date
is present in MacOS X; I've not double-checked GNU.) The 'time' variable could be avoided by writing the command substitution directly in the touch
command line.
time=$(date -r 1312603983 '+%Y%m%d%H%M.%S')
marker=/tmp/marker.$$
trap "rm -f $marker; exit 1" 0 1 2 3 13 15
touch -t $time $marker
find . -type f -newer $marker
rm -f $marker
trap 0
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
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 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.
There is PowerShell available on Linux for some time so I recommend to use that since it does not deal just with pure text but handles real objects and so avoids formatting and make the task much easier
ls -recurse | where lastwritetime -gt ((get-date).AddDays(-1))
It's another way. You can recursively find files newer than a given timestamp using touch -d
and find /dir -newer
commands.
For example, if you need find files newer than '1 June 2018 11:02', you can create a file with this creation date.
touch -d '1 June 2018 11:02' ref_timestamp
Then, you can use the file timestamp as reference in find
command.
find /dir -newer ref_timestamp
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 '{}'
精彩评论