-->

日期范围内创建的所有文件中的grep(Grep inside all files created w

2019-06-25 05:29发布

我在Ubuntu操作系统。 我希望到grep一句话(说XYZ),其日期范围内创建28-MAY-2012 30-MAY-2012所有的日志文件中。

我怎么做?

Answer 1:

这是Banthar的解决方案有所不同,但它会随着版本一起find不支持-newermt ,它显示了如何使用xargs命令,这是一个非常有用的工具。

您可以使用find命令来查找文件“一定年龄的”。 这将找到修改的5至10天前的所有文件:

 find /directory -type f -mtime -10 -mtime +5

然后,为了搜索字符串这些文件:

 find /directory -type f -mtime -10 -mtime +5 -print0 |
   xargs -0 grep -l expression

您也可以使用-exec开关,但我发现xargs更具可读性(而且往往会在这种情况下有更好的表现,太,但可能不是)。

(注意-0标志是有没有让这个命令操作文件嵌入空间,如this is my filename 。)

更新注释中的问题

当您提供多个表达式find ,他们正在与运算在一起。 例如,如果你要求:

find . -name foo -size +10k

...... find将只返回都(一)命名的文件foo (b)大于10千字节大。 同样,如果您指定:

find . -mtime -10 -mtime +5

... find只返回文件是(a),比10天前 (b)超过5天前更新。

例如,我的系统上它目前是:

$ date
Fri Aug 19 12:55:21 EDT 2016

我有以下文件:

$ ls -l
total 0
-rw-rw-r--. 1 lars lars 0 Aug 15 00:00 file1
-rw-rw-r--. 1 lars lars 0 Aug 10 00:00 file2
-rw-rw-r--. 1 lars lars 0 Aug  5 00:00 file3

如果我要“修改超过5天前的文件( -mtime +5 )我得到:

$ find . -mtime +5
./file3
./file2

但是,如果我问“修改超过5天前,但不到10天前的文件”( -mtime +5 -mtime -10 ),我得到:

$ find . -mtime +5 -mtime -10
./file2


Answer 2:

结合使用grep的发现 :

find -newermt "28 May 2012" -not -newermt "30 May 2012" -exec grep XYZ \{\} \;


Answer 3:

find似乎没有有选择,您可以指定时间戳比较具体日期(至少在我的笔记本电脑上的版本不-可能有其他版本和/或执行类似的其他工具),所以你必须要使用天数。 所以,作为2012/06/05的,你想找到超过900天,但年长超过6天更新的文件:

find . -type f -ctime -9 -ctime +6 -print0 | xargs -0 grep XYZ


文章来源: Grep inside all files created within date range