好用的15个history命令例子
文章目录
- 1 使用HISTTIMEFORMAT显示时间戳
- 2 使用Control+R搜索历史记录
- 3 使用4种不同的方法快速重复上一个命令
- 4 执行历史记录中的特定命令
- 5 执行以特定单词开头的上一个命令
- 6 使用HISTSIZE控制历史记录中的行总数
- 7 使用HISTFILE更改历史记录文件名
- 8 使用HISTCONTROL从历史记录中消除连续重复输入
- 9 使用HISTCONTROL清除整个历史记录中的重复项
- 10 使用HISTCONTROL强制历史记录不记住特定命令
- 11 使用选项-c清除所有以前的历史记录
- 12 从历史记录命令中替换单词
- 13 用特定参数替换特定命令。
- 14 使用HISTSIZE禁用历史记录的使用
- 15 使用HISTIGNORE忽略历史记录中的特定命令
当您经常使用Linux命令行时,有效地使用history可以大大提高生产率。事实上,一旦您掌握了我在这里提供的15个示例,您就会发现使用命令行更有趣。
使用HISTTIMEFORMAT显示时间戳
通常,当您从命令行键入历史记录时,它会显示命令#和命令。出于审核目的,显示timepstamp以及如下所示的命令可能是有益的。
# 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
使用Control+R搜索历史记录
当您已经执行了一个很长的命令时,您可以简单地使用关键字搜索历史,然后重新执行相同的命令,而不必完全键入它。按Control+R并键入关键字。在下面的示例中,我搜索了red,它显示了历史记录中包含单词red的上一个命令“cat/etc/redhat release”。
# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)
有时,您希望在执行命令之前从历史记录中编辑命令。例如,您可以搜索httpd,它将从命令历史记录中显示服务httpd stop,选择此命令并将stop更改为start,然后再次执行,如下所示。
# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start
使用4种不同的方法快速重复上一个命令
有时,由于各种原因,您可能会重复前面的命令。以下是重复上次执行的命令的4种不同方式。
- 使用向上箭头查看上一个命令,然后按enter键执行该命令。
!!
然后在命令行中按enter键!-1
,然后从命令行按enter键。- 按
Control+P
将显示上一个命令,按enter键执行该命令
执行历史记录中的特定命令
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
执行以特定单词开头的上一个命令
打字!后跟要重新执行的命令的起始字母。在下面的示例中,键入!ps和enter,执行以ps开头的上一个命令,即“ps aux | grep yp”。
# !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
使用HISTSIZE控制历史记录中的行总数
将以下两行附加到.bash_概要文件,并再次重新登录到bash shell以查看更改。在本例中,bash历史记录中只存储450个命令。
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
使用HISTFILE更改历史记录文件名
默认情况下,历史记录存储在~/.bash_历史文件中。将以下行添加到.bash_概要文件并重新登录到bash shell,以将历史记录命令存储在.commandline_warrior文件而不是.bash_历史记录文件中。
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
使用HISTCONTROL从历史记录中消除连续重复输入
在下面的示例中,pwd被键入了三次,当您执行历史记录时,您可以看到它的所有3次连续出现。要消除重复项,请将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
使用HISTCONTROL清除整个历史记录中的重复项
上面显示的ignoredups仅当重复项是连续命令时才删除它们。要消除整个历史记录中的重复项,请将HISTCONTROL设置为“擦除重复项”,如下所示。
# 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
使用HISTCONTROL强制历史记录不记住特定命令
执行命令时,可以通过将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
使用选项-c清除所有以前的历史记录
有时,您可能希望清除所有以前的历史记录,但希望保持历史记录向前发展。
# history -c
从历史记录命令中替换单词
当您在历史记录中搜索时,您可能希望执行不同的命令,但使用与刚才搜索的命令相同的参数。
在下面的示例中,!!:$
在vi命令旁边,获取上一个命令到当前命令的参数。
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
在下面的示例中,^在vi命令旁边,获取从上一个命令(即cp命令)到当前命令(即vi命令)的第一个参数。
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi !^
vi anaconda-ks.cfg
用特定参数替换特定命令。
在下面的例子中!cp:2
在历史记录中搜索以cp开头的前一个命令,并获取cp的第二个参数,并将其替换为ls-l命令,如下所示。
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt
使用HISTSIZE禁用历史记录的使用
如果希望同时禁用历史记录,并且不希望bash shell记住键入的命令,请将HISTSIZE设置为0,如下所示。
# export HISTSIZE=0
# history
# [Note that history did not display anything]
使用HISTIGNORE忽略历史记录中的特定命令
有时,您可能不想用基本命令(如pwd和ls)混乱您的历史记录。使用HISTIGNORE指定要从历史记录中忽略的所有命令。请注意,将ls添加到HISTIGNORE只会忽略ls,而不会忽略ls-l。因此,您必须提供希望从历史记录中忽略的确切命令。
# 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