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"
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityDid you just mean to grep
the file(s) instead of (uselessly) cat
ing 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
\|
(without the -E
option) or by |
(with the -E
option), that is grep 'foo\|bar'
or grep -E 'foo|bar'
.