Advertisement

Find the file include the key word in Linux

Started by September 05, 2006 08:16 AM
4 comments, last by Ravuya 18 years, 2 months ago
What is the command if we want to find the file include the keyword given.
Your request is somewhat generic.

In general "grep" is able to scan a file for a given text string. In a directory a
> grep -H "hello" *
will search for the string hello in all files, and print the names of matching files to the shell.

You could, of course, also filter the files of interest. E.g.
> grep -H "hello" *.h
will search only in header files of the current directory.

More possibilities are given by using the "find" command. If you want to grep in header files inside /usr/include (and its subdirectories), you can use
> find /usr/include -name "*.h" -exec grep -H "hello" \{\} \;
The "find" command is very potent in filtering: Names, file types, dates, sizes, directory recursion, and so on are supported criteria.

Look at "man grep" and "man find" for more informations on these.


EDIT: If you want to know especially what header file to include for a given C function you may find this information in the "man" page of that function.
Advertisement
I have just tried the command "find /usr/include -name "*.h" -exec grep -H "hello" \{\} \;" but find the message "exec missing argument".how to work it around
The find's exec option is sensitive of how the braces and semicolon appear.

An excerp from the man page:

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the `-exec' option. The specified command is run once for each matched file. The command is executed in the starting directory.

And later in the "Examples" section:

find . -type f -exec file '{}' \;
Runs `file' on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though ';' could have been used in that case also.



On my linux (SuSE 9.3, bash) all these variants work well:
find /usr/include -name "*.h" -exec grep -H "hello" {} \;
find /usr/include -name "*.h" -exec grep -H "hello" "{}" \;
find /usr/include -name "*.h" -exec grep -H "hello" '{}' \;
find /usr/include -name "*.h" -exec grep -H "hello" \{\} \;

But, if I don't escape the semicolon and write down e.g.
find /usr/include -name "*.h" -exec grep -H "hello" {} ;
then the same error occurs. So make sure to follow the hints given in the man page.
Quote: Original post by Heavenhan
I have just tried the command "find /usr/include -name "*.h" -exec grep -H "hello" \{\} \;" but find the message "exec missing argument".how to work it around

  find /usr/include -name \*.h -print | xargs grep -H hello

Stephen M. Webb
Professional Free Software Developer

If it's a kind of standard function or object, you can probably do man -k {command} to look for that command in the manpages.

ToastyBook:~$ man -k atan2atan2(3)                 - arc tangent function of two variables

This topic is closed to new replies.

Advertisement