Bash命令搜索任何出现的短语和返回文件和路径的列表 [英] Bash command to search for any occurrence of phrase and return list of files and paths

查看:177
本文介绍了Bash命令搜索任何出现的短语和返回文件和路径的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的bash命令来搜索文件名,目录名称和所有文件的文本中的短语。它应该返回文件和目录的列表。理想情况下,我想要的选项管道它像一个文件像> myfiles.txt

I'm looking for a simple bash command to search for a phrase in filenames, directory names, and within text of all files. It should return a list of files and directories. Ideally, I'd like the option to pipe it to a file like > myfiles.txt

像:

find 'my key phrase'
find 'my key phrase' > mylist.txt

将返回:

/home/stuff/filewithmykeyphraseinit.txt
/home/stuff/a filename with my key phrase.doc
/home/stuff/a directory with my key phrase/another subdirectory/

EDIT:
很多很好的建议,我目前测试。一个问题:有没有办法使这些不区分大小写?我相信添加-i到grep工程。如何找到文件路径/名称?此外,我想选择将输出发送到文本文件或屏幕。

I'm getting a lot of great suggestions I'm currently testing. One issue: is there a way to make these case-insensitive? I believe adding -i to grep works. How about find for filepaths/names? Also, I'd like to have the option to either send the output to a text file or to screen.

推荐答案

{ find . -name '*my key phrase*' ;
  grep -rl 'my key phrase' .     ;
} | sort -u > mylist.txt

这篇关于Bash命令搜索任何出现的短语和返回文件和路径的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆