Linux command line-the Terminal, like all shells, keeps a history of commands executed in the past by a user. This history is persistent and stays in the memory even when we reboot our system. These stored commands can be recalled and reused by us in the most optimal way to efficiently use the history saving feature of most shells.
The Linux bash comes with a very powerful command called “history”. This command is a built-in bash command, used to extract the history information about commands that have been executed by Linux users in all the previous sessions. In this tutorial, we will enable you to get the best out of your shell history by learning the proper usage of the history command. Linux stores the command history for a certain user in a file ~/.bash_history by default.
We will use the Terminal application of Linux for running the commands mentioned in this tutorial.
Viewing the Entire 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:
The history will be printed on the screen in the following default format:
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 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
You can see that my output now displays only the commands including my search keyword “ping”
Viewing a Number of Recent Commands
You can also view a specific number of commands, for example:
- N number of recent commands
- N number of oldest commands
N Number of Recent Commands
You can view a specific number of recently run commands through the following command:
$ history | tail -n
In this example, I want to view the last 3 commands that I ran, with the keyword ‘ping’ :
$ history | grep ping |tail -3
The output only displays the 3 recent ping commands instead of the entire history
N Number of Oldest Commands
You can view a specific number of the oldest run commands through the following command:
$ history | head -n
In this example, I want to view the oldest 3 commands that I ran, with the keyword ‘ping’ :
$ history | grep ping |head -3
The output only displays the 3 oldest ping commands instead of the entire history
List All 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.|
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|
In this example, I pressed ctrl+R and then provided the search string ‘ar’. The history displayed the command ‘arch’.
When I ran the command ‘arch’ by pressing ctrl+O, it displayed the CPU architecture of my system:
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:
Here I am running the command number 95 from my history:
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:
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:
- In order to search for a command from history and then run it, use the following command:
I will use the following command to re-run the last command with the keyword ‘dconf’ in it:
You can re-confirm in the following image that it was the last dconf command I had run.
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.
In this example, I will create a file with a long name.
I will then copy it to another location by avoiding to type the long name again as follows:
Cp !$ [destinationfolder]
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] !^
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 !^
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]:#
$ echo !cp:2
This command will take the following parameters:
#:2(second argument of the command)
Following output shows the 2nd argument of the last cp command I ran:
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]:*
In the following command, I will print all the arguments of the last cp command by using the echo command as follows:
$ echo ![cp]:*
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:
I mistyped the command ‘grep’ as ‘gep’ and now I want to run the command by correcting the text:
Now you can see that my corrected command will run properly as follows:
In order to erase the history of your bash, use the following command:
$ history -c
This command will clear your history and remove all contents from the file ~/.bash_history
In this tutorial we have learned that while printing the entire history contents might not be that useful, optimizing the output through the commands we learned is the real game changer. Practicing along with this article will enable you to view the exact commands you want to, re-run and re-use those commands, and also use arguments from previously run commands to optimize new ones.