关于linux:linux-上查找包含特定文本的所有文件

4次阅读

共计 746 个字符,预计需要花费 2 分钟才能阅读完成。

grep

> 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 参数排除一个或多个目录。例如,这将排除目录 dir1/dir2/ 以及所有与 *.dst/ 匹配的目录

> grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

ack

> awk "/root/" /etc/passwd

find

> find / -type f -exec grep -l "rumenz" {} \; 
> find . -name "*.txt" | xargs grep -i "rumenz"

别名一个 ffind

在~/.bashrc 文件中

> alias ffind find / -type f | xargs grep

启动一个新终端

> ffind 'rumenz'

ack-grep

> ack-grep "rumenz"

ack

> ack -i rumenz doc/*

git 存储库中查找

> git grep "rumenz"

原文链接:https://rumenz.com/rumenbiji/…
微信公众号: 入门小站

正文完
 0