如何在Linux上找到所有包含特定文本的文件?
0 665
0
该提问暂无详细描述
收藏
2021-02-15 17:39 更新 空心人 •  3374
共 2 个回答
高赞 时间
0

想找到一种方法来扫描整个Linux系统,来查找包含特定文本字符串的所有文件。 曾经使用过如下的解决方案: find / -type f -exec grep -H 'text-to-find-here' {} ; 但是,却显示了系统中的每个文件。

收藏
2021-02-15 17:42 更新 空心人 •  3374
0

请执行下列操作: grep -rnw '/path/to/somewhere/' -e 'pattern'

• -r或者-R表示递归 • -n 是行号 • -w 代表匹配整个单词。 • -l 可以添加(小写的L)以仅给出匹配文件的文件名。 • -e 是搜索过程中使用的模式 除了这些,--exclude,--include,--exclude-dir标志可用于高效搜索: • 只会搜索扩展名为.c或.h的文件: grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" • 排除搜索以.o扩展名结尾的所有文件: grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern" • 对于目录,可以使用--exclude-dir参数排除一个或多个目录。 例如,这会排除dirs dir1 /,dir2 /及其全部与* .dst /匹配的目录: grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern" 有关更多选项,请检查man grep

转载自:https://stackoverflow.com/questions/16956810/how-do-i-find-all-files-containing-specific-text-on-linux

收藏
2021-02-15 17:46 更新 小眼的铁板烧 •  3524