Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Grep how to find files based on file contents in UNIX     - Linux common network tools: traceroute routing of scanned (Linux)

- How to install Linux Kernel 4.4 on Ubuntu (Linux)

- Linux disk and File System Concepts (Linux)

- C ++ function object (Programming)

- Selection sort, insertion sort, and Shell sort (Programming)

- Git remote repository Explanation (Linux)

- Eclipse installs support for Java 8 (Linux)

- iostat command Detailed (Linux)

- Linux ldconfig command (Linux)

- Zabbix installation and configuration process (Server)

- Graphic Git (Linux)

- How to use the internal network IP forwarding to connect to the Internet on Linux (Linux)

- Analysis of native Java keywords (Programming)

- Network security system (Network)

- Git large file storage will help handle large binary files (Linux)

- CentOS 7.0 Experience with previous versions (Linux)

- Httpclient4.4 of principle (Http execution context) (Programming)

- Deploy Apache Spark cluster environment in Ubuntu (Server)

- CentOS 6.6 command-line automatic completion (Linux)

- Let Ubuntu 14.04 Save screen luminance value (Linux)

  Grep how to find files based on file contents in UNIX
  Add Date : 2018-11-21      
  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
among them

-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.
among them,

-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 ("
- FreeRadius installation process record (Linux)
- HBase Application Development Review and Summary of Series (Database)
- Depth understanding of DB2 table space (Tablespace) (Database)
- Servlet life cycle works (Programming)
- How to install Visual Studio Code on Ubuntu (Linux)
- Handle large data problems Bit-map method (Programming)
- Install GAMIT / GLOBK 10.50 software under Ubuntu 14.04 (Linux)
- Ubuntu 14.10 PPA installed Android Studio (Linux)
- 6 common PHP security attacks (Linux)
- MySQL root password reset under CentOS (Database)
- How to use the command line ftp upload and download files (Linux)
- How to install Docker and basic usage on Ubuntu 15.04 (Server)
- VPN built on CentOS (Server)
- How to modify the Ubuntu terminal user name changed to red color (Linux)
- How to fix the Ubuntu Error script returned error exit status 1 (Linux)
- The principle Httpclient4.4 (HttpClient Interface) (Programming)
- The Sublime Text 3 configuration file (Linux)
- swap space is insufficient cause OOM kill MySQL Case (Database)
- Android Studio quick overview of Gradle (Programming)
- NFS-based services and service utilization Corosync DRBD high availability cluster configuration, respectively (Server)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.