Site icon LinuxCommands.site

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

Syntax

grep -rnw '/path/to/somewhere/' -e 'pattern'

Example

➜  ~ grep -rnwl ~/Documents/blog -e "linuxcommand"
/Users/ylspirit/Documents/blog/awk/grep/test1.txt
/Users/ylspirit/Documents/blog/grep/test1.txt
➜  ~
➜  ~ grep -rnw ~/Documents/blog -e "linuxcommand"
/Users/ylspirit/Documents/blog/awk/grep/test1.txt:8:linuxcommand
/Users/ylspirit/Documents/blog/grep/test1.txt:8:linuxcommand

use grep command and find command

Syntax

find '/path/to/somewhere/' -type f | xargs grep -hl 'pattern'

Example

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

All right.

Exit mobile version