Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use grep to find a word inside a folder?

In Windows, I would have done a search for finding a word inside a folder. Similarly, I want to know if a specific word occurs inside a directory containing many sub-directories and files. My searches for grep syntax shows I must specify the filename, i.e. grep string filename.

Now, I do not know the filename, so what do I do? A friend suggested to do grep -nr string, but I don't know what this means and I got no results with it (there is no response until I issue a Ctrl + C).

like image 219
kiki Avatar asked Nov 08 '10 06:11

kiki


People also ask

How do I search for a word in a directory in Linux?

Where the -R option tells grep to read all files under each directory, recursively, following symbolic links only if they are on the command line and option -w instructs it to select only those lines containing matches that form whole words, and -e is used to specify the string (pattern) to be searched.

How do I find a specific string or word in files and directories?

You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.

How do I grep recursively in a directory?

To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

Does grep look inside files?

You can use the grep command to search through files for a particular pattern. The name grep comes from the ed(C) command g/re/p, which means ``globally search for a regular expression and print it''. The grep command looks through the files you specify for lines containing the regular expression you tell it to find.


1 Answers

grep -nr 'yourString*' . 

The dot at the end searches the current directory. Meaning for each parameter:

-n            Show relative line number in the file 'yourString*' String for search, followed by a wildcard character -r            Recursively search subdirectories listed .             Directory for search (current directory) 

grep -nr 'MobileAppSer*' . (Would find MobileAppServlet.java or MobileAppServlet.class or MobileAppServlet.txt; 'MobileAppASer*.*' is another way to do the same thing.)

To check more parameters use man grep command.

like image 196
Manish Ranjan Avatar answered Oct 16 '22 15:10

Manish Ranjan