Search and Grep for text in unix/linux

We will see how to search for files and then grep for a string of text in those files. First i will use the find command in unix or linux to search for the regular files in the current directory. The grep command to search for the normal files in the current directory is shown below:

> find . -type f
./docs/sum.pl
./add.sh
./sample

Now we will grep for a particular word in these files and display only the filenames that has the matching word. The unix command is shown below:

> find . -type f -exec grep -l word {} \;

The above command just displays the filenames that has the specified word. Now we will try to display the lines from the files that have the matching word. The unix command for this is:

> find . -type f -exec grep -l word {} \; -exec grep word {} \;

If you want to put space between the results of the above command, display the line using echo. The complete unix command is