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
相关博客:https://blog.sina.com.cn/s/blog_ecd48c580102vxjn.html