Grep command series: how to find files based on the file content in UNIX

Source: Internet
Author: User

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:

  1. grep'string'*.txt
  2. grep'main('*.c
  3. grep'#include<example.h>'*.c
  4. grep'getChar*'*.c
  5. grep-i 'ultra'*.conf
  6. 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:

  1. $ 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:

  1. $ 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 .)

  1. $grep --color=auto-nH 'DIR'*

Output example:

Figure 01: The grep command displays the searched mode.

You can also use the find command:

  1. $ 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.