首页 > IT职业 > IT生活 > Bash中的[命令历史]tips小结
在之前就记录过不少和Bash的命令历史相关的小技巧,比如:统计最常用的10个命令、以root身份执行上一条命令、快速获取上一条命令的最后一个参数……
后来看到一篇文章: 像黑客一样使用 Linux 命令行 ,学习到了不少,同时又去网上搜了更多的资料,记录、总结成了本文。
history |> awk '{CMD[$2]++;count++;} END{ for(a in CMD) print CMD[a],CMD[a]/count*100 "% " a;}' |> grep -v "./" |> column -c3 -s " " -t |> sort -nr |> nl |> head -n 10
# 以root身份执行上一条命令$ sudo !!
#使用 ^old^new 换掉输错或输少的部分( !:s/old/new )#使用 !:gs/old/new 将 old 全部换成 new$ echo "a nginx, web nginx server, nginx..."a nginx, web nginx server, nginx...$ ^nginx^apache echo "a apache, web nginx server, nginx..."a apache, web nginx server, nginx...$ $ echo "a nginx, web nginx server, nginx..."a nginx, web nginx server, nginx...$ !:gs/nginx/apache echo "a apache, web apache server, apache..."a apache, web apache server, apache...$
#通过 !$ 得到上一条命令的最后一位参数#快捷键 [Alt + .]
#通过 !^ 得到上一条命令的第一个参数#快捷键 [Ctrl + Alt + y]
#通过 !:n 得到上一条命令第 n 个参数$ touch foo.txt bar.txt baz.txt $ vim !:2vim bar.txt#通过 !:x-y 得到上一条命令从 x 到 y 的参数$ touch foo.txt bar.txt baz.txt $ vim !:1-2vim foo.txt bar.txt#通过 !:n* 得到上一条命令从 n 开始到最后的参数$ cat /etc/resolv.conf /etc/hosts /etc/hostname $ vim !:2*vim /etc/hosts /etc/hostname#通过 !* 得到上一条命令的所有参数$ touch foo.txt bar.txt hey.txt haha.txt hello.txt $ cat !*cat foo.txt bar.txt hey.txt haha.txt hello.txt
#通过 !# 得到上一条命令的命令名
原文链接:http://outofmemory.cn/shell/shell-tips
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2989/viewspace-2805592/ ,如需转载,请注明出处,否则将追究法律责任。