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?)
Source:
man grep
There's an option for
--files-with-matches
or-l
Two examples:
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.