I've got a number of .conf files in the following "lxadmin" directory and suspect that the text "tanguay.info" is in one of them somewhere.

I do this command:

cat /etc/httpd/conf/lxadmin/* | grep "tanguay.info"

And it tells me:

cat: /etc/httpd/conf/lxadmin/forward: Is a directory
Include /home/httpd/tanguay.info/conf/lxadmin.tanguay.info

So I know that it is in there somewhere, but it doesn't tell me in which file.

How can I get it to also tell me in which file the text is located?

link|improve this question

55% accept rate
3  
This is a classic useless usage of cat. In 99% of cases you do not need to use cat, as the command you're piping to will take a filename or a list of files. partmaps.org/era/unix/award.html will show you some examples of how to write your commands better. – David Pashley Jul 5 '09 at 8:14
feedback

9 Answers

up vote 13 down vote accepted

grep "tanguay.info" /etc/httpd/conf/lxadmin/*, as long as the file is not in a subdirectory.

link|improve this answer
feedback

If there are subdirectories you can use

rgrep "tanguay.info" /etc/httpd/conf/lxadmin/

or

grep -r "tanguay.info" /etc/httpd/conf/lxadmin/

EDIT:

You can also use ack, an enhanced grep written in perl (no deps required on standalone version). ack searches recursively through directories by default.

ack "tanguay.info" /etc/httpd/conf/lxadmin/

On Ubuntu, you can find it in the ack-grep package.

link|improve this answer
1  
If grep is given more than one file to search, or you use the recursive search, it will print out the name of matching file at the start of the line. – David Pashley Jul 5 '09 at 8:11
+1 for recommending Ack. After all, it's betterthangrep.com – Telemachus Jul 5 '09 at 21:34
feedback

What you are looking for is option -H of grep which shows the matching filename together with the match. You can also add -r to search a directory recursively. I.e.

$ grep -r -H "tanguay.info" /etc/httpd/conf/lxadmin/
link|improve this answer
feedback

find

find . | xargs grep 'string' -sl

The -s is for summary and won't display warning messages such as grep: ./directory-name: Is a directory

The -l is for list, so we get just the filename and not all instances of the match displayed in the results.

link|improve this answer
4  
-1 First of all, never use find and xargs without using -print0 with find and -0 with xargs, or files with spaces will cause you problems. Secondly, you've just reimplemented "grep -r", badly. Thirdly, -s is "suppress error messages", not "summary" – David Pashley Jul 5 '09 at 8:09
feedback
# cd /directory/of/files/
# grep -A 5 -B 5 'keywords' *

show 5 lines before and 3 lines after so that you can see the context of the keywords

makes all the difference in understanding how the keywords are used in the file

link|improve this answer
feedback
grep -Rl "tanguay.info" /etc/httpd/conf/lxadmin/
link|improve this answer
feedback

On windows systems:

type *.* | find "<string>"

The quotes must be in there, by the way.

link|improve this answer
feedback

You can use backquote to find only in files. [ ` -> key left to Number 1 key above to Tab key]

grep -wn "tanguay.info" find /etc/httpd/conf/lxadmin/ -iname "*.conf"

The above code will first get the *.conf files and then search the "tanguay.info" among the result.

Run the above command in a super user login to avoid permission problems.

link|improve this answer
feedback

The solition below is very similar to Izzy's response above but offers a little more flexibility. I find it very useful when I need to do a little more than xargs will allow (especially on older SunOS systems where grep -r or rgrep is not available)

find . -name "*.conf" -exec grep -l tanguay.info {} \;

Quite simply, you are finding all the *.conf files and executing a grep on each result that is found. The \; completes the -exec portion of the command.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.