Linux下HISTORY命令使用详解

经常使用Linux命令行的CXY会特别喜欢history命令,因为它可以有效地提升你的工作效率。

下面详细介绍下history命令的16个用法。

1:使用HISTTIMEFORMAT显示时间戳:

输入命令history只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:
# export HISTTIMEFORMAT=’%F %T ‘
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/redhat-release
注意:这个功能只在HISTTIMEFORMAT这个环境变量被设置之后才可用。

2:使用 Ctrl+R 搜索历史

Ctrl+R快捷键对命令历史进行搜索将展示reverse-i-search
#(reverse-i-search)`red‘: cat /etc/redhat-release
直接按Enter键将直接执行选择的命令,按左键或者右键,可以编辑它
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

3:四种方法重复执行上一条命令

使用上方向键,并回车执行。
按 !! 并回车执行。
输入 !-1 并回车执行。
按 Ctrl+P 并回车执行。

4:从命令历史中执行一个指定的命令

在下面的例子中,如果你想重复执行第4条命令,那么可以执行!4:
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)

5:通过指定关键字来执行以前的命令

在下面的例子,输入!ps并回车,将执行以ps打头的命令:
# !ps
ps aux | grep yp
root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp

6:使用 HISTSIZE 控制历史命令记录的总行数

将下面两行内容追加到.bash_profile文件并重新登录bash shell,命令历史的记录数将变成 450 条::
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

7:使用HISTFILE:更改历史文件名称

默认情况下,命令历史存储在~/.bash_history文件中。添加下列内容到.bash_profile文件并重新登录bash shell,将使用.commandline_warrior来存储命令历史:
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

8:使用HISTCONTROL从命令历史中剔除连续重复的条目

在下面的例子中,pwd命令被连续执行了三次。执行history后你会看到三条重复的条目。要剔除这些重复的条目,你可以将HISTCONTROL设置为ignoredups:
# pwd
# pwd
# pwd
# history | tail -4
44 pwd
45 pwd
46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47 history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56 export HISTCONTROL=ignoredups
57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58 history | tail -4

9:使用HISTCONTROL清除整个命令历史中的重复条目

上例中的ignoredups只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将HISTCONTROL设置成erasedups
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38 pwd
39 service httpd stop
40 history | tail -3
# ls -ltr
# service httpd stop
# history | tail -6
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
39 service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6

10:使用HISTCONTROL强制history不记住特定的命令

将HISTCONTROL设置为ignorespace,并在不想被记住的命令前面输入一个空格:
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3

11:使用:-c选项清除所有的命令历史

如果你想清除所有的命令历史,可以执行:
# history -c

12:命令替换

在下面的例子里,!!:$将为当前的命令获得上一条命令的参数
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg

补充:使用!$可以达到同样的效果,而且更简单。

下例中,!^从上一条命令获得第一项参数:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg

13:为特定的命令替换指定的参数

下面的例子,!cp:2从命令历史中搜索以cp开头的命令,并获取它的第二项参数:
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt

下例里,!cp:$ 获取 cp 命令的最后一项参数:
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

14:使用HISTSIZE禁用history

如果你想禁用history,可以将HISTSIZE设置为0
# export HISTSIZE=0
# history
# [Note that history did not display anything]

15:使用HISTIGNORE忽略历史中的特定命令

下面的例子,将忽略pwd、ls、ls -ltr等命令:
# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79 export HISTIGNORE=”pwd:ls:ls -ltr:”
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr]

16:导出history命令到文件
# history > /tmp/history.txt

郑重声明:

1 本资源来源于互联网,资源的版权归资源原作者所持有,受《中华人民共和国著作权法》等相关法律保护。

2 由于无法和原作者取得联系,所以上传的部分资源无法先通过原作者的同意就分享给大家了,如本资源侵犯了您(原作者)的权益,请联系我们(微信号 xiaohaimei1989),我们会立马删除您的资源,并向您表达诚挚的歉意!

3 本站是一个公益型网站,分享资源的目的在于传播知识,分享知识,收取一点点打赏的辛苦费是用于网站的日常运营开支,并非用于商业用途。

4 本站资源只提供学习和参考研究使用,使用过后请在第一时间内删除。本站不承担资源被单位或个人商用带来的法律责任。

发表评论