想必大家在命令行环境下工作时候,一定有想要查找当前目录下的源代码文件中的某些字符的需求,这时候如果使用传统方案,你可能需要输入一长串的命令,比如这样:
1. grep -R 'string' dir/ 或者 grep -r -e string directory
2. grep -rnw 'directory' -e "pattern"
3. grep --include=*.{c,h} -rnw 'directory' -e "pattern"
4. grep --exclude=*.o -rnw 'directory' -e "pattern"
5. grep --exclude-dir={dir1,dir2,*.dst} -rnw 'directory' -e "pattern"
6. find dir/ -type f -exec grep -H 'string' {} +
或者更复杂的情况你可能需要管道与awk组合。
这里推荐一个命令行下的全文搜索工具:ack,非常方便地查找搜索,还支持高亮和文件行数定位。示例图如下:
安装方法:
Macports
Port "perl/p5-app-ack"
Homebrew on Mac
Formula "ack"
FreeBSD port
Port "textproc/p5-ack"
OpenBSD port
Package "p5-ack"
Debian
Package "ack-grep"
(To rename the "ack-grep" program to its proper name "ack", see the section below on renaming ack-grep)
Ubuntu
Package "ack-grep"
Fedora
Package "ack"
Gentoo
Package "sys-apps/ack"
Sabayon
Package "sys-apps/ack"
Arch
Package "community/ack"
Chocolatey for Windows
http://chocolatey.org/packages/ack
NixOS
Package "perl-ack"
参考链接:
0. ack 官网: http://beyondgrep.com/
1. find a text in all files in a dir : http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-on-linux
2. how to grep a string in a dir: http://stackoverflow.com/questions/15622328/how-to-grep-a-string-in-a-directory-and-all-its-subdirectories-files-in-linux
3. linux 利器: http://coolshell.cn/articles/7829.html