grep找到一个包含目录中的字符串的文件 [英] Grep to find a file that contains a string in a directory

查看:120
本文介绍了grep找到一个包含目录中的字符串的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图熟悉终端的基础知识。我希望在我的CMS网站中找到包含我的Google Analytics(分析)跟踪代码gaq的文件,作为搜索字符串应该有效。

我的桌面上包含所有网站文件的文件夹。


/ Users / myname / Desktop / website

我打开了终端,并尝试了

  grep gaq / Users / myname / Desktop / website 

grep gaq * / Users / myname / Desktop / website

我搜索了SO和Google,似乎挤满了涉及正则表达式和条件的grep的稍微更高级的用法。

eg:
,<一个包含另一个字符串的非*命令href =https://stackoverflow.com/questions/4121803/how-can-i-use-grep-to-find-a-word-inside-a-folder>我如何使用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是我应该使用的命令?)

man 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆