Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep to find a file that contains a string in a directory

Tags:

grep

bash

shell

I'm trying to get familiar with the basics of the terminal. I'd like to find the file within my CMS website that contains my Google Analytics tracking code "gaq" as a string to search should do the trick.

There is a folder on my desktop that contains all of the sites files.

/Users/myname/Desktop/website

I opened the terminal and tried

grep gaq /Users/myname/Desktop/website

grep gaq * /Users/myname/Desktop/website

I searched on SO and Google but the internet seems crowded out with slightly more advanced uses of grep involving regular expressions and conditions.

e.g.: Unix Command to List files containing string but *NOT* containing another string, How can I use grep to find a word inside a folder?.

I thought I'd found the answer with the second example question. I tried the following command: grep -nr gaq* /Users/myname/Desktop/website

But that returned many results and, from what I can see, not exactly accurate matches of my search string.

Here's a sample of the Google Analytics snippet, taken by viewing the source of the html page. My goal is to find the file that generates the analytics snippet in order to update it to the newer version of Google Analytics:

var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
            _gaq.push(['_trackPageview']);

So I am using "gaq" as a string to search for.

I realize that this must sound pretty basic but it's pretty frustrating as a beginner to the shell.

How would I search the directory /Users/myname/Desktop/website for the file (return the file not the actual paragraph of text) that contains the analytics code using grep (assuming grep is the command I should be using?)

like image 257
Doug Fir Avatar asked Jul 08 '13 15:07

Doug Fir


People also ask

How do I grep to find a string in a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do I use grep to find a word in a directory?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.


2 Answers

Two examples:

    grep -nr '/users/myname/Desktop/website' -e "gaq"
    grep -nrw '/users/myname/Desktop/website' -e "gaq"

What the options mean:

-n, --line-number Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)

-r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.

-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

like image 110
Mike Q Avatar answered Oct 24 '22 06:10

Mike Q


Source: man grep

There's an option for --files-with-matches or -l

grep -Rl  gaq /Users/myname/Desktop/website
like image 24
dougEfresh Avatar answered Oct 24 '22 04:10

dougEfresh