How to use the history command in Linux

As you spend more and more time in Terminal sessions, you will constantly find new commands that make everyday tasks more efficient. The GNU history command is one of them.

The history command holds a list of all the other commands that have been run since that Terminal session, then allows you to view or reuse those commands instead of re-entering them. If you are an experienced Terminal user, you will know the power of the history command. Let's find out more details through the following article!

See a list of the commands used

To see how the history command works, open a Terminal and type:

$ history

The response you receive should be something like this:

1 clear 2 ls -al 3 sudo dnf update -y 4 history

Picture 1 of How to use the history command in Linux
The history command holds a list of all other commands that have been run since the Terminal session

The history command shows you a list of commands entered since you started the session. The advantage is that you can now re-execute any option using the command:

$ !3

Command ! 3 at the prompt tells the shell to re-run the command on line 3 of the history list. You can also access that command by typing:

$ !sudo dnf

This will prompt the history to find the closest command matching the pattern you provided (in this case, that template is dnf ) and run the command.

Execute used commands

You can also use history to re-run the last command entered by typing !!. By pairing it with grep , you can either search for commands that match a sample of the text, or by using it with tail , you can find a couple of commands that are closest to you that you have executed. For example:

$ history | grep dnf 3 sudo dnf update -y 5 history | grep dnf $ history | tail -n 3 4 history 5 history | grep dnf 6 history | tail -n 3

Another way to access this search is to type Ctrl + R to invoke a recursive search for the command history. Once imported, the prompt will change to:

(reverse-i-search)`':

Now you can start typing a command, and the matching commands will be displayed for you to execute by pressing Return or Enter.

Executed command changes

You can also use history to re-run a command with a different syntax. You can edit history with the history command. For example, if you want to change the previous command history history | grep dnf to history | grep ssh , you can execute the following command at the prompt:

$ ^dnf^ssh^

The command is run again, but with dnf replaced with ssh. In other words, the command to be run is:

$ history | grep ssh

Clear history

There may be times when you want to delete some or all of the commands in your history file. If you want to remove a specific command, enter:

history -d row_number=""

To delete the entire contents of the history file, execute:

history -c

The history file is also stored in a file that you can modify. The Bash shell user found it in his home directory as .bash_history.

Some other uses

There are a few other things you can do with the history command:

  1. Sets the size of the history buffer to a certain number of instructions
  2. Record the date and time for each line in history
  3. Prevent some commands from being recorded in history
Update 06 October 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile