To complete the course work, I write a lot of C language code and save them as /home/user/c/*.c and * .h. Then the UNIX shell window How can I pass a string or a word (for example, the function name main ()) contents of the file to find the file it?
You will need the following tools:
[A] grep command: output matching model line.
[B] find command: find files in the directory hierarchy.
Use the grep command to find files based on content
Enter the following command:
grep'string '*. txt
grep'main ( '*. c
grep '# include ' *. c
grep'getChar * '*. c
grep-i 'ultra' *. conf
grep-iR 'ultra' *. conf
-i: ignore patterns (matching string valid, VALID, ValID) and the input file (matching file.c FILE.c FILE.C) case.
-R: Recursively read all files in each directory.
Highlight to match the pattern
Searching for a large number of files when you can easily highlight mode:
$ Grep - color = auto-iR 'getChar ();' * c.
It displays the file name and line number to find patterns
You may want to display the file name and line number:
$ Grep - color = auto-iRnH 'getChar ();' * c.
-n: in front of each line of output is added to the beginning of the line number 1.
-H: Print filename for each match. To search for multiple file This is the default option. (LCTT Annotation: -h option forces hide filenames; additional -l and -L option to display only matches / does not match the file name, and -H and -h for controlling the front display matching lines display / not display file name, note the distinction.)
$ Grep --color = auto-nH 'DIR' *
You can also use the find command:
$ Find.-name "* .c" -print | xargsgrep "main ("