Bash中的[命令历史]tips小结


在之前就记录过不少和Bash的命令历史相关的小技巧,比如:统计最常用的10个命令、以root身份执行上一条命令、快速获取上一条命令的最后一个参数……

后来看到一篇文章:像黑客一样使用 Linux 命令行,学习到了不少,同时又去网上搜了更多的资料,记录、总结成了本文。

0.最常用的10个命令
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
1.以root身份执行上一条命令
# 以root身份执行上一条命令
$ sudo !!
2.替换上一条命令的[部分]内容
#使用 ^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...
$
3.快速获取上一条命令的最后一位参数
#通过 !$ 得到上一条命令的最后一位参数
#快捷键 [Alt + .]
4.快速获取上一条命令的第一位参数
#通过 !^ 得到上一条命令的第一个参数
#快捷键 [Ctrl + Alt + y]
5.如何获取上一条命令的多个参数
#通过 !:n 得到上一条命令第 n 个参数
$ touch foo.txt bar.txt baz.txt
$ vim !:2
vim bar.txt

#通过 !:x-y 得到上一条命令从 x 到 y 的参数
$ touch foo.txt bar.txt baz.txt
$ vim !:1-2
vim 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
6.如何获取上一条命令的命令名
#通过 !# 得到上一条命令的命令名
参考链接:

=EOF=

,

《“Bash中的[命令历史]tips小结”》 有 7 条评论

  1. 命令替换的相关参考链接:
    `
    root@crazy:~# echo “hi, nihao, hi, good”
    hi, nihao, hi, good
    root@crazy:~#
    root@crazy:~# ^hi^hey^:& #最后的「:&」表示重复执行之前的动作,不是进行一次全局替换「http://stackoverflow.com/a/6357381」
    echo “hey, nihao, hey, good”
    hey, nihao, hey, good
    root@crazy:~#
    root@crazy:~# echo “hi, nihao, hi, good”
    hi, nihao, hi, good
    root@crazy:~# !!:gs^hi^hey^ #对上一个命令进行全局替换「http://stackoverflow.com/a/2149566」
    echo “hey, nihao, hey, good”
    hey, nihao, hey, good
    `
    http://stackoverflow.com/questions/2149482/caret-search-and-replace-in-bash-shell
    http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators
    http://www.softpanorama.org/Scripting/Shellorama/history.shtml

  2. 为Bash增加命令执行日志
    http://itxx00.github.io/blog/2011/02/17/bash-history-logging/
    `
    一:bash为何需要logging
    二:加固bash_history
    第1步:使bash历史记录文件和相关文件无法被删除或修改。
    第2步:配置 .bash*配置文件
    第3步:禁掉系统中所有其他shell,一般包括csh,tcsh,ksh。
    三:攻破logging机制
    四:Hacking bash-使用syslog日志接口
    五:总结
    六:参考资料
    `

  3. Ubuntu bash不记录history方法
    https://www.cnblogs.com/sevck/p/7997739.html
    `
    很多都是用:
    unset HISTORY HISTFILE HISTSAVE HISTZONE HISTORY HISTLOG
    export HISTFILE=/dev/null
    export HISTSIZE=0
    export HISTFILESIZE=0

    HISTCONTROL=ignoredups:忽略连续重复的命令。
    HISTCONTROL=ignorespace:忽略以空白字符开头的命令。
    HISTCONTROL=ignoreboth:同时忽略以上两种。
    HISTCONTROL=erasedups:忽略所有历史命令中的重复命令。
    `

  4. 隐藏 Bash 命令历史的技巧
    https://digi.ninja/blog/hiding_bash_history.php
    `
    在依赖这些命令/方法时,请记得先自行测试!

    set +o history #不会将任何当前会话中敲的命令写入日志。可以在会话期间随时运行以隐藏其后的命令。
    export HISTIGNORE=”ls*:cat*” #用冒号分隔的不记录的命令列表
    在命令前面放置一个「空格」 #https://www.linuxjournal.com/content/using-bash-history-more-efficiently-histcontrol
    `

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注