Merge pull request #19518 from wxy/20200415-Tweaking-history-on-Linux

TSL&PRF:20200415 Tweaking history on Linux
This commit is contained in:
Xingyu.Wang 2020-09-07 16:07:09 +08:00 committed by GitHub
commit c1214bbeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 189 additions and 189 deletions

View File

@ -1,189 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Tweaking history on Linux)
[#]: via: (https://www.networkworld.com/article/3537214/tweaking-history-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
Tweaking history on Linux
======
The bash shell's history command in Linux makes it easy to review and reuse commands, but there's a lot you do to control how much it remembers and how much forgets.
[Claudio Testa][1] [(CC0)][2]
The bash **history** command on Linux systems helps with remembering commands youve previously run and repeating those commands without having to retype them.
You could decide, however, that youd be just as happy to forget that you referenced a dozen man pages, listed your files every 10 minutes or viewed previously run commands by typing “history”. In this post, were going to look at how you can get the history command to remember just what you want it to remember and forget commands that are likely to be of little "historic value".
### Viewing your command history
To view previously run commands, you simply type “history”. Youll probably see a long list of commands. The number of commands remembered depends on an environment variable called **$HISTSIZE** that is set up in your **~/.bashrc** file, but theres nothing stopping you from changing this setting if you want to save more or fewer commands.
To view history, use the **history** command:
```
$ history
209 uname -v
210 date
211 man chage
...
```
To see the maximum number of commands that will be displayed:
```
$ echo $HISTSIZE
500
```
You can change **$HISTSIZE** and make the change permanent by running commands like these:
```
$ export HISTSIZE=1000
$ echo “HISTSIZE=1000” >> ~/.bashrc
```
Theres also a difference between how much history is preserved for you and how much is displayed when you type “history”. The **$HISTSIZE** variable controls how much history is displayed while the **$HISTFILESIZE** variable controls how many commands are retained in your **.bash_history** file.
[][3]
```
$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
2000
```
You can verify the second variable by counting the lines in your history file:
```
$ wc -l .bash_history
2000 .bash_history
```
One thing to keep in mind is that commands that you enter during a login session arent added to your **.bash_history** file until you log off, even though they show up in the **history** command output right away.
### Using history
There are three ways to reissue commands that you find in your history. The simplest way, especially if the command you want to reuse was run recently, is often to type a ! followed by enough of the first letters in the command's name to uniquely identify it.
```
$ !u
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020
```
Another easy way to repeat a command is to simply press your up-arrow key until the command is displayed and then press enter.
Alternately, if you run the history command and see the command you want to rerun listed, you can type an ! followed by the sequence number shown to the left of the command.
```
$ !209
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020
```
### Hiding history
If you want to stop recording commands for some period of time, you can use this command:
```
$ set +o history
```
Commands will not show up when you type "history" nor will they be added to your **.bash_history** file when you exit the session by logging off or exiting the terminal.
To reverse this setting, use **set -o history**. To make it permanent, you can add it to your **.bashrc** file, though failing to make use of command history altogether is generally not a good idea.
```
$ echo 'set +o history' >> ~/.bashrc
```
To temporarily clear history, so that only commands that you enter afterwards show up when you type "history", use the **history -c** (clear) command:
```
$ history | tail -3
209 uname -v
210 date
211 man chage
$ history -c
$ history
1 history
```
NOTE: The commands entered after typing "history -c" will not be added to your .bash_history file.
### Controlling history
The command history settings on many systems will default to including one called **$HISTCONTROL** that ensures that, even if you run the same command seven times in a row, it will only be remembered once. It also ensures that commands that you type after first entering one or more blanks will be omitted from your command history.
```
$ grep HISTCONTROL .bashrc
HISTCONTROL=ignoreboth
```
The "ignoreboth" means "ignore both duplicate commands and command starting with blanks". For example, if you type these commands:
```
$ echo try this
$ date
$ date
$ date
$ pwd
$ history
```
your history command should report something like this:
```
$ history
$ echo try this
$ date
$ history
```
Notice that the sequential date commands were reduced to one and the indented command was omitted.
### Overlooking history
To ignore certain commands so that they never show up when you type "history" and are never added to your **.bash_history** file, use the **$HISTIGNORE** setting. For example:
```
$ export HISTIGNORE=”history:cd:exit:ls:pwd:man”
```
This setting will cause all **history**, **cd**, **exit**, **ls**, **pwd** and **man** commands to be omitted from your **history** output and your **.bash_history** file.
If you want to make this setting permanent, you have to add it to your **.bashrc** file.
```
$ echo 'HISTIGNORE="history:cd:exit:ls:pwd:man"' >> .bashrc
```
This setting just means that when you look back through previously run commands, the list wont be cluttered by commands that you're unlikely to be looking for when you are looking through your command history.
### Remembering, ignoring and forgetting the past
Command history is useful because it helps you remember what commands youve recently used and reminds you about changes youve recently made. It also makes it easier to rerun commands, especially those with a string of arguments that you don't necessarily want to recreate. Tailoring your history settings can make your use of command history a little easier and more efficient.
Join the Network World communities on [Facebook][4] and [LinkedIn][5] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3537214/tweaking-history-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/iqeG5xA96M4
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.networkworld.com/blog/itaas-and-the-corporate-storage-technology/?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE22140&utm_content=sidebar (ITAAS and Corporate Storage Strategy)
[4]: https://www.facebook.com/NetworkWorld/
[5]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,189 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Tweaking history on Linux)
[#]: via: (https://www.networkworld.com/article/3537214/tweaking-history-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
在 Linux 上调整命令历史
======
> 在 Linux 系统上bash shell 的 history 命令可以方便地回顾和重用命令,但是你要控制它记住多少,忘记多少,有很多事情要做。
![](https://images.idgesg.net/images/article/2019/08/uk_united_kingdom_england_london_natural_history_museum_by_claudio_testa_cc0_via_unsplash_2400x1600-100808449-large.jpg)
Linux 系统中的 bash `history` 命令有助于记住你以前运行过的命令,并重复这些命令,而不必重新输入。
如果可以的话,你肯定会很高兴不用翻阅十几页的手册,每过一会再次列出你的文件,而是通过输入 `history` 查看以前运行的命令。在这篇文章中,我们将探讨如何让 `history` 命令记住你希望它记住的内容,并忘记那些可能没有什么“历史价值”的命令。
### 查看你的命令历史
要查看以前运行过的命令,你只需输入 `history`。你可能会看到一长串命令。记忆的命令数量取决于在 `~/.bashrc` 文件中设置的名为 `$HISTSIZE` 的环境变量,但是如果你想保存更多或更少的命令,你可以根据你的需要改变这个设置。
要查看历史记录,请使用 `history` 命令:
```
$ history
209 uname -v
210 date
211 man chage
...
```
要查看将显示的最大命令数量:
```
$ echo $HISTSIZE
500
```
你可以通过运行这样的命令来改变 `$HISTSIZE` 并使之永久化:
```
$ export HISTSIZE=1000
$ echo "HISTSIZE=1000" >> ~/.bashrc
```
在为你保留多少历史记录和当你输入 `history` 时显示多少历史记录之间也有区别。`$HISTSIZE` 变量控制显示多少历史记录,而 `$HISTFILESIZE` 变量控制在你的 `.bash_history` 文件中保留多少命令。
```
$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
2000
```
你可以通过计算历史文件中的行数来验证第二个变量:
```
$ wc -l .bash_history
2000 .bash_history
```
需要注意的是,在登录会话中输入的命令在注销前不会被添加到你的 `.bash_history` 文件中,尽管它们会立即显示在 `history` 命令输出中。
### 使用历史
有三种方法可以重发你在 `history` 中发现的命令。最简单的方法,特别是当你想重用的命令是最近运行的时候,通常是输入一个 `!` 后面跟上命令中足够多的首字母来唯一地识别它。
```
$ !u
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020
```
另一种简单的重复命令的方法是,只需按上箭头键,直到显示了该命令,然后按回车键。
另外,如果你运行 `history` 命令,并看到你想重新运行的命令被列出,你可以输入一个 `!` 后面跟着命令左边显示的序号。
```
$ !209
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020
```
### 隐藏历史
如果你想在一段时间内停止记录命令,你可以使用这个命令:
```
$ set +o history
```
当你输入 `history` 时,你输入的命令不会显示出来,当你退出会话或退出终端时,它们也不会被添加到你的 `.bash_history` 文件中。
要取消这个设置,使用 `set -o history`
要使它永久化,你可以把它添加到你的 `.bashrc` 文件中,尽管不使用命令历史记录通常不是一个好主意。
```
$ echo 'set +o history' >> ~/.bashrc
```
要暂时清除历史记录,这样在输入 `history` 时只显示之后输入的命令,可以使用 `history -c`(清除)命令:
```
$ history | tail -3
209 uname -v
210 date
211 man chage
$ history -c
$ history
1 history
```
注意:在输入 `history -c` 后输入的命令不会被添加到 `.bash_history` 文件中。
### 控制历史
许多系统上的 `history` 命令的设置会默认包括一个名为 `$HISTCONTROL` 的变量,以确保即使你连续运行同一命令七次,也只会被记住一次。它还可以确保你在首先输入一个或多个空格后跟着的命令将从你的命令历史记录中忽略。
```
$ grep HISTCONTROL .bashrc
HISTCONTROL=ignoreboth
```
`ignoreboth` 的意思是“忽略重复的命令和以空格开头的命令”。例如,如果你输入这些命令:
```
$ echo try this
$ date
$ date
$ date
$ pwd
$ history
```
你的 `history` 命令应该像这样报告:
```
$ history
$ echo try this
$ date
$ history
```
请注意,连续的 `date` 命令被缩减为一条,以空格缩进的命令被省略。
### 忽略历史
要忽略某些命令,使它们在你输入 `history` 时不会出现,也不会被添加到你的 `.bash_history` 文件中,可以使用 `$HISTIGNORE` 设置。例如:
```
$ export HISTIGNORE=”history:cd:exit:ls:pwd:man”
```
这个设置将导致所有的 `history`、`cd`、`exit`、`ls`、`pwd` 和 `man` 命令从你的 `history` 命令的输出和 `.bash_history` 文件中被忽略。
如果你想把这个设置变成永久性的,你必须把它添加到你的 `.bashrc` 文件中。
```
$ echo 'HISTIGNORE="history:cd:exit:ls:pwd:man"' >> .bashrc
```
这个设置只是意味着当你回看以前运行的命令时,列表不会被你在查看命令历史记录时不想看到的命令所干扰。
### 记住、忽略和忘记过去的命令
命令历史记录很有用,因为它可以帮助你记住最近使用过的命令,并提醒你最近所做的更改。它还可以让你更容易地重新运行命令,特别是那些有一串参数但你不一定想重新创建的命令。定制你的历史设置可以让你对命令历史的使用变得更容易,更有效率。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3537214/tweaking-history-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/iqeG5xA96M4
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.networkworld.com/blog/itaas-and-the-corporate-storage-technology/?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE22140&utm_content=sidebar (ITAAS and Corporate Storage Strategy)
[4]: https://www.facebook.com/NetworkWorld/
[5]: https://www.linkedin.com/company/network-world