Linux 递归查找比给定时间更新的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively find all files newer than a given time
提问by whatupdave
Given a time_t:
给定一个 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.
我正在寻找一个列出所有更新文件的 bash one-liner。比较应考虑时区。
Something like
就像是
find . --newer 1312603983
But with a time_t instead of a file.
但是使用 time_t 而不是文件。
采纳答案by Jonathan Leffler
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.
这有点迂回,因为touch
不接受原始time_t
值,但它应该在脚本中非常安全地完成这项工作。(在 MacOS X 中存在-r
选项date
;我没有仔细检查 GNU。)可以通过直接在touch
命令行中编写命令替换来避免“时间”变量。
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
回答by Dan Bloch
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.
迄今为止的 %s 格式是自纪元以来的秒数。find 的 -mmin 标志接受一个以分钟为单位的参数,因此将秒差除以 60。年龄前面的“-”表示查找最后修改时间小于年龄的文件。
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.
对于较新版本的 gnu find,您可以使用 -newermt,这使得它变得微不足道。
回答by vadonka
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
要查找上周的所有内容,请使用“1 周前”或“7 天前”您想要的任何内容
回答by test30
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 '{}'
回答by Stefan Diabo
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"
回答by rsanden
Given a unix timestamp (seconds since epoch) of 1494500000
, do:
给定 Unix 时间戳(自纪元以来的秒数)1494500000
,请执行以下操作:
find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)"
To grep those files for "foo":
要为 "foo" 查找这些文件:
find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)" -exec grep -H 'foo' '{}' \;
回答by nobar
Assuming a modern release, find -newermt
is powerful:
假设一个现代版本,find -newermt
是强大的:
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):
或者,如果您想指定一个time_t
(自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.
供参考,-newermt
没有直接列在手册页中供查找。相反,它显示为-newerXY
,其中XY
是 的占位符mt
。其他替换是合法的,但不适用于此解决方案。
From man find -newerXY
:
来自man find -newerXY
:
Time specifications are interpreted as for the argument to the -d option of GNU date.
时间规范被解释为 GNU 日期的 -d 选项的参数。
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
在date -d
(和find -newermt
)参数是相当灵活,但文档是模糊的。这是一个似乎很重要的来源:日期输入格式