15个例子掌握Linux命令行历史

15个例子掌握Linux命令行历史

当你经常使用Linux命令行时,有效地使用历史可以提高生产力。事实上,一旦你掌握以下15个例子,你会发现使用命令行更有意思更有趣。

1.使用HISTTIMEFORMAT显示时间戳

通常,当你从命令行键入历史记录时,它会显示#和命令。出于审计目的,将timestamp与命令一起显示是很有帮助的,如下所示。

# export HISTTIMEFORMAT='%F %T '
# history | more
1  2018-08-05 19:02:39 service network restart
2  2018-08-05 19:02:39 exit
3  2018-08-05 19:02:39 id
4  2018-08-05 19:02:39 cat /etc/redhat-release

2.使用Ctrl+R搜索历史记录

这可能是最常用的历史特征。当你执行了很长的命令,你只需使用关键词搜索历史记录并重新执行相同命令即可,而无需完全键入命令。按Ctrl+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
CentOS release 6.5 (Final)

有时希望在执行命令之前能够从命令历史中编辑命令。例如,你可以搜索httpd,它显示命令历史中的service 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

3.使用4种不同的方法快速执行上一条命令。

有时你会因为各种原因重复执行以前的命令。以下是4种不同的方法去重复执行上次执行的命令。

  1. 使用up arrow查看上以前的命令,然后按Enter键执行
  2. 键入!!并在命令中按Enter键执行
  3. 键入!-1并在命令中按Enter键执行
  4. Control + P将显示以前的命令,然后按enter键执行

4.从命令历史中执行特定的命令

如下所示,如果你想重复执行命令#4,你可以键入!4执行。

# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release

# !4
cat /etc/redhat-release
CentOS release 6.5 (Final)

5.执行上一条以特定关键字开头的命令

输入! 然后是您想要重新执行的命令的起始几个字母。在下面的示例中,键入 !ps 并输入,执行上一个以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

6.使用HISTSIZE控制历史记录的总数

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

# vi~ / .bash_profile
HISTSIZE = 450
HISTFILESIZE = 450

7.使用HISTFILE更改历史文件名

默认情况下,历史记录存储在~/.bash_history文件中。将以下行添加到.bash_profile并重新登录到bash shell,以将历史命令存储在.commandline_warrior文件中,而不是.bash_history文件中。我还没有找到实际用途。当您想要使用不同的历史文件名跟踪从不同终端执行的命令时,这时候就派上用场了。

# vi~ / .bash_profile 
HISTFILE = / root / .commandline_warrior

8.使用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

注意:相同的命令必须是满足连续这个条件

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强制历史记录不要记住特定命令

执行命令时,可以通过将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.从历史命令中获取参数

在搜索历史记录时,您可能希望执行不同的命令,但使用您刚刚搜索过的命令中的相同参数。

在下面的示例中,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

13.替换特定命令的特定参数。

在下面的示例中,!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

14.使用HISTSIZE禁用历史记录

如果想要禁用历史记录并且不希望bash shell记住您键入的命令,请将HISTSIZE设置为0,如下所示。

# export HISTSIZE=0
# history
# [Note that history did not display anything]

15.使用HISTIGNORE忽略历史记录中的特定命令

有时你可能不希望使用pwd和ls等基本命令来混淆您的历史记录。使用HISTIGNORE指定要从历史记录中忽略的所有命令。请注意,向HISTIGNORE添加ls仅忽略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
[Note that history did not record pwd, ls and ls -ltr]

猜你喜欢

转载自www.cnblogs.com/dnote/p/9461813.html
今日推荐