How to Find all Files Containing a String in Linux

Channel: Linux
Abstract: /lib64/libdevmapper-event.a.1.02Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files conta

How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses 「find」 command to search string in files. Alternatively, You can also use grep command to search text.

Command:-
# find / -exec grep -l 「string-to-search」 {} ;
Uses:

Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files containing string ‘redhat’ and list file names only like 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
...

To Search in specific file extension files. Like search ‘redhat’ in all php files only, use following command.

# find / -name '*.php' -exec grep -l "redhat" {} ; 

/var/www/projects/index.php
/var/www/projects/new.php
...

Ref From: tecadmin
Channels: searchfindfiles

Related articles