grep找到一个包含目录中的字符串的文件 [英] Grep to find a file that contains a string in a directory
问题描述
我的桌面上包含所有网站文件的文件夹。
/ Users / myname / Desktop / website
我打开了终端,并尝试了
grep gaq / Users / myname / Desktop / website
grep gaq * / Users / myname / Desktop / website
我搜索了SO和Google,似乎挤满了涉及正则表达式和条件的grep的稍微更高级的用法。
我想我已经找到了第二个问题的答案。我尝试了以下命令: grep -nr gaq * / Users / myname / Desktop / website
很多结果,从我所看到的,我的搜索字符串不完全匹配。
以下是一个Google Analytics代码片段示例,通过查看html页面。我的目标是找到生成分析代码段的文件,以便将其更新为新版Google Analytics:
var _gaq = _gaq || [];
_gaq.push(['_ setAccount','UA-xxxxxxxx-1']);
_gaq.push(['_ trackPageview']);
所以我使用gaq作为字符串来搜索。
我意识到这听起来很基本,但作为初学shell的人非常沮丧。
如何搜索目录/ Users / myname / Desktop / website用于使用grep包含分析代码的文件(返回文件不是文本的实际段落)(假设grep是我应该使用的命令?)
有一个选项可用于:
- 带有匹配的文件
或 -l </ code>
grep -Rl gaq / Users / myname / Desktop / website
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
grep -Rl gaq /Users/myname/Desktop/website
这篇关于grep找到一个包含目录中的字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!