How do I find all files containing specific text on Linux?
(...)
I came across this solution twice:
find / -type f -exec grep -H 'text-to-find-here' {} \;
If using find like in your example, better add -s
(--no-messages
) to grep
, and 2>/dev/null
at the end of the command to avoid lots of Permission denied messages issued by grep
and find
:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
find is the standard tool for searching files - combined with grep when looking for specific text - on Unix-like platforms. The find command is often combined with xargs, by the way.
Faster and easier tools exist for the same purpose - see below. Better try them, provided they're available on your platform, of course:
Faster and easier alternatives
RipGrep - fastest search tool around:
rg 'text-to-find-here' / -l
The Silver Searcher:
ag 'text-to-find-here' / -l
ack:
ack 'text-to-find-here' / -l
Note: You can add 2>/dev/null
to these commands as well, to hide many error messages.
Warning: unless you really can't avoid it, don't search from '/' (the root directory) to avoid a long and inefficient search!
So in the examples above, you'd better replace '/' by a sub-directory name, e.g. "/home" depending where you actually want to search...