Reputation: 659
How do I get unix to search inside ALL files for a the string "hello"?
I've tried the following but it doesn't work:
sudo grep "hello" | find / -name "*" 2>/dev/null
Does anyone have any other ideas?
Thank you.
Upvotes: 8
Views: 49256
Reputation: 26086
Use
grep -r 'pattern' /
If your grep
supports -r
, (GNU grep
does); if not use
find / -type f -exec grep 'pattern' {} +
If your find
supports -exec
with +
, otherwise use
find / -type f -printf '%p\0' | xargs -0 grep 'pattern'
If your find
supports -printf
and your xargs
supports -0
, or
find / -type f -print0 | xargs -0 grep 'pattern'
If your find
supports only -print0
and your xargs
supports -0
. In all other cases fall back on
find / -type f | xargs grep 'pattern'
This is maximally compatible, with the caveat that certain unusual file names will fail to be grepped and crafted file names could pose a security risk.
Note that you will have to be root in order to be sure of searching all files and that the search will be case-sensitive unless you add -i
to grep
.
Upvotes: 12
Reputation: 70931
Maybe this one?
sudo cd / && grep -rn "hello" *
EDIT: the 'n' option of course is not needed - it simply displays the line numbers and I find it nice. The 'r' option tells grep to perform recursive search within directories.
Upvotes: 28