bash grep 在目录中查找包含字符串的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17530617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep to find a file that contains a string in a directory
提问by Doug Fir
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.
我正在努力熟悉终端的基础知识。我想在我的 CMS 网站中找到包含我的 Google Analytics 跟踪代码“gaq”作为搜索字符串的文件应该可以解决问题。
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.
我在 SO 和 Google 上搜索过,但互联网似乎挤满了涉及正则表达式和条件的 grep 稍微更高级的用法。
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?.
例如: 列出包含字符串但 *NOT* 包含另一个字符串的文件的 Unix 命令,如何使用 grep 在文件夹中查找单词?.
I thought I'd found the answer with the second example question. I tried the following command: grep -nr gaq* /Users/myname/Desktop/website
我以为我已经在第二个示例问题中找到了答案。我尝试了以下命令: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:
这是通过查看 html 页面源获取的 Google Analytics 代码段示例。我的目标是找到生成分析代码片段的文件,以便将其更新到较新版本的 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.
所以我使用“gaq”作为搜索字符串。
I realize that this must sound pretty basic but it's pretty frustrating as a beginner to the shell.
我意识到这听起来一定很基本,但作为 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?)
我将如何使用 grep 在目录 /Users/myname/Desktop/website 中搜索包含分析代码的文件(返回文件而不是实际的文本段落)(假设 grep 是我应该使用的命令?)
采纳答案by dougEfresh
Source: man grep
来源: man grep
There's an option for --files-with-matchesor -l
有一个选项--files-with-matches或-l
grep -Rl gaq /Users/myname/Desktop/website
回答by Mike Q
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.)
-n, --line-number 在其输入文件中使用从 1 开始的行号为每行输出添加前缀。(-n 由 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.
-r, --recursive 以递归方式读取每个目录下的所有文件,仅当它们在命令行上时才遵循符号链接。这等效于 -d 递归选项。
-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.
-w, --word-regexp 仅选择包含构成整个单词的匹配项的那些行。测试是匹配的子字符串必须位于行首或前面是非单词组成字符。同样,它必须位于行尾或后跟非单词组成字符。单词组成字符是字母、数字和下划线。

