Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do the following:

cat *.xml | grep some_string_here

This tells me if a particular string exists in an xml file in a directory. Unfortunately it does not tell me which file.

how can I do this better? cat command does not see me to have an switch that adds a filename prefix to the output...

share|improve this question
Migrate to unix se site? – mattdm Jan 15 '11 at 17:32
up vote 7 down vote accepted

cat is unnecessary (UUOC!) grep will normally tell you which file the matched line was found in when used like this:

   grep some_string_here *.xml

You can also use the -H switch to always to this:

   grep -H some_string_here *.xml
share|improve this answer
Thanks for bringing UUOC to my attention, I feel like a right noob :-) – DutchUncle Jan 15 '11 at 17:41
LOL, I'd give you an extra +1 if I could for "UUOC". – JakeRobinson Jan 16 '11 at 0:09

You need to do:

grep some_string_here *.xml

instead, then grep will automatically prepend the filename to each match.

share|improve this answer

Just use grep

grep some_string *.xml

The output will be something like

a.xml: string containing some_string
xyzzy.xml:some_string in a different line

If a file contains more than one occurrence of some_string each occurrence will be printed. If you use

grep -l some_string *.xml

only the filename will be printed.

share|improve this answer
-i is "ignore case" in the versions of grep that I know about. Some versions use -m to stop after one match. – Dennis Williamson Jan 15 '11 at 18:00
grep some_string_here /dev/null *.xml

... is a small improvement over the other suggestions above, since it makes sure that the name of the file containing the match is always printed, even if the current directory only contains one XML file.

share|improve this answer

Your Answer


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.