标题:常用find + grep查找封装 出处:Felix021 时间:Sun, 03 Mar 2013 23:45:52 +0000 作者:felix021 地址:https://www.felix021.com/blog/read.php?2107 内容: 看源码的时候经常要在某一类文件里面grep一些内容,用标准的find + grep写起来很辛苦: $ find -name "*.c" -exec grep {} -Hne "hello world" \; 所以简单封装了下,保存成 ~/bin/xgrep 然后把 ~/bin 加入到 PATH 里去,以后就只需要 $ xgrep \*.c "hello world" #注意这个 \*.c 里可以用的是*和?的通配符,不是正则 #!/bin/bash if [ -z "$1" -o -z "$2" ]; then echo "Usage: xgrep FilePattern WordPattern" exit fi filepat="$1" greppat="$2" shift shift set -x find -name "$filepat" -exec grep {} -Hne "$greppat" $* \; #后来才想起grep其实有个--exclude=PATTERN(可以去掉find),但是已经这么用了挺久,习惯了。。。 Generated by Bo-blog 2.1.0