Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
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
add comment

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.

share|improve this answer
add comment

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.

share|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
add comment

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/
share|improve this answer
add comment

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.

share|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
add comment
# 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

share|improve this answer
add comment
grep -Rl "tanguay.info" /etc/httpd/conf/lxadmin/
share|improve this answer
add comment

On windows systems:

type *.* | find "<string>"

The quotes must be in there, by the way.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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