How to search a directory for certain text in Linux



This tutorial will show you how to search through files in a directory that contain a specified text string.  I was using this command to figure out which file in a directory contained an IP address that I was looking for.
To search a directory for a specified text string and have it display the file names in which it finds the text:
$ grep -H "sample text" /home/sample/dir/*
The -H option will display the file name in which the text string was found. I have seen some Linux distributions where the -H option is not required to get the file name.
If you need to search through nested directories, you can use the -r option to search recursively:

$ grep -H -r "sample text" /home/sample/dir/*







Post a Comment

Previous Post Next Post