Grep command series: how to find files based on the file content in UNIX
To complete the course assignments, I wrote a lot of C-language code and saved them as/home/user/c/*. c and *. h. In the UNIX shell window, how can I find a file through a string or word (for example, the function name main?
You need to use the following tools:
[A] grep command: output the matching mode line.
[B] find command: find files at the directory level.
Use the grep command to search for files based on the Content
Enter the following command:
grep'string'*.txt
grep'main('*.c
grep'#include<example.h>'*.c
grep'getChar*'*.c
grep-i 'ultra'*.conf
grep-iR 'ultra'*.conf
Where
- -I: Case sensitivity of the ignore mode (matching string valid, VALID, and ValID) and input file (matching FILE. c FILE. c file. C.
- -R: recursively reads all files in each directory.
Highlighted matching mode
You can easily highlight a large number of files when searching:
$ grep--color=auto-iR 'getChar();'*.c
Display the file name and row number for the searched Mode
You may need to display the file name and row number:
$ grep--color=auto-iRnH 'getChar();'*.c
Where,
- -N: add a line number starting with 1 before each output line.
-H: print the file name for each matching file. This is the default option when you want to search for multiple files. (LCTT :-h option forcibly hides the file name. In addition, the-l and-L options are used to only display the matching/mismatched file names, -H and-h are used to control whether to display/not display the file name before displaying matching rows. Note the difference .)
$grep --color=auto-nH 'DIR'*
Output example:
Figure 01: The grep command displays the searched mode.
You can also use the find command:
$ find.-name "*.c"-print|xargsgrep"main("
Grep uses concise and Regular Expressions
Shell programming in Linux -- basic use of the grep command
Grep command details and related examples
Linux basic commands-grep
Set grep to highlight matching items
Linux grep command learning and summary
Example of 14 grep commands
Via: http://www.cyberciti.biz/faq/unix-linux-finding-files-by-content/
Author: Vivek Gite Translator: ictlyh Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates the link address: