Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Grep how to find files based on file contents in UNIX     - How to install Ubuntu applications Device 7 (Linux)

- CentOS How to mount the hard drive (Linux)

- Nginx is used to build the cache module srcache_nginx (Server)

- Linux character device - automatically creates the device nodes and devices (Linux)

- After Oracle 11g dataguard failover rebuild the archive logs are not applied to be NO problem (Database)

- MySQL event table to achieve timing build a small note (Database)

- The AWK use Cygwin tools mysql binlog log viewer (Database)

- Ubuntu 12.04 64-bit installation Redmine + Git + ReviewBoard (Linux)

- MySQL Tutorial: About checkpoint mechanism (Database)

- LNMP Note: Addressing mail function can not send mail (Server)

- Linux screen commonly commands (Linux)

- CentOS 7.0 Enable iptables firewall (Linux)

- SecureCRT in Python scripting study guide (Programming)

- RHEL6.4 x86_64 build SVN service (Server)

- Java how to achieve bubble sort the problem Arraylist (Programming)

- Linux kernel IPv6 protocol closed manner (Linux)

- Several back door and log tool under Linux (Linux)

- Simple configuration shell scripts virtual machine environment experiment (Linux)

- Android Custom View password box example (Programming)

- Installation salt-minion on RHEL5 (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 ("
     
         
         
         
  More:      
 
- Using packet capture libpcap be reconciliation package in Ubuntu 14.04 64 bits (Linux)
- Oracle database import and export (Database)
- Kernel compile under Debian (Linux)
- Oracle () trunc function usage (Database)
- Python class of operator overloading (Programming)
- Linux and Windows virtual machine shared folders ---- Linux install VMware tools (Linux)
- expdp / impdp use version parameter data migration across versions (Database)
- Linux AS4 VPN server in conjunction with a firewall perfect (Linux)
- OpenJDK 7 compiled under Ubuntu 14.04.3 64-bit (Linux)
- Network Security Basics Linux command (Linux)
- Oracle DataGuard principles and basic configuration (Database)
- Kali Linux 2.0 U disk installation errors Your installation cd-rom could not be mounted (Linux)
- Check with Hello World Docker installation (Server)
- How to download video youtube-dl in Linux (Linux)
- A custom implementation of the Android sidebar (Programming)
- CentOS 6.6 source compiler GCC upgrade to 4.8.2 (Linux)
- Vmstat command Linux Performance Monitoring (Linux)
- To use Android RecyclerView (Programming)
- How to install and configure a VNC server on CentOS 7.0 (Server)
- OpenWrt modify flash size (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.