Search anything:

history command in Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

The history command is used to view previously executed commands in the Linux terminal. In this article we discuss the commonly used history commands.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Commands.
  4. Summary.
  5. References.


The history command is used to view previously executed command in the Linux terminal. This allows us to avoid mistyping or having to remember very long complex commands which we had previously executed.

We can also use the up/down , Ctrl+n/Ctrl+p to scroll through bash history.
Command history is stored in a file .bash_history file in the home directory, you can view it by writing, cat ~/.bash_history command.
By default the history command will display the last 500 commands from the older to the most recent(highest numbers).

The history command will treat each executed command as a separate event then it associates each event with a number used to recall or modify the command when needed.

History command in Linux is used by Software Developers to:

  • Quickly find a command they have used for a particular task.
  • Reuse command instead of typing it again.


The syntax is as follows,


Various options include,
-c, to clear the history list by deleting all entries.
-d, deletes history entry at position OFFSET.
-a, to append history lines from current session to the history file.
-n, to read all history lines that are not already read from the history file
and append them to the history list.
-r, to read the history file and append contents to history.
-w, to write current history to history file.
-p, performs history expansion.
-s, appends ARGs to the history list as a single entry.


You can view command history by typing,


without any options.

The output will print out all previously used commands with the most recent at the bottom of the list, We can get more information such as the exact times these command were executed by doing the following,
First we create a shell variable HISTTIMEFORMAT

Secondly we define is as follows,


Thirdly we add it to ~/.bash_profile as follows,

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

Where %d, %m, %y, %T represents the day, month, year and time respectively.
Then we load it to the current shell as follows,

. ~/.bash_profile

Now when we run history command, the commands are accompanied by a date and time.

508  28/01/22 01:59:03 cat ~/.bash_history
509  28/01/22 02:05:26 HISTTIMEFORMAT="%d/%m/%y %T "

To display the nth command in history we write,


The above command will execute the 100th command.

To execute the most recent command, we write,


Useful when we try to execute a command without using sudo privileges, the next time we type sudo !! and this runs the previous command with sudo privileges.

We can also display the last n commands by writing,

history 10

This command will display the last 10 commands.

We can also display a command by its name by writing,


The command will display the most recent use of ping command.

To display command history by specifying its search pattern we use a text filtering command grep combined with history as follows,

history | grep pi

The output is all matches of the pattern pi, that is all commands with the matched pattern.

We can also perform a search for a previously executed command by using the Ctrl+r which results in a prompt,


Which we can use to search for commands.

To change the most recent command(history | grep pi) to history | grep ls we can write,


Now the output is all matching patterns of ls from history command.

To save the current session history we use the -a option,

history -a

Bash history is not written to .bash_history file until one logs off and thereby this can make it difficult to utilize this file for scripting.

We solve this by using the -w option to write the history to the history file.

history -w

To delete history we use the -d option as follows,

history -d 100

After this command is executed, the 100th command is deleted.

We can also remove a range of lines as follows,

history -d 100 120

The above command deletes history lines from the 100th line to the 120th line.

The -c option is used to delete the entire history,

history -c


Command history is stored in the .bash_history file in the ~ home directory. Since it is hidden(preceded by a .) we use ls -a command to list it.


  1. history --help command and history man pages.(man history).
history command in Linux
Share this