grep在日期范围内创建的所有文件内 [英] Grep inside all files created within date range
问题描述
如何操作这与Banthar的解决方案有点不同,但它可以与 find 不支持
-newermt
,它显示了如何使用 xargs
命令,这是一个非常有用的工具。
您可以使用 find
命令来定位特定时代 。这将找到5至10天之前修改的所有文件:
$ p $ find $ c $ find $ find $ -mtime -10 -mtime + 5
然后搜索这些文件的字符串:
find / directory -type f -mtime -10 -mtime +5 -print0 |
xargs -0 grep -l表达式
您也可以使用 -exec
开关,但是我发现 xargs
更具可读性(在这种情况下,它通常也会更好,但可能不会) / p>
(请注意, -0
标志可以让这个命令在嵌入空格的文件上运行,比如这是我的文件名
。)
注释中的问题更新
当您为查找
提供多个表达式时,它们被与在一起。例如,如果您要求:
find。 -fize -size + 10k
... find
将仅返回(a)名称 foo
和(b)大于10 kbytes的文件。同样,如果您指定:
find。 -mtime -10 -mtime +5
... find
只会返回比10天前更新的文件和(b)5天前以前的文件。
例如,在我的系统上,它是目前:
$ date
8月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
I am on ubuntu OS. I want to grep a word (say XYZ) inside all log files which are created within date range 28-may-2012 to 30-may-2012.
How to do that?
This is a little different from Banthar's solution, but it will work with versions of find
that don't support -newermt
and it shows how to use the xargs
command, which is a very useful tool.
You can use the find
command to locate files "of a certain age". This will find all files modified between 5 and 10 days ago:
find /directory -type f -mtime -10 -mtime +5
To then search those files for a string:
find /directory -type f -mtime -10 -mtime +5 -print0 |
xargs -0 grep -l expression
You can also use the -exec
switch, but I find xargs
more readable (and it will often perform better, too, but possibly not in this case).
(Note that the -0
flag is there to let this command operate on files with embedded spaces, such as this is my filename
.)
Update for question in comments
When you provide multiple expressions to find
, they are ANDed together. E.g., if you ask for:
find . -name foo -size +10k
...find
will only return files that are both (a) named foo
and (b) larger than 10 kbytes. Similarly, if you specify:
find . -mtime -10 -mtime +5
...find
will only return files that are (a) newer than 10 days ago and (b) older than 5 days ago.
For example, on my system it is currently:
$ date
Fri Aug 19 12:55:21 EDT 2016
I have the following files:
$ 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
If I ask for "files modified more than 5 days ago (-mtime +5
) I get:
$ find . -mtime +5
./file3
./file2
But if I ask for "files modified more than 5 days ago but less than 10 days ago" (-mtime +5 -mtime -10
), I get:
$ find . -mtime +5 -mtime -10
./file2
这篇关于grep在日期范围内创建的所有文件内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!