Re: how to ask grep to search specific file recurisively?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

> if I want grep do the same thing recursively, I try to use "grep -r
> 'test' *.h" and it complains no *.h such file.
> Would anyone know how to use grep to search specific file recursively?
> appreciate your help,

You could use find and it's exec option. Or, you could also use find and xargs.

$ find . -type f -iname '*.h' -exec grep -H 'test' {} \;
or
$ find . -type f -iname '*.h' | xargs grep -H 'test'

Explanation:
find: the program that does the job :)
-type f: Check only for files. -type d will check for directories.
Similarly, you can search for pipes, char devices, etc.
-iname regex: case insensitive match regex. You can also invert the regex.
-exec grep -H 'test': for each found file, exec the command
{}: Replace this by the file that is found
\;: Escape the semicolon (so that it's understood by find, and not
your shell); which indicates the end of -exec command.

`xargs program-with-options' does something like this:

for each line in stdin:
  invoke program-with-options $line

Hope that helps,
Regards,
-- 
Vimal

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux