How to search all files with specific text on Linux?

In daily development, we often encounter the situation of searching specific content.

How to search all files with specific content in Linux system?

In this case, Linux grep command and Linux find command will be used.

Use the grep command to search for specific content

➜ grep -Rn 'ylspirit' .

# OR

➜ grep -e 'ylspirit' -Rn .
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.

Search only for specific extensions

Search only for “. sh” extensions

➜ grep -e 'ylspirit' -Rn --include=\*.sh .
  • –include=GLOB
    Search only files whose base name matches GLOB

Use the find and grep to search for specified content

➜ find . -type f -exec grep -Hn 'ylspirit' {} \;

# OR

➜ find . -type f | xargs grep -Hn 'ylspirit'
  • -H print the file name for each match.

Search only for specific extensions

➜ find . -name "*.sh" -type f -exec grep -Hn 'ylspirit' {} \;

Add a Comment

Your email address will not be published.