Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Grep how to find files based on file contents in UNIX     - Spring AOP for logging (Programming)

- How to install PlayOnLinux 4.2.5 under Ubuntu 14.04 / 12.04 (Linux)

- Install Firefox 32 official version of the Linux system (Linux)

- Oracle to start to solve the error ORA-27102 (Database)

- Copy and paste in Linux terminal and Vim (Linux)

- How to install with JSON support in Ubuntu 15.04 SQLite 3.9.1 (Database)

- Struts2 interceptor simulation (Programming)

- ORA-00824: can not set sga_target due to existing problem-solving (Database)

- Using Linux stat command to view the files (Linux)

- Use regular expressions to check whether the input box to enter a URL (Programming)

- Distributed File System using MogileFS (Linux)

- Ubuntu 13.04 configure MyEclipse 10.7 Environment (Linux)

- Xmanager Remote Desktop connection CentOS (Linux)

- Ubuntu font settings: Using Windows Font (Linux)

- Installation in lxml Python module (Linux)

- SSH without password Definitive Guide (Linux)

- [JavaScript] catch (ex) statements of ex (Programming)

- RHEL5 / 6 Installation Notes (Linux)

- Install Jetty on CentOS / RHEL 6.X (Server)

- Oracle 11g to create a second instance on Linux (Database)

 
         
  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:      
 
- Linux character device - a simple character device model (Linux)
- A well-known but rarely used skills: object pool (Programming)
- Use FirewallD build dynamic firewall (Linux)
- Linux awk text analysis tool (Linux)
- Linux, modify the hostname (Linux)
- How do I upgrade from Ubuntu 15.04 to Ubuntu 15.10 (Linux)
- Android timer use (Programming)
- Easy to get hidden administrator account (Linux)
- Security Knowledge: How to hide a backdoor PHP file tips (Linux)
- CentOS How to mount the hard drive (Linux)
- PL / SQL in forall simple test (Database)
- Java objects are taking up much space (Programming)
- Linux Command Line Art (Linux)
- DataGuard the MRP can not start to analyze and solve problems (Database)
- To control based on IP address routing policy under Linux (Linux)
- Linux System Getting Started Learning: Using yum to download the RPM package without installing (Linux)
- Btrfs file system creation and their characteristics in Linux (Linux)
- Object-oriented language Java some of the basic features (Programming)
- SELinux multi-level security (Linux)
- C language binary tree counts words (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.