Making use of command history on Linux

How many commands are saved?

You can control the number of commands that will be saved in your history file. The default is usually 1000. To confirm the length of your command history, type the command shown below.

$ echo $HISTSIZE
1000

To change this to a different size, add a line to the bottom of your .bashrc with a command like those shown below that change the number of commands to be saved to 2,000. The 4th line sources the .bashrc file to adopt the change.

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

You can change the size if you find you’re saving too many or too few commands.

To change the default setting for $HISTSIZE, use root privilege and change the setting in /etc/profile. Note, however, that this will not change the setting for existing accounts.

Avoid saving duplicate commands

To avoid saving a command multiple times when entered some number of times in a row, use the HISTCONTROL option to ignore duplicates. To check your current settings, use these commands:

$ echo $HISTFILE
/home/justme/.bash_history
$ echo $HISTSIZE
1000
$ echo $HISTCONTROL
ignoredups

The settings shown display the name and location of the history file, the number of commands to be saved and that the ignoredups setting is already set up. If you type the whoami command three times in a row and then run the command shown below, you will only see the whoami command listed once.



Source link

Leave a Comment