Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Grep how to find files based on file contents in UNIX     - KUbuntu / Ubuntu 14.04 (downgrade) installed SVN 1.7 (Linux)

- Oracle restrict certain IP, the malicious user actions on important table (Database)

- To compile and install Redis Linux and master-slave replication configuration (Database)

- SHELL script to use anti SSH brute force and vsftpd (Linux)

- Linux system server network security management tips (Linux)

- Install Websphere MB required system rpm package under Linux (Linux)

- VirtualBox modify the size of the virtual machine disk VDI (Linux)

- About Java 7 module system (Programming)

- To install Redis under Linux (Database)

- Analysis of Java reflection mechanism (Programming)

- Upgrade installation manual CentOS6.5 GCC4.8.2 (Linux)

- Machine Learning: Classification of the curse of dimensionality (Programming)

- Linux common commands: nslookup, ls md5sum, uname, history, etc. (Linux)

- Using Vagrant to build multi-platform environment (Server)

- Oracle lag () and lpad () function (Database)

- Linux more efficient than select a model epoll (Linux)

- Android 5.1 OTA package compilation error (Programming)

- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)

- How to install CentOS 7.x in OpenERP (Odoo) (Linux)

- Applications in Objective-C runtime mechanism (Programming)

  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 ("
- DRBD switchover (Server)
- Installation of network monitoring ntopng under CentOS 6.4 (Linux)
- Four Methods of Self - Learning Linux (Linux)
- Python function arguments * args and ** kwargs usage (Programming)
- MySQL 5.6 master-slave replication configuration (Database)
- Swift notes - let you two hours to learn Swift (Programming)
- To disable the function by pressing Ctrl-Alt-Del to restart the system in RHEL / CentOS 5/6 (Linux)
- Linux systems dmesg command processing failures and system information collected seven usage (Linux)
- iscsiadm command usage (Linux)
- Java interface (Programming)
- About Python default character set (Linux)
- CentOS 6.5_x64 install Oracle 11g R2 (Database)
- Repair CentOS 6.4 Grub boot (Linux)
- To install the iNode client on UbuntuKylin 13.10 (Linux)
- Help you make Git Bisect (Linux)
- How to install the Linux text editor Atom 0.124.0 (Linux)
- Create several practical points of high security PHP site (Linux)
- Mybatis + binding Struts2: achieving user to insert and find (Programming)
- Linux, MySQL root privilege escalation another method (Linux)
- Implement Oracle dynamic registration of non-standard port 1521 (Database)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.