11

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.

Kamath
  • 4,461
  • 5
  • 33
  • 60
  • You might want to include examples of your current commands. Saves us the trouble of pointing out the obvious stuff that you already know. – sehe Jan 13 '12 at 15:12

3 Answers3

20

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)
ovenror
  • 552
  • 4
  • 11
12

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thanks, I came across something more valuable than just answer. I had never come across GCCSense. I was looking for eclipse code completion capabilities in vim. Guess its very recently developed. – Kamath Jan 16 '12 at 05:09
  • Ah ha! It's so obvious! – Guillochon May 14 '16 at 16:18
2

Use this. Because if find doesn't return a file, then grep will keep waiting for an input halting the script.

find . -mtime 0 -type f | xargs grep "pattern"
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
stirderAX
  • 21
  • 3