Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I searched everywhere but didn't find out how to return a file content that matches given words or a regex.

cat /tmp/*.txt | grep "strings"
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Did you just mean to grep the file(s) instead of (uselessly) cating it/them to grep?

grep strings /tmp/*.txt

What cat does is that it combines all *.txt files into one big "file". If you cat multiple files, grep therefore will only see one input without a real name—it's all your files combined, fed through the pipe. This won't get you the file names, since you basically made everything one big file.

If you only want grep to return the files that match, use the -l option:

grep -l strings /tmp/*.txt
share|improve this answer
    
thanks, my english is a little bit approximative. –  Julio Fong Apr 30 '13 at 14:49
    
If you want to search for string_s_ (more than one), separate them either by \| (without the -E option) or by | (with the -E option), that is grep 'foo\|bar' or grep -E 'foo|bar'. –  Uwe Apr 30 '13 at 15:03
    
ack is an alternative tool that is great for this sort of thing –  Ciclamino Apr 30 '13 at 17:24
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.