Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"grep -rnw": search for a string in all files

Tags:

Related question: How do I find all files containing specific text on Linux?

I have been using the command mentioned in the answer of above question to search for string occurences in all files:

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

However lately I encountered a problem, shown in the following picture: enter image description here

Looks like this command only recognizes strings that stand out as a word or something. How should I modify the command to improve my search result?

like image 201
shenkwen Avatar asked Aug 09 '16 19:08

shenkwen


People also ask

How do I search for a string in multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do you search a string in all files in a folder?

Select Search > Find in Files from the menu. If you like keyboard shortcuts better, use Ctrl-Shift-F to open the search window instead. The find in files configuration window is pretty easy to use as you can ignore most options if you don't require them.

How do you search for a string in all files in Linux?

Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.


1 Answers

explainshell helpfully explains your command, and gives an excerpt from man grep:

   -w, --word-regexp           Select  only  those  lines  containing matches that form whole words.  

So just remove -w since that explicitly does what you don't want:

grep -rn '/path/to/somewhere/' -e "pattern" 
like image 171
that other guy Avatar answered Sep 21 '22 13:09

that other guy