【问题标题】:Write a shell script that find-greps files and outputs only those filenames编写一个查找 greps 文件并仅输出这些文件名的 shell 脚本
【发布时间】:2012-10-31 21:33:03
【问题描述】:

我想搜索特定文件夹和内容中的文件,然后我只想打印找到的文件的名称。 我有一个命令:

for file in $(find from | xargs grep 'move')
do 
   echo $file
done

它打印例如:

from/1.txt:move
from/2.txt:some text
move
from/3.txt:move text

但我想要:

from/1.txt
from/2.txt
from/3.txt

我尝试使用以下方法删除不必要的部分:

${file%:*}

这给出了结果:

from/1.txt
from/2.txt
move
from/3.txt

那个“移动”就剩下了。

【问题讨论】:

    标签: bash shell grep find


    【解决方案1】:

    Grep 有一个递归以及一个“仅列出文件名”选项,所以这应该可以工作:

    grep -r -l "move" from
    

    【讨论】:

      【解决方案2】:

      使用选项-lgrep,即

      for file in $(find from | xargs grep -l 'move')
      do 
         echo $file
      done
      

      甚至更好:

      for file in $(find from -type f -print0 | xargs -r0 grep -l 'move')
      do 
         echo $file
      done
      

      【讨论】:

        猜你喜欢
        • 2010-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-13
        • 2021-04-24
        • 1970-01-01
        • 2017-11-19
        • 2011-07-06
        相关资源
        最近更新 更多