查找包含给定文本的文件

Owe*_*wen 148 bash find

在bash中我想为.php|.html|.js包含不区分大小写的字符串的每个类型的文件返回文件名(以及文件的路径)"document.cookie" | "setcookie"

我该怎么办?

bea*_*4rw 202

egrep -ir --include=*.{php,html,js} "(document.cookie|setcookie)" .
Run Code Online (Sandbox Code Playgroud)

如果您只想要文件名,请添加r(小写i)标志:

egrep -lir --include=*.{php,html,js} "(document.cookie|setcookie)" .
Run Code Online (Sandbox Code Playgroud)

  • 您忘了添加搜索路径.路径是'.' 在上面的例子中.在您的情况下,脚本正在等待输入搜索stdin.尝试:egrep -lir --include =*"repo"/(或任何其他路径) (13认同)
  • 这似乎对我不起作用(至少不是在 mac 上)......只是挂起...... egrep -lir --include=* "repo" egrep: 警告:标准输入的递归搜索 (2认同)
  • `grep -E ... ` > `egrep ...` (2认同)
  • 为了让这个工作,我不得不跳过 * 和 \。所以我有`--include=\*.{php,html,js}` (2认同)

Rao*_*oul 49

尝试类似的东西 grep -r -n -i --include="*.html *.php *.js" searchstrinhere .

-i使得情况insensitlve

.末意味着你想从你的当前目录开始,这可能是与任何目录来代替.

这些-r方法在目录树下递归执行

-n打印匹配项的行号.

--include让你添加的文件名,扩展名.接受通配符

有关详细信息,请参阅:http://www.gnu.org/software/grep/

  • 或者使用`-l`选项(只打印匹配的文件名)而不是`-n` (4认同)

Mic*_*ski 15

find他们和grep字符串:

这将在/ starting/path中找到3种类型的所有文件,在正则表达式中找到grep '(document\.cookie|setcookie)'.使用反斜杠分割2行以方便阅读......

find /starting/path -type f -name "*.php" -o -name "*.html" -o -name "*.js" | \
 xargs egrep -i '(document\.cookie|setcookie)'
Run Code Online (Sandbox Code Playgroud)


Fre*_*ihl 9

听起来像是一个完美的工作,grep或者可能是ack

或者这个美妙的建筑:

find . -type f \( -name *.php -o -name *.html -o -name *.js \) -exec grep "document.cookie\|setcookie" /dev/null {} \;
Run Code Online (Sandbox Code Playgroud)


nos*_*nos 5

find . -type f -name '*php' -o -name '*js' -o -name '*html' |\
xargs grep -liE 'document\.cookie|setcookie'
Run Code Online (Sandbox Code Playgroud)