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"

1 Answer 1


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
  • 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, 2013 at 15:03
  • ack is an alternative tool that is great for this sort of thing
    – Ciclamino
    Apr 30, 2013 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .