How to use the history command on CentOS 8

s2 0

The ‘history’ command is used to view the terminal history. It saves the history of all the terminal commands that are executed on your system. It also allows users to repeat or reuse previously executed commands on the terminal without having to re-enter them all. So the history command is useful when the user has forgotten a command that was previously executed on the terminal. The history of all executed commands is saved in the file ~ / .bash_history. By default, the history file saves a record of all commands executed on the Linux system. If you are having trouble checking the history of all the commands you previously ran, this article will help you. In this article we are going to 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 symbol on the desktop. To check the history of your terminal, you need to log in with the root user. To do this, enter the following command.

$su

Now log in as the root user.

To view your terminal’s history, use the following different commands:

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

$history

If this command is successfully executed, the history of all executed commands is displayed. The last command executed is displayed from top to bottom in the terminal.

When you run the ‘cat ~ / .bash_history’ command, it is 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. The output shows the last lines on the terminal.

$history 7

As mentioned above, the ‘history’ command is used to display all executed commands with their line numbers. You can repeat certain 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 you wish.

Rerun command from history

Note: The command line number always changes when we enter further commands on the terminal. So do not rely on the previous history command line number.

You can also see the latest commands on the terminal by typing ‘!!’ input. or press the up arrow key on your system keyboard to view recent commands.

Run the last command again

You can repeat the command based on its start string. Enter the starting string of the previously executed command with ‘!’ a. Follow the syntax given below:

!string

For example, to check the commands that begin with the string “yum”, enter the following command at the terminal:

!yum

The above method can be at risk if the last command you ran is different from the one you need. Enter ‘: p’ at the end of the start string for confirmation instead of executing directly on the start string.

Search for commands in bash history

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

You can use the command ‘history’ by coupling it with grep, it looks for the commands that match the specific text pattern or the command end. This allows you to search through the most recently executed commands.

Enter the following command on the terminal:

$history | grep dnf

Use grep to search for a command

As you can see, the list of all commands executed on the terminal is returned, which were compared with the ‘dnf’ text.

You can write commands to the history file. If you have no further history of the logged file, 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 should be noted that clearing the history will only erase the contents from memory. You can also delete the ~ / .bash_history file to remove the history, but the current session will still be saved to the file when the user logs out.

Instead of deleting the entire history, you can also delete the specific line of numbers from the history file using the -d option. Use the following command to remove the specific line of numbers from the history file

$history -d 457

For example, if you have entered your account password, it is recommended that you use the above command for security reasons. This command allows users to secure information from unauthorized access.

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

$history -a

You can also add the timestamps to the history lines. This means that by default, you won’t be able to see the date and time on previously executed commands, except just in order. So, you can use the timestamp command to view the date and time of the commands that were executed. Run the following command on the terminal:

$HISTTIMEFORMST=”%F%T ”

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

$HISTSIZE=10000

diploma

In this tutorial, we have explored how to use various functions of the history command with the terminal in CentOS 8.0. Following this tutorial, you are now ready to deal with the history of your CentOS 8.0 terminal. I hope you find this article useful. For further questions please do comments below.

How to use the history command on CentOS 8