find . -name "*.php" | xargs grep -i -n "searchstring" >output.txt
这里我试图将数据写入一个没有发生的文件......
答案 0 :(得分:10)
如何使用>>
附加结果?
find . -name "*.php" | xargs grep -i -n "searchstring" >> output.txt
我现在还没有带有Linux的盒子,所以我会尝试改进。
xargs grep -i -n "searchstring"
让我感到困扰。
也许您的意思是xargs -I {} grep -i "searchstring" {}
,或只是xargs grep -i "searchstring"
?
由于-n
作为grep的参数只给你数字行,我怀疑这是你需要的。
这样,您的最终代码将是
find . -name "*.php" | xargs grep -i "searchstring" >> output.txt
答案 1 :(得分:2)
find . -name "*.php" -exec grep -i -n "function" {} \; >output.txt
但你不会知道它来自哪个文件。你可能想要:
find . -name "*.php" -exec grep -i -Hn "function" {} \; >output.txt
代替。
答案 2 :(得分:1)
我猜你在php文件名中有空格。如果按照您的方式将它们传递给grep
到xargs
,名称将被拆分为多个部分,grep
将这些部分解释为文件名,然后找不到。
有一个解决方案。 find
有一个-print0
选项,指示find
按NUL字节分隔结果,xargs
有一个-0
选项,指示xargs
期望一个NUL字节作为分隔符。使用你得到的那些:
find . -name "*.php" -print0 | xargs -0 grep -i -n "searchstring" > output.txt
答案 3 :(得分:1)
尝试使用line-buffered
grep --line-buffered
[编辑]
我在我的盒子上运行你的原始命令,似乎工作正常,所以我不确定了。
答案 4 :(得分:0)
对我来说很好看。如果您删除>output.txt
会怎样?
答案 5 :(得分:0)
如果您要搜索源代码树,请考虑使用ack。要做你在ack中所做的事情,无论文件名中是否有空格,你都可以:
ack --php -i searchstring > output.txt
答案 6 :(得分:0)
我总是使用以下命令。它在控制台上显示输出并创建文件
grep -r"要搜索的字符串" 。 2>& 1 | tee /your/path/to/file/filename.txt
答案 7 :(得分:0)
通过
检查可用磁盘空间<%= link_to content_path(id: content.id) do %>
磁盘上的可用空间不足。