linux 上查找包含特定文本的所有文件


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/linux-find-strings.html
微信公眾號:入門小站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM