How to use the history command on CentOS 8

The 'history' command is used to display the terminal history. It keeps the history of all terminal commands executed on your system. It also allows users to replay or reuse previously executed commands on the terminal without having to type them all again. So the History command is useful in the situation when the user has forgotten a command that was previously executed on the terminal. The history of all executed commands is stored in the file ~/.bash_history. By default, the history file stores the record of all executed commands on the Linux system. If you have difficulties to check the history of all previously executed commands, this article would be useful for you. In this article we show you how to use the "history" command on your CentOS 8.0.

How to use the history command

Open the terminal window of your CentOS 8.0 by clicking on the terminal icon present on the desktop. To check the history of your terminal, you need to be login with the root user. For this purpose, type the following command.

$su

Now you are login as a root user.

To see the history of your terminal, use the following different commands:

To print the history of all recent and old executed commands, type the following command in CentOS 8.0 terminal

$history

On successful execution of this command, it will show the history of all executed commands. The most recent executed command will display from the top towards the bottom of the terminal.

The History Command

If you run ‘cat ~/.bash_history’ command then it would be quite similar to the history file but does not show the exact formatting or line numbers.

You can also specify a number with a history command. In the output, the recent lines will be shown on the terminal.

$history 7
Limit history command output to a certain number of results

As mentioned above, the ‘history’ command is used to display all executed commands with its line numbers. You can repeat specific commands on the terminal by using the command number.

!command-line-number!

For example, I want to run the command of line number 416 again, use this command:

$ !416!

You can change the line number as per your requirements.

Run command from history again

Note: The command line number always changes as we type more commands on the terminal. So, don’t rely on the previous history command line number.

You can also display the most recent commands on the terminal by typing ‘!!’ or press ‘up’ arrow key present on your system’s keyboard to view the most recently executed commands.

Run most recent command again

You can repeat the command by its starting string. Type the starting string of the previously executed command with ‘!’. Follow the syntax that is given below:

!string

For example, you want to check the commands that start with the  string “yum”, then you will type the following command on the terminal:

!yum

The above-mentioned method may be a risk if the last executed command is different from what you need. Type ‘:p’ at the end of the starting string for confirmation instead of directly run with the starting string.

Search for commands in bash history

The above-mentioned ‘!yum:p’ command only displays the command for confirmation.

You can use ‘history’ command with pairing it with grep, it will search the commands that will match the specific text pattern or command tail. By this method, you will be able to search the last few executed commands.

Type the following command on the terminal:

$history | grep dnf

Use grep to search for a command

As you can see the list of all executed commands will be returned on the terminal that was matched with ‘dnf’ text.

You can write commands into the history file. If you have no more history on logged file then run the following command on the terminal.

$history -w

You can also clear the contents of the history. Run the following command to clear the contents:

$history -c

It is noted that if you will clear history then this will only clear contents from the memory. You can also delete the ~/.bash_history file to remove the history, but the current session will be still saved into the file on user logout.

Instead of clearing all the history, you can also delete the specific number line from the history file with -d option. Use the following command to remove the specific number line from the history file

$history -d 457

For example, if you have typed your account password then it is recommended to use the above-mentioned command for security purposes. Users can secure information through unauthorized access by using this command.

You can also save the history of the current session by ‘-a’ option to ~/.bash_history file. Type the following command on the terminal:

$history -a

You can also add the timestamps to the history lines. It means that by default, you can not see the date and time with previously executed commands except just order. So, by using timestamp command you can view the date and time of the executed commands. Run the following command on the terminal:

$HISTTIMEFORMST=”%F%T ”

You can also change the history stored line size.  By default, the bash file keeps 1000 command lines in the history list. The $HISTSIZE is a variable that allows you to change value according to your needs. Type the following command to set it 10000 to increase the size.

$HISTSIZE=10000

Conclusion

In this tutorial, we have explored how to use different features of history command using the terminal in CentOS 8.0. After this tutorial, now you are able to deal with your CentOS 8.0 terminal’s history. I hope this article would be useful for you. For more queries please do comments below.