Linux 中 histroy 很常用, 但是无论对于管理员, 普通使用者还是黑客, 从另一个角度而言, 它对于安全的重要性也不言而喻, 比如说这条命令”mysql –h198.168.0.1 –uroot –p1234”, 很重要的资讯, 资料库的密码被破解了. 同样掌握了 history 命令, 可以使你的效率得到很大提升.
执行 history 命令, 终端会显示之前执行过的命令及其编号, 而我们执行”! 编号”(例如!10) 或”!ls”(历史中以 ls 开头的命令, 以最近执行的优先), 可以执行 history 历史用相应的指令.
预设情况下, 命令历史被储存在.bash_history 档案中, 可以把下面一行新增到.bash_profile 档案中, 重新登入 shell, 则.new_command 档案将取代.bash_history 档案用来储存历史命令.#vi ~/.bash_profile HISTFILE=/root/.new_command.
接下来, 让我们看看 history 一些小技巧, 也许很有乐趣哦!
1 使用 HISTTIMEFORMAT 在历史命令中显示 TIMESTAMP
通常情况下, 我们输入 history 命令时, 终端中将显示你刚输入的命令及其编号. 如果出于审查命令的目的, 和命令一起显示时间戳, 使用者将很有帮助, 如下所示:
[root@sqj2015 ~]export HISTTIMEFORMAT=”`whoami` %F %T ”  //whoami 用反引号 “
[root@sqj2015 ~]history |more
…..
74  root 2015-11-06 10:05:16 vi ~/.bash_profile
75  root 2015-11-06 10:06:15 vi /root/.bash_profile
76  root 2015-11-06 10:08:58 vi ~/.bash_profile
77  root 2015-11-06 10:09:56 vi /root/.bash_history
78  root 2015-11-06 10:10:16 cd /root
79  root 2015-11-06 10:10:18 ls -a
80  root 2015-11-06 10:35:23 export HISTTIMEFORMAT=”`whoami` %F %T ”
81  root 2015-11-06 10:35:25 history
注: 我们可以使用 history N(N 为数字) 来显示最近的几条命令.
 
2 巧妙使用 HISTCONTROL 命令
2.1 使用 HISTCONTROL 消除历史中连续重复命令
如下, 若连续输入三次 pwd, 当你使用 history 的时候, 你会看到这三条命令连续出现. 设定 HISTCONTROL 为 ignoredups. 来消除重复命令:
[root@sqj2015 ~]# history 4
108  pwd
109  pwd
110  pwd
111  history
若你设定:export HISTCONTROL=ignoredups, 而你再次连续输入三次 pwd, 执行 history 就只会显示一条 pwd 命令了.
 
2.2 使用 HISTCONTROL 消除整个历史中重复命令
[root@sqj2015 home]# history 4
97  cd /home/
98  pwd
99  history
100  history 4
设定 HISTCONTROL 为 erasedups,export HISTCONTROL=erasedups; 这时候, 当我再次键入 pwd 命令, 然后使用 history 会有什么样的结果:
[root@sqj2015 home]# history 5
97  cd /home/
98  history
99  history 4
100  pwd
101  history 5
你会发现,cd /home 下的 pwd 命令条目消除了.
 
2.3 使用 HISTCONTROL 强制 history 忽略某条特定命令
设定 HISTCONTROL 为 ignorespace(export HISTCONTROL=ignorespace) 后, 在执行一条命令时, 在该命令前加一个空格来指示 history 忽略该命令. 如下:
[root@sqj2015 home]# export HISTCONTROL=ignorespace
[root@sqj2015 home]#  cd /
[root@sqj2015 /]# history 3
105  history
106  export HISTCONTROL=ignorespace
109  history 3
很明显 cd / 这条命令在 history 历史条目中被隐藏了.
 
2.4 检视/设定 HISTCONTROL 变数值
echo $HISTCONTROL    //检视 HISTCONTROL 值
export HISTCONTROL=ignoredups/erasedups/ignorespace/  //设定 HISTCONTROL 值
若设定 export HISTCONTROL=  , 为空, 则会取消所有设定.
 
3 使用 c 选项清除所有历史命令
有时候你或许想要清楚之前所有的历史命令, 而你又不想让 history 继续工作. 就可以执行 history -c 命令.
 
4 替换命令历史中的内容
当你搜寻历史命令时, 你可能希望执行一个与刚刚查询到的历史命令具有相同的引数的命令, 例如:  “!!:$” 可以将前一个命令引数作为当前命令引数;“!^” 把上一个命令的第一个引数传递给当前命令;“!$” 把上一个命令的最后一个引数传递给当前命令.
[root@sqj2015 /]# touch 01.txt 02.txt 03.txt
[root@sqj2015 /]# ls 01.txt
01.txt
[root@sqj2015 /]# vi !!:$
vi 01.txt
 
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !^
vi 01.txt
 
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !$
vi 03.txt
 
5 替换特定命令的特定引数
[root@sqj2015 /]# ls 01.txt 02.txt 03.txt
01.txt  02.txt  03.txt
[root@sqj2015 /]# vi !ls:2
vi 02.txt
[root@sqj2015 /]# vi !ls:3
vi 03.txt
同样可以使用 “!ls:^”“!ls:$” 来指定第一个或最后一个引数.
 
6 使用 HISTSIZE 禁用 history
若你想禁用 history, 又不让 bashshell 记录你的命令, 就可以把 HISTSIZE 设为 0;
export HISTSIZE=0, 这样执行 history 后就不会显示任何资讯了.
 
7 使用 HISTIGNORE 让 history 在储存时忽略某些指令
有时候你不想在记录中看到诸如 pwd ls 之类的基本命令, 可以用 HISIGNORE 忽略这些指令. 但要注意: 在 HISTIGNORE 中新增 ls 只忽略 ls, 不忽略 ls -l , 所以一定要写出要忽略的指令.
[root@sqj2015 /]# export HISTIGNORE=”pwd:ls:”
[root@sqj2015 /]# pwd
/
[root@sqj2015 /]# ls
[root@sqj2015 /]# ls -l
[root@sqj2015 /]# history 5
13  vi 01.txt
14  vi 03.txt
15  export HISTIGNORE=”pwd:ls:”
16  ls -l
17  history 5
相关博客网站:http://blog.sina.com.cn/s/blog_ecd48c580102vxjn.html