Finding a string in text files
- Details
- Category: Newbies in Command
One of the great things with Linux is that almost all files are out there in the open. Configuration of the OS, application config files, php files of your webserver, etc. There's only one problem: there's way too many files to find what you're looking for. Now what?
Easy: search for them. So: we'll show you how to find a file containing a particular text string.
All you need is the grep command. It searches the given input FILE(s) for lines containing a match or a text string.
grep "text string to search" directory-path
Examples
For example search for a string called "bubba" in all text files located in /home/bubba/*.txt directory, use
$ grep "bubba" /home/bubba/*.txt
You can search for a text string all files under each directory, recursively with the -r option:
$ grep -r "bubba" /home/bubba
By default, grep command prints the matching lines. You can use the -H option to also print the filename for each match.
$ grep -H -r "bubba" /home/bubba
To just print the filename use cut command as follows:
$ grep -H "bubba" /home/bubba -R | cut -d: -f1