Detailed Explanation of Linux Adjustment Command History Method

  • 2021-08-31 09:55:38
  • OfStack

The bash history command in the Linux system helps you remember the commands you have run before and repeat them without having to re-enter them.

If you can, you'll be glad not to flip through the 10-plus pages of the manual and list your files again every 1, but to check the previously run commands by typing history. In this article, we will explore how to make the history command remember what you want it to remember, and forget commands that may have little "historical value".

View your command history

To see the commands that have been run before, you only need to type history. You may see a long list of commands. The number of commands memorized depends on the environment variable named $HISTSIZE set in the ~/. bashrc file, but if you want to save more or fewer commands, you can change this setting to suit your needs.

To view the 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 it permanent by running this command:

$ export HISTSIZE=1000
$ echo "HISTSIZE=1000" > > ~/.bashrc

There is also a difference between how much history is kept for you and how much history is displayed when you type history. The $HISTSIZE variable controls how much history is displayed, while the $HISTFILESIZE variable controls how many commands are kept in your. bash_history file.

$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
2000

You can verify the second variable by counting the number of lines in the history file:

$ wc -l .bash_history
2000 .bash_history

It should be noted that the commands entered during the login session are not added to your. bash_history file until you log out, although they are immediately displayed in the history command output.

Usage history

There are 3 ways to resend commands you find in history. The easiest way, especially if the command you want to reuse is recently run, is usually to type 1! Follow it with enough initials in the command to recognize it only 1.

$ !u
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020

Another simple way to repeat a command is to simply press the up arrow key until the command is displayed, and then press Enter.

In addition, if you run the history command and see that the command you want to rerun is listed, you can enter 1! Followed by the sequence number displayed on the left side of the command.

$ !209
uname -v
#37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020

Hide history

If you want to stop recording commands for 1 period of time, you can use this command:

$ set +o history

When you enter history, the commands you enter will not be displayed, nor will they be added to your. bash_history file when you exit the session or exit the terminal.

To cancel this setting, use set-o history

To make it permanent, you can add it to your. bashrc file, although not using command history is usually not a good idea.

$ echo 'set +o history' >> ~/.bashrc

To temporarily clear the history so that when you enter history, only the commands entered later are displayed, you can use the history-c (clear) command:

$ history | tail -3
209 uname -v
210 date
211 man chage
$ history -c
$ history
1 history

Note: Commands entered after entering history-c will not be added to the. bash_history file.

Control history

The history command settings on many systems include a variable named $HISTCONTROL by default to ensure 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 follow you by entering one or more spaces will be ignored from your command history.

$ grep HISTCONTROL .bashrc
HISTCONTROL=ignoreboth

ignoreboth means "ignore repetitive commands and commands that begin with spaces." For example, if you type these commands:

$ echo try this
$ date
$ date
$ date
$ pwd
$ history

Your history command should report like this:

$ history
$ echo try this
$ date
$ history

Note that successive date commands are reduced to one, and commands indented with spaces are omitted.

Ignore history

To ignore certain commands so that they do not appear when you type history and are not 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 ignored from the output of your history command and from the. bash_history file.

If you want to make this setting permanent, you must add it to your. bashrc file.

$ echo 'HISTIGNORE="history:cd:exit:ls:pwd:man"' >> .bashrc

This setting simply means that when you look back at previously run commands, the list will not be disturbed by commands that you do not want to see when you look at the command history.

Remember, ignore and forget the commands of the past

Command history is useful because it helps you remember the commands you have recently used and reminds you of the changes you have made recently. It also makes it easier to rerun commands, especially those that have 1 string of arguments but you don't necessarily want to recreate. Customizing your history settings can make your use of command history easier and more efficient.


Related articles: