history命令
在Linux输入history命令可以查看你输入的历史命令,例如:
[root@demo ~]# history
1 gnome-session
2 cd /etc/profile.d/
3 ls
4 ls -l
5 more lang.
6 more lang.sh
7 ls -l
8 clear
9 man history
10 ls
11 cd /home/test/
……………………………..
默认显示两列第一列是序号,根据你当时输入的命令序号自动增加,第二列就是你输入的命令包括命令的参数
通过修改变量可以改变默认的显示风格
[root@demo ~]# HISTTIMEFORMAT='%F %T '
重新运行history命令
[root@demo ~]# history
1 2014-11-28 13:43:06 gnome-session
2 2014-11-28 13:43:06 cd /etc/profile.d/
3 2014-11-28 13:43:06 ls
4 2014-11-28 13:43:06 ls -l
5 2014-11-28 13:43:06 more lang.
6 2014-11-28 13:43:06 more lang.sh
7 2014-11-28 13:43:06 ls -l
8 2014-11-28 13:43:06 clear
9 2014-11-28 13:43:06 man history
这样新增了当时执行该命令的时间的列
通常我们都是使用方向键来查找上一个命令,也可以使用Ctrl+P组合键来向上查找命令,如果有几百个命令想翻到前几个命令,那太费时间了,Linux提供了一个和数字组合的命令可以快速定位到某个历史命令,这个组合就是:!n,如果n是负数就代表是倒数的第n个命令,例如:
[root@demo ~]# history | more
1 gnome-session
2 cd /etc/profile.d/
3 ls
4 ls -l
5 more lang.
6 more lang.sh
7 ls –l
我想运行第7个命令,可以直接输入!7
[root@demo ~]# !7 ls -l 总用量 92 -rw-------. 1 root root 1642 5月 24 2014 anaconda-ks.cfg -rw-r--r--. 1 root root 43493 11月 28 12:54 install.log -rw-r--r--. 1 root root 9922 5月 24 2014 install.log.syslog drwxr-xr-x. 2 root root 4096 5月 24 2014 公共的 drwxr-xr-x. 2 root root 4096 5月 24 2014 模板 drwxr-xr-x. 2 root root 4096 5月 24 2014 视频 drwxr-xr-x. 2 root root 4096 5月 24 2014 图片 drwxr-xr-x. 2 root root 4096 5月 24 2014 文档 drwxr-xr-x. 2 root root 4096 5月 24 2014 下载 drwxr-xr-x. 2 root root 4096 5月 24 2014 音乐 drwxr-xr-x. 2 root root 4096 11月 28 13:03 桌面
可以输入负数自己看些效果。
如果要执行上一命令可以直接使用快捷键!!
如果我只记住一个历史命令的开始,也可以使用!String
例如:
[root@demo test]# history
1 cd /home/test/
2 date
3 history
我想重新执行第二个命令,就可以使用!d
[root@demo test]# !d date 2014年 11月 28日 星期五 17:02:07 CST
如果想查找某个历史命令匹配命令的任意位置,而不是开始位置时,可以使用!?String
[root@demo test]# history
1 service sshd restart
2 history
我想重新运行第二条命令,可以使用!?ssh
[root@demo test]# !?ssh service sshd restart 停止 sshd: [确定] 正在启动 sshd: [确定]
通常我们在编辑一个文件时,会先查看下文件的内容,例如:
[root@demo test]# more /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 NM_CONTROLLED=yes ONBOOT=yes HWADDR=00:0c:29:68:94:6b TYPE=Ethernet BOOTPROTO=dhcp PREFIX=24 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 USERCTL=no DNS1=202.99.166.4 DNS2=192.168.239.2 PEERDNS=yes
如果要编辑该文件,还要重新输入后面常常的参数,即使向上找到这个命令,也要讲光标定位到开头,将more修改为vim,能不能输入vim命令后,直接将上个命令的参数追加到后面呢?答案是肯定的,就是使用!!:$(或者!:$)
[root@demo test]# vim !!:$ vim /etc/sysconfig/network-scripts/ifcfg-eth0
如果你感觉!!:$太复杂,也可以使用Ese+.,输入该命令后,它只是将参数给你补齐,需要你手动回车里真正运行编辑命令。
如果命令有多个参数,你需要的不是最后一个参数,你需要使用!:String:n
例如:
[root@demo test]# history
1 cp /home/test/output.txt /root/bb
2 history
我想编辑/home/test/output.txt这个文件,需要输入vim !:cp:1
[root@demo test]# vi !cp:1 vi /home/test/output.txt
文章开头介绍了一个和history命令控制相关的变量HISTTIMEFORMAT,下面接着介绍:
1. export HISTCONTROL=ignoredups 忽略重复的历史命令
默认历史命令会记录你重复的命令
[root@demo ~]# history -c
[root@demo ~]# date
2014年 11月 28日 星期五 18:18:40 CST
[root@demo ~]# date
2014年 11月 28日 星期五 18:18:41 CST
[root@demo ~]# echo ""
[root@demo ~]# date
2014年 11月 28日 星期五 18:18:51 CST
[root@demo ~]# history
1 date
2 echo ""
3 date
4 history
注意:如果你连续输入相同的命令则history只记录一个。
设置参数后重新测试:
[root@demo ~]# history -c
[root@demo ~]# export HISTCONTROL=ignoredups
[root@demo ~]# date
2014年 11月 28日 星期五 18:20:54 CST
[root@demo ~]# date
2014年 11月 28日 星期五 18:20:54 CST
[root@demo ~]# ls
aa bb install.log.syslog 公共的 视频 文档 音乐
anaconda-ks.cfg install.log output.txt 模板 图片 下载 桌面
[root@demo ~]# date
2014年 11月 28日 星期五 18:20:56 CST
[root@demo ~]# history
1 export HISTCONTROL=ignoredups
2 date
3 ls
4 date
5 history
2. export HISTCONTROL=erasedups移除重复的记录,当你输入的命令已经在历史命令列表时,会移除之前的命令,并记录当前命令。
[root@demo ~]# export HISTCONTROL=erasedups
[root@demo ~]# history -c
[root@demo ~]# pwd
/root
[root@demo ~]# ls
aa bb install.log.syslog 公共的 视频 文档 音乐
anaconda-ks.cfg install.log output.txt 模板 图片 下载 桌面
[root@demo ~]# pwd
/root
[root@demo ~]# history
1 ls
2 pwd
3 history
第四次输入的pwd命令和第一次输入的pwd命令重复,所以第一次输入的pwd命令被移除。
3. export HISTSIZE=0:禁用历史存储命令。
4. export HISTIGNORE=”pwd:ls:ls –ltr:”不记录某些特定的命令。