行业新闻

深入了解Linux中的history

深入了解Linux中的history

在 Linux 中,有一个非常有用的命令可以向你显示最近使用过的所有最后命令。该命令简称为history

显示时间戳

通常当你从命令行键入历史记录时,它会显示命令# 和命令。出于审计目的,将时间戳与命令一起显示可能会有所帮助,如下所示。
export HISTTIMEFORMAT='%F %T '
history | more
1  2020-06-25 19:02:39 systemctl restart network
2  2020-06-25 19:02:43 exit
3  2020-06-25 19:02:47 id
4  2020-06-25 19:02:56 cat /etc/hosts

使用 Control+R 搜索历史记录

这可能是你历史上最常用的功能。当你已经执行了一个很长的命令时,你可以简单地使用关键字搜索历史记录并重新执行相同的命令,而无需完全键入它。
  • 按 Control+R 并键入关键字。
  • 当你看到你的命令时按回车键,这将执行历史记录中的命令。
在以下示例中,我搜索了host,它显示cat /etc/hosts了历史记录中包含单词的上一个命令host
(reverse-i-search)`host': cat /etc/hosts
cat /etc/hosts
#  
11 localhost.localdomain localhost server1
::1  localhost.localdomain localhost server1
有时你想在执行之前编辑历史命令。例如,你可以搜索systemctl,它将
systemctl restart network
从命令历史记录中显示,选择此命令(按 ESC 键)并更改restartstop并再次重新执行,如下所示。
(reverse-i-search)`systemctl': systemctl stop network
systemctl stop network

使用 4 种不同的方法快速重复上一个命令

有时你可能会因为各种原因重复前面的命令。以下是重复上次执行命令的 4 种不同方式。
  1. 使用 up arrow 查看上一条命令,按回车键执行。
  2. 类型 !! ,并按命令行输入
  3. !-1 从命令行键入 并按回车键。
  4. 按 Control+P 会显示上一条命令,按回车键执行。

执行历史记录中的特定命令

在下面的例子中,如果你想重复命令#4,你可以做 !4 如下所示。

history | more

systemctl restart network
exit
id
cat /etc/hosts

!4

cat /etc/hosts
#  
11 localhost.localdomain localhost server1
::1  localhost.localdomain localhost server1

执行以特定单词开头的上一个命令

输入!后跟要重新执行的命令的开头几个字母。在以下示例中,输入!ps并回车,执行以 开头的上一个命令ps,即ps aux | grep yp

!ps

ps aux | grep yp
root     16947  0  1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0  0   4124   740 pts/0    S+   19:19   0:00 grep yp

控制历史总行数

将以下两行附加到.bash_profile并重新登录到 bash shell 以查看更改。在此示例中,1500 命令将存储在 bash 历史记录中。

vi ~/.bash_profile

HISTSIZE=1500
HISTFILESIZE=1500

更改历史文件名

默认情况下,历史记录存储在 ~/.bash_history 文件中。将以下行添加到 .bash_profile 并重新登录到 bash shell,将历史命令存储在.my_commandline文件而不是.bash_history文件中。当你想要使用不同的历史文件名跟踪从不同终端执行的命令时,会使用它。

vi ~/.bash_profile

HISTFILE=/root/.my_commandline

消除历史的连续重复输入

在下面的例子pwd中输入了 3 次,当你做 history 时,你可以看到它的所有 3 次连续出现。为了消除重复的,设置HISTCONTROLignoredups如下所示。

history | tail -4

44  pwd
45  pwd
46  pwd
47  history | tail -4
export HISTCONTROL=ignoredups
history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd
58  history | tail -4

擦除整个历史中的重复项

上面显示的 ignoreups 仅当它们是连续命令时才会删除重复项。要消除整个历史记录中的重复项,请将 HISTCONTROL 设置为 erasedups,如下所示。
export HISTCONTROL=erasedups
pwd
systemctl restart network
history | tail -3
38  pwd
39  systemctl restart network
40  history | tail -3
ls -l
systemctl restart network
history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -l
39  systemctl restart network
40  history | tail -6

强制历史记录不记住特定命令

当你执行命令时,你可以通过设置HISTCONTROLignorespaceAND 在命令前键入一个空格来指示历史记录忽略该命令,如下所示。我可以看到很多初级系统管理员对此感到兴奋,因为他们可以从历史记录中隐藏命令。了解ignorespace工作原理是很好的。但是,作为最佳实践,不要有目的地向历史隐瞒任何事情。
export HISTCONTROL=ignorespace
ls -ltr
pwd
systemctl restart network #Note that there is a space at the beginning of service, to ignore this command from history

history | tail -3

67  ls -l
68  pwd
69  history | tail -3

清除所有以前的历史记录

有时你可能想清除所有以前的历史记录,但又想让历史记录向前推进。

history -c

替换历史命令中的单词

当你搜索历史记录时,你可能想要执行不同的命令,但使用刚刚搜索的命令中的相同参数。
在下面的示例中,!!:$ vi 命令的 next 获取从前一个命令到当前命令的参数。
ls nginx.conf
nginx.conf
vi !!:$
vi nginx.conf`
在下面的示例中,!^ vi 命令的 next 获取从前一个命令(即cp命令)到当前命令(即vi命令)的第一个参数。
cp nginx.conf nginx.conf.bak
vi  !^

vi nginx.conf

将特定参数替换为特定命令

在下面的示例中, !cp:2 搜索历史记录中以 cp 开头的上一个命令,并采用 cp 的第二个参数并将其替换为 ls -l 命令,如下所示。
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 开头的上一个命令,并采用 cp 的最后一个参数(在这种情况下,也是如上所示的第二个参数)并将其替换为 ls -l 命令,如下所示.

ls -l !cp:$

ls -l /really/a/very/long/path/long-filename.txt

禁用历史的使用

如果你想同时禁用历史记录并且不希望 bash shell 记住你输入的命令,请将 设置HISTSIZE为 0,如下所示。
export HISTSIZE=0
history
# Note that history did not display anything

忽略历史记录中的特定命令

有时你可能不想用基本命令(例如pwd和 )弄乱你的历史记录ls。使用HISTIGNORE你想从历史中忽略指定的所有命令。请注意,添加lsHISTIGNOREignores onlyls而不是ls -l. 因此,你必须提供你希望从历史记录中忽略的确切命令。
export HISTIGNORE="pwd:ls:ls -ltr:"
pwd
ls
ls -l
systemctl restart network

history | tail -3

79  export HISTIGNORE="pwd:ls:ls -l:"
80  systemctl restart network
81  history
[Note that history did not record pwd, ls and ls -l]
关闭