Home  >  Article  >  Operation and Maintenance  >  How to find all files containing a string in Linux

How to find all files containing a string in Linux

不言
不言Original
2019-02-28 10:16:306219browse

How to find all files containing a string in How to find all files containing a string in Linux? This article will explain about recursively searching all files containing a specific text string. The "find" command is used in the article to search for strings in files. Alternatively, you can use the grep command to search for text. Let’s look at the specific content below.

How to find all files containing a string in Linux

First let’s look at the command

#find / -exec grep -l“string-to-search”{};

Let’s look at the specific usage

The following is an example command for searching/ Text 'redhat' in filesystem. This command will search for all files containing the string 'redhat' and the list file name as shown below.

# find / -exec grep -l“redhat”{}; 

/lib64/security/pam_oddjob_mkhomedir.so 
/lib64/libdevmapper.a.1.02 
/lib64/libdevmapper-event.a.1.02 
/lib64/libdevmapper.a 
/lib64/libdevmapper-event.a 
...

Search within files with a specific file extension. Like just searching for 'redhat' in all php files, use the following command.

# find / -name'* .php'-exec grep -l“redhat”{}; 
/var/www/projects/index.php 
/var/www/projects/new.php 
...

This article has ended here. For more other exciting content, you can pay attention to the relevant column tutorials on the php Chinese website! ! !

The above is the detailed content of How to find all files containing a string in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn