Linux 基于时间戳的grep文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8851667/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:03:48  来源:igfitidea点击:

grep files based on time stamp

linuxubuntugrep

提问by Kamath

This should be pretty simple, but I am not figuring it out. I have a large code base more than 4GB under Linux. A few header files and xml files are generated during build (using gnu make). If it matters the header files are generated based on xml files.

这应该很简单,但我没有弄清楚。我在 Linux 下有一个超过 4GB 的大型代码库。在构建过程中会生成一些头文件和 xml 文件(使用 gnu make)。如果重要的话,头文件是基于 xml 文件生成的。

I want to search for a keyword in header file that was last modified after a time instance ( Its my start compile time), and similarly xml files, but separate grep queries.

我想在时间实例(它是我的开始编译时间)之后最后修改的头文件中搜索关键字,以及类似的 xml 文件,但单独的 grep 查询。

If I run it on all possible header or xml files, it take a lot of time. Only those that were auto generated. Further the search has to be recursive, since there are a lot of directories and sub-directories.

如果我在所有可能的头文件或 xml 文件上运行它,则需要很多时间。只有那些自动生成的。此外,搜索必须是递归的,因为有很多目录和子目录。

采纳答案by jfs

To find 'pattern'in all files newer than some_filein the current directory and its sub-directories recursively:

'pattern'在所有比some_file当前目录及其子目录中更新的文件中递归查找:

find -newer some_file -type f -exec grep 'pattern' {} +

You could specify the timestamp directly in date -dformat and use other findtests e.g., -name, -mmin.

您可以直接在date -d格式中指定时间戳并使用其他find测试,例如-name, -mmin

The file list could also be generate by your build system if findis too slow.

如果find太慢,文件列表也可以由您的构建系统生成。

More specific tools such as ack, etags, GCCSensemight be used instead of grep.

可以使用更具体的工具,例如acketagsGCCSense来代替grep

回答by ovenror

You could use the findcommand:

您可以使用以下find命令:

find . -mtime 0 -type f

prints a list of all files (-type f) in and below the current directory (.) that were modified in the last 24 hours (-mtime 0, 1 would be 48h, 2 would be 72h, ...). Try

打印过去 24 小时内修改过-type f的当前目录( ) 中和下方的所有文件 ( )的列表.( -mtime 0, 1 表示 48h, 2 表示 72h, ...)。尝试

grep "pattern" $(find . -mtime 0 -type f)

回答by stirderAX

Use this. Because if finddoesn't return a file, then grepwill keep waiting for an input halting the script.

用这个。因为如果find不返回文件,grep则将继续等待停止脚本的输入。

find . -mtime 0 -type f | xargs grep "pattern"