【问题标题】:Identify all files or folder which contains a specific keyword [duplicate]识别包含特定关键字的所有文件或文件夹[重复]
【发布时间】:2019-11-08 05:58:03
【问题描述】:

使用脚本需要识别给定路径下包含特定关键字的所有文件或文件夹。该路径将有多个文件夹。 下面是使用的脚本。

   #!/bin/sh
   DIR=''
   for FILE in ls "$DIR"*
   do
           # echo $FILE
               grep -l $FILE "*FY*"
    done
~

【问题讨论】:

  • 闻起来像作业。
  • #!/bin/sh DIR='' for FILE in ls "$DIR"* do # echo $FILE grep -l $FILE "FY" done ~
  • 以上是我正在使用但不工作的脚本。在这方面需要帮助。
  • @lakshmisowmya 我想你的老师要么希望你使用递归函数,要么使用find,这取决于你到目前为止所学的内容。
  • @lakshmisowmya Did You Know™ 你可以edit 将你的代码包含在问题中,而不是将其发布在难以阅读的评论中?

标签: shell sh


【解决方案1】:

您必须在要搜索的路径之前给出“关键字”。在您的脚本中并非如此。

  grep -l -r "keyword" path/to/folder

试试上面的脚本。

-l, --files-with-matches
          Suppress normal output; instead print the  name  of  each  input
          file  from  which  output would normally have been printed.  The
          scanning will stop on the first  match.   (-l  is  specified  by
          POSIX.)

-R, -r, --recursive
          Read all  files  under  each  directory,  recursively;  this  is
          equivalent to the -d recurse option.

更多信息refer

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    相关资源
    最近更新 更多