Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep files based on time stamp

Tags:

linux

grep

ubuntu

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.

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.

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.

like image 681
Kamath Avatar asked Jan 13 '12 13:01

Kamath


2 Answers

You could use the find command:

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

grep "pattern" $(find . -mtime 0 -type f)
like image 91
ovenror Avatar answered Oct 01 '22 23:10

ovenror


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

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

You could specify the timestamp directly in date -d format and use other find tests e.g., -name, -mmin.

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

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

like image 41
jfs Avatar answered Oct 01 '22 23:10

jfs