在软件开发中, 我们经常要到工程中查找"test"这个串所在的位置极相关信息。 当然, 如果您用source insight, 那也可以, 但快不快我就不保证了。 另外, 我发现, 如果在Win7的文件夹中要找这样的“test”, 那将是极为恼火的, 反正我搞不定。
还是用linux命令吧, 比如: find -depth -type f -iname "*.cpp" | xargs grep --color -nE -ir test
上面命令中的各个参数还是很容易懂的, 如果不清楚, man find一下, man grep一下, 就什么都知道了。 要善于利用linux自己提供的功能。
当然, 事情还没有完, 上面那一串太复杂了, 我们可以用alias来简化, 把下面语句放在主目录的.bashrc中:
alias findcpp='find -depth -type f -iname "*.cpp" | xargs grep --color -nE -ir'
保存.bashrc文件, 然后source ~/.bashrc使之生效(事实上, 以后每次打开shell, 都会自动执行这个命令, 所以上述的alias是可以生效的), 然后就有了:
[taoge@localhost ~]$ source ~/.bashrc
[taoge@localhost ~]$ cat test.cpp
hello
world
good
hello ok
[taoge@localhost ~]$ findcpp hello
1:hello
4:hello ok
[taoge@localhost ~]$ touch b.cpp
[taoge@localhost ~]$ findcpp hello
./test.cpp:1:hello
./test.cpp:4:hello ok
[taoge@localhost ~]$
这是多么美好的结果啊。 用alias, 事半功倍。 alias的本质就是别名。 用alias定义xxx后, 如果要取消, 则用unalias xxx即可。
我喜欢的用法是:
alias findcpp='find -depth -type f -iname "*.cpp" | xargs grep --color -nE -ir'
alias findtxt='find -depth -type f -iname "*.txt" | xargs grep --color -nE -ir'
alias findxml='find -depth -type f -iname "*.xml" | xargs grep --color -nE -ir'
alias findcfg='find -depth -type f -iname "*.cfg" | xargs grep --color -nE -ir'
alias findc='find -depth -type f -iname "*.c" | xargs grep --color -nE -ir'
alias findjava='find -depth -type f -iname "*.java" | xargs grep --color -nE -ir'
alias findh='find -depth -type f -iname "*.h" | xargs grep --color -nE -ir'
alias findsh='find -depth -type f -iname "*.sh" | xargs grep --color -nE -ir'
alias findini='find -depth -type f -iname "*.ini" | xargs grep --color -nE -ir'
alias findhtml='find -depth -type f -iname "*.html" | xargs grep --color -nE -ir'
alias findjs='find -depth -type f -iname "*.js" | xargs grep --color -nE -ir'
alias findcss='find -depth -type f -iname "*.css" | xargs grep --color -nE -ir'
alias findphp='find -depth -type f -iname "*.php" | xargs grep --color -nE -ir'
alias findmk='find -depth -type f -iname "*make*" | xargs grep --color -nE -ir'
alias findso='find -depth -type f -iname "*.so" | xargs strings -f | grep --color -nE -ir'
alias finda='find -depth -type f -iname "*.a" | xargs strings -f | grep --color -nE -ir'
alias findfile='find . -depth -type f -iname "*" | xargs md5sum | grep --color -nE -ir' # 实际上, 如果遇到错误信息, 则需要用如下:
alias findfile='find . -depth -type f -iname "*" 2>/dev/null | xargs md5sum 2>/dev/null | grep --color -nE -ir'
alias h='history 100 | grep -i'
alias tailf='tail -f'