The Linux command line – the terminal – stores history of commands executed in the past by a user. This history is persistent and remains in memory even if we reboot our system. We can retrieve and reuse these stored commands to make the most of the history storage feature of the bash shell.

The Linux bash has a very powerful command called “history”. This command is a built-in bash command that is used to extract history information about commands that have been executed by Linux users in all previous sessions. In this tutorial, we will enable you to get the most out of your shell history by learning the proper use of the history command. Linux stores the command history for a particular user in a ~/.bash_history file by default.

We will use the Linux terminal application to execute the commands mentioned in this tutorial.

Viewing the Entire Bash History

A user can view the entire history for his/her shell commands from the history file saved exclusively for that user by using the following simple command:

$ history

The history will be printed on the screen in the following default format:

Getting the most out of Linux Bash history command shell

In the above image, you can see a list of all commands with a number assigned to each of them. The number 1 command corresponds to the first command you ran and the last numbered command represents the latest command you executed.

If you have been running a lot of commands since using the bash, you will observe that there will be hundreds and even thousands of those commands displayed here. To access a few relevant commands, you can filter the results displayed by the history feature. You can also customize the history command to show a specific number of commands for you.

Filtering the Bash History Output

If you want to filter your history output based on a specific keyword that you might have used in the previous commands, you can use the history command as follows:

$ history | grep [keyword]

Example: In this example, I want to view only the ping commands I have ever run in the shell. Therefore, I will use the following command:

$ history | grep ping

Getting the most out of Linux Bash history command shell

You can see that my output now displays only the commands including my search keyword “ping”

Viewing a Number of recently executed shell commands

You can also view a specific number of commands, for example:

  • N number of recent commands

OR

  • N number of oldest commands

N Number of Recent Bash Commands

You can view a specific number of recently run commands through the following command:

$ history | tail -n

Example:

In this example, I want to view the last 3 commands that I ran, with the keyword ‘ping’ :

$ history | grep ping |tail -3

Getting the most out of Linux Bash history command shell

The output only displays the 3 recent ping commands instead of the entire history

N Number of Oldest Bash Commands

You can view a specific number of the oldest run commands through the following command:

$ history | head -n

Example:

In this example, I want to view the oldest 3 commands that I ran, with the keyword ‘ping’ :

$ history | grep ping |head -3

Getting the most out of Linux Bash history command shell

The output only displays the 3 oldest ping commands instead of the entire history

List All Bash Commands with Date and Timestamp

If you wish to view your command history along with the date and timestamp, please use the following export command:

$ export HISTTIMEFORMAT='%F, %T '

Now when you wish to view the history, just run the history command to see the output in the following format:

Please note that this change in format is temporary and it will be restored to the previous standard when you close the session.

Navigating Commands from History

While you are on the command line, you can navigate through previously run commands and also recall some recently used commands by providing a keyword.

Scrolling through Commands

You can use the following keyboard controls to navigate through history commands

Up Arrow/Ctrl P Using these controls, you can display the previous command you used. You can hit these controls multiple times to go back to a specific command in history.
Down Arrow/Ctrl N Using these controls, you can display the next command you used. You can hit these controls multiple times to move forward to a specific command in history.
Alt R If you edit a command, that you have pulled from the history, on a current line, you can use this control to revert it to the original command.

Recalling Commands

You can recall, run or choose not to run a specific command from the history, using the following controls:

Ctrl R Using this control, you can recall a command from the history by specifying a search string.
Ctrl O Using this control, you can run the command you recalled through Ctrl R
Ctrl G Using this control, you can exit history without running the command you recalled through Ctrl R

Example:

In this example, I pressed ctrl R and then provided the search string ‘ar’. The history displayed the command ‘arch’.

Getting the most out of Linux Bash history command shell

When I ran the command ‘arch’ by pressing ctrl O, it displayed the CPU architecture of my system:

Getting the most out of Linux Bash history command shell

Utilizing the Bash History

The real magic of the bash history is by using various commands and customizing the history command itself to make the best use of the history feature:

Running Commands from History

  • As we have seen before, the history command displays the commands by associating a number with each. By using the following command, you can run a certain command from history, based on its number:
$ !#

Example:

Here I am running the command number 95 from my history:

$ !95

Getting the most out of Linux Bash history command shell

The output displays the result of ‘lscpu’ which was listed as command number 95 in my bash history. This command displays my CPU and operating system specifications.

  • You can re-run your previous command by using the following command:
$ !!

Example:

Here you can see that I ran the command ‘arch’. By running the above-mentioned command, I can re-run the ‘arch’ command to view my CPU architecture as follows:

Getting the most out of Linux Bash history command shell

  • In order to search for a command from history and then run it, use the following command:
$ ![keyword]

Example:

I will use the following command to re-run the last command with the keyword ‘dconf’ in it:

$ !dconf

Getting the most out of Linux Bash history command shell

You can re-confirm in the following image that it was the last dconf command I had run.

Getting the most out of Linux Bash history command shell

Using Arguments from Previous Commands

Linux bash enables you to run a new command by using arguments from the previous command. It is especially helpful when you want to avoid retyping long or complicated arguments. You can use the following command for this purpose:

Using the last argument from the previous command

$ [Command] !$

The variable !$ has the last argument from the previous command stored in it.

Example:

In this example, I will create a file with a long name.

Nano [longnameOfaFile]

I will then copy it to another location by avoiding to type the long name again as follows:

Cp !$ [destinationfolder]

Getting the most out of Linux Bash history command shell

You can see that I was able to copy a file without retyping the filename by using the variable !$

Using the first argument from the previous command

Sometimes, only the first argument from the previous command is helpful in the current one.

In that case, you can use the following command:

$ [command] !^

Example:

In the following example, I ran the ping command by specifying the hostname and a count as arguments. Now I want to use the hostname(first argument) rather than 2(the last argument) in my new command. I will use the following command for this purpose:

$ ping !^

Getting the most out of Linux Bash history command shell

Using all the arguments from the previous command

You can also use all the arguments from the previous command by using the following command:

$ !*

Using one argument from a history command

You can also use commands from your history by re-using their arguments. Use the following command for this purpose:

$ [command] ![keyword]:#

Example:

$ echo !cp:2

This command will take the following parameters:

command: echo

Keyword:cp

#:2(second argument of the command)

Following output shows the 2nd argument of the last cp command I ran:

Getting the most out of Linux Bash history command shell

Using all arguments from a history command

The following command will store all the arguments from the searched history command and use them in the current command:

$ command ![keyword]:*

Example:

In the following command, I will print all the arguments of the last cp command by using the echo command as follows:

$ echo ![cp]:*

Getting the most out of Linux Bash history command shell

Modifying and Running Previous Commands

If you mistyped a command or you want to re-run a command by changing some text in it; following is the way to do so:

$ ^[previoustext]^[newtext]

Example:

I mistyped the command ‘grep’ as ‘gep’ and now I want to run the command by correcting the text:

$ ^gep^grep

Now you can see that my corrected command will run properly as follows:

Getting the most out of Linux Bash history command shell

Clearing History

In order to erase the history of your bash, use the following command:

$ history -c

This command clears your history and removes all contents from the ~/.bash_history file.

In this tutorial, we learned that while printing out the entire history contents is not that useful, optimizing the output with the commands we learned is the real game-changer. By practicing with this article, you will be able to display exactly the commands you want, re-execute and re-use those commands, and also use arguments from previously executed commands to optimize new commands.