【问题标题】:find the folder along with the file names which has my string in it?找到包含我的字符串的文件夹以及文件名?
【发布时间】:2015-04-13 21:25:14
【问题描述】:

我需要找到文件夹名称以及内容中包含我的字符串的文件名。我在这个目录“/data/queue/data”中,我在同一个目录中有很多文件夹,在每个文件夹中,我有各种文件。

我想运行一个命令,它可以找到我的文件夹名称和文件名,它们的内容中有一个特定的字符串,比如“badezimmer”。最快的方法是什么?

以下是此目录“/data/queue/data”中的文件夹,每个文件夹中都有不同的文件。

david@machineA:/data/queue/data$ ls -lrth
drwxr-xr-x 2 david david  12K Apr 11 18:58 1428800400
drwxr-xr-x 2 david david  12K Apr 11 19:58 1428804000
drwxr-xr-x 2 david david  12K Apr 11 20:58 1428807600

我只想从这个目录运行一个命令 - “/data/queue/data”,如果它们的内容中有我的字符串,它可以打印我的文件夹名称和文件名。

david@machineA:/data/queue/data$ some command to find the folder and files which has my string.

【问题讨论】:

  • 你的意思是像find /data/queue/data -name *badezimmer*?或者您是在谈论在其内容中包含字符串 的文件?
  • 我的意思是在其内容中包含字符串的文件。很抱歉造成混乱
  • 那么一个简单的grep -rl badezimmer /data/queue/data 就可以了。
  • 我明白了。假设如果是两个词,那么我可以使用grep -rl "A B" /data/queue/data。对吗?
  • 是的,没错。

标签: linux search text grep find


【解决方案1】:

如前所述,以下内容将满足您的需求:

grep -rl 'badezimmer' /data/queue/data

来自grep(1) 的手册页:

-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.

-r, --recursive
       Read all files under each directory, recursively, following symbolic links only if they are on the  command  line.
       This is equivalent to the -d recurse option.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多