How to view and clear console history on Linux

In this article I will give examples of viewing and clearing the history of entered commands in the Linux terminal.

View the full history of entered commands and the last 10 entered commands:

history
history 10

To re-execute the command from the history, you can type:

!command_number

To re-execute the previous one:
!!

To re-execute the penultimate one:

!-2

To execute a previously entered command starting with the letters ixnfo:

!ixnfo

To execute a previously entered command containing the letters ixnfo:

!?ixnfo?

View the very first 10 entered commands:

head -n 10 ~/.bash_history

View the last 10 entered commands:

tail -f -n 10 ~/.bash_history

Counting the number of lines in a file:

wc -l ~/.bash_history

Settings for storing the history of entered commands are stored in the ~/.bashrc file, for example:
HISTFILE – path to the history file
HISTSIZE – the number of lines stored in the interpreter memory
HISTFILESIZE – the number of commands stored in the history file
etc.
You can also view a specific parameter by typing “echo PARAMETER” in the console, for example:

echo $HISTFILE

Clearing the history of entered commands:

history -c

Removing from the history of a specific command entered:

history -d COMMAND_NUMBER

Append the commands entered in the current session to the end of the file:

history -a

Disabling the history of entered commands:

set +o history

Enabling the history of entered commands:

set -o history

See also my article:
Linux User Administration

Leave a comment

Leave a Reply