Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Grep how to find files based on file contents in UNIX     - PHP file upload methods exist in the database (Programming)

- Linux, modify / retrieve the root password (Linux)

- 3 tips Linux command (Linux)

- Ubuntu 14.04 next upgrade to GNOME 3.12 (Linux)

- To modify the existing user ID and comments GPG key (Linux)

- Vim useful plugin: EasyGrep (Linux)

- How to configure Proftpd server on Fedora 22 (Server)

- How to build a container cluster (Server)

- How to use the TF / SD card making Exynos 4412 u-boot boot disk in Mac (Linux)

- Linux Security Module (LSM) Introduction (Linux)

- Oracle 11g RAC manually playing GI PSU patch ( (Database)

- Java thread lifecycle (Programming)

- Oracle11g build physical standby database (Database)

- Linux can modify the maximum number of open files (Linux)

- Upgrading KDE Plasma 5.3 in Ubuntu 15.04 (Linux)

- How to find out a Unix system library files are 32-bit or 64-bit (Linux)

- Java objects to garbage collection (Programming)

- Use XtraBackup to MySQL database online incremental backup and recovery (Database)

- Linux tool curl and wget advanced use (Linux)

- Classical sorting algorithm - Selection Sort (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 ("
- Oracle 11g principle study Dataguard (Database)
- Linux how to prohibit the use of Ping command (Linux)
- Elementary OS Freya global menu (Linux)
- How to experience Unity 8 Mir on Ubuntu 16.04 (Linux)
- How to configure HTTP Strict Transport Security (HSTS) (Linux)
- IBM Data Studio to create objects using ---- double quotes / sensitive issues and the table / column renaming (Database)
- RHEL 6.6 install GNOME desktop environment (Linux)
- The temporary operation of the document mkstemp on Linux (Linux)
- Vi editor Introduction (Linux)
- C ++ overloaded stream insertion operator and the stream extraction operator (Programming)
- Linux LVM space expansion Demo (Linux)
- Iptables principle (Linux)
- Go powerful development server simple example (Server)
- Impact test noatime Linux file access time (Linux)
- Convert MySQL date string to a NULL value exception handling (Database)
- expdp reported ORA-39181 Export Processing Method (Database)
- Usage logs Python library (Programming)
- What factors affect the performance of Java calls (Programming)
- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)
- Configuration based on open source Lucene Java development environment (Server)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.