This article will help you how to recursively dig into directories to find and list all files that include a given string of text.


An easy way to work this out is by practising grep pattern searching tool, which is a powerful, productive, secure, and most popular command-line utility for identifying patterns and words from files or directories on Unix-like systems.


The following command will list all files including a line with the text “check_root”, by repeatedly and aggressively searching the ~/bin directory.


$ grep -Rw ~/bin/ -e 'check_root'


Where the -R option tells grep to upload all files under each directory, -w option helps to select just those lines including events that form whole words and -e option used to specify the string that searched.


When searching specific directories or files that demand root access, you should use sudo command if you are running your system without the root account.


$ sudo grep -Rw / -e 'check_root'

You can apply -i option to ignore case distinctions as shown:


$ grep -Riw ~/bin/ -e 'check_root'


Apply the -n option if you need to get the exact line where the string of text exists.


$ grep -Rinw ~/bin/ -e 'check_root'


If there are different types of files in a directory you wish to search in; you can also specify the type of files to search for instance, by adding --include option.
This pattern instructs grep to only looking for all .sh files.


$ grep -Rnw --include=\*.sh ~/bin/ -e 'check_root'


Moreover, it is possible to search for more than one pattern, applying the below command.


$ grep -Rinw ~/bin/ -e 'check_root' -e 'netstat'

 

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)