How to find all files containing specific text on Linux?

In linux, to find all files containing a specific text, we can use the grep command, which can search for the specified text in the file.

In the following example, we will introduce two methods to find all files containing the specified text.

use grep command


grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search


➜  ~ grep -rnwl ~/Documents/blog -e "linuxcommand"
➜  ~
➜  ~ grep -rnw ~/Documents/blog -e "linuxcommand"

use grep command and find command


find '/path/to/somewhere/' -type f | xargs grep -hl 'pattern'
  • find -type f is find files only.


➜  ~ find ~/Documents/blog -type f | xargs grep -hl 'linuxcommand'

All right.

Add a Comment

Your email address will not be published. Required fields are marked *