再来说说linux中重要的alias命令------从findcpp命令说起

本文介绍了一种在Linux环境中高效查找特定字符串的方法,通过使用find和grep命令结合alias功能,实现对不同类型的文件进行快速搜索,并展示了具体的命令示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在软件开发中, 我们经常要到工程中查找"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'

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值