How to Search for a String Inside Files via Terminal Print

  • grep
  • 4

 

How to Search for a String Inside Files via Terminal

If you need to locate a specific piece of text but don’t know exactly where it’s stored, you can use the grep command from a Linux terminal. This is a powerful tool that performs fast and recursive searches across directories.

 

Command to use

grep -rnw ./ -e "string"

 

Explanation of parameters

  • grep: command used to search for text inside files
  • -r: performs a recursive search through subdirectories
  • -n: shows the line number where the string is found
  • -w: matches exact words only (avoids partial matches)
  • ./: sets the current directory as the starting point
  • -e "string": defines the keyword or phrase to search

 

Practical example

If you want to search for the keyword database_password inside all files in your project directory, use the following command:

grep -rnw ./ -e "database_password"

The system will list all matching files, along with the line number and full file path where the string was found.

 


Was this answer helpful?

« Back