A simple way to work this out is by using grep pattern searching tool, is a powerful, efficient, reliable and most popular command-line utility for finding patterns and words from files or directories on Unix-like systems.

 

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

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

 

Where the -R option tells grep to read all files under each directory, recursively, following symbolic links only if they are on the command line and option -w instructs it to select only those lines containing matches that form whole words, and -e is used to specify the string (pattern) to be searched.

 

You should use the sudo command when searching certain directories or files that require root permissions (unless you are managing your system with the root account). 

$ sudo grep -Rw / -e 'check_root'

 

To ignore case distinctions employ the -i option as shown:

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

 

If you want to know the exact line where the string of text exists, include the -n option.

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

 

Assuming there are several types of files in a directory you wish to search in, you can also specify the type of files to be searched for instance, by their extension using the --include option.

 

This example instructs grep to only look through all .sh files.

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

 

In addition, it is possible to search for more than one pattern, using the following command.

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

 

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