You might know the basic use of history command, but it can do a lot more than that. Bash history is usually stored in the file
/.bash_history. It enables you to recall and reuse the stored record in an efficient way to get the best out of bash history saving feature. Not only this, but you can also customize and control the bash command output in the way you want.
In this article, we will explain how to effectively use and customize the bash command history to get the most out of its features.
We have used Debian 10 for running the commands and procedure mentioned in this article.
Using bash command history
1. Viewing the bash History
To view the entire history of shell commands, you can run the following command in Terminal:
It will list the entire history for a specific user from the history file stored specifically for that user. You will see all the commands starting with a number allocated to each of them. It will list the older commands at the top starting with number 1 and the newer commands at the bottom.
2. Searching the History Output
You can also search for a specific keyword from the history output. Pair the history command with grep and a specific keyword to search for commands that match your specified keyword as follows:
For instance, to list all the commands that include the keyword “find”, the command would be:
3. Viewing last n commands
The history command by default lists the last 1000 number of commands executed by a user. In case, you want to list only a specific number let’s say n number of last executed command, run the following command in Terminal:
For instance, to list the last 7 executed commands, the command would be:
To view the number of the last n run commands that includes a specific keyword, you can use the following syntax:
An example of this would be to view the last 4 executed commands with the keyword “java”.
4. Viewing oldest commands
To view the oldest n number of commands, you can use the following syntax in Terminal:
To view the oldest n number of commands that includes a specific keyword, use the following syntax:
An example of this would be to view the oldest 4 executed commands with the keyword “java”.
5. Clear Bash history completely
To remove the entire bash history, run the following command in Terminal:
Customizing bash command history
To customize bash command history, we will have to make changes in the
/.bashrc file. To edit the
/.bashrc file, use the following command:
Once you are done with modifying the file, use Ctrl+O and Ctrl+X to save and close the nano editor.
Then run the following command to apply the modifications:
1. Add Date and Timestamp to Bash History
If you want to display date and timestamp along with the command history, you can do that by adding the following line in
Now run the history command and it will show the command history with corresponding data and timestamp.
2. Increasing size of Bash History
Bash by default keeps 500 commands in the history list. However, we can change this value using the HISTSIZE value.
To view the current size of bash history, run the following command in Terminal:
Similarly, the default size of the bash history file is 500. It is the maximum number of entries that are contained in the history file.
To increase the size of bash history let’s say 10000, add the following lines in
To verify if the bash history size has changed successfully, run the following commands in Terminal:
3. Append Bash Commands to History File
When a bash session is closed, you can choose to whether overwrite or append the commands in the history file using the histappend variable. To view the current settings, run the following command in Terminal:
The “on” in the output shows histappend option is enabled and the commands will be appended in the history file instead of overwriting. While “off” shows, histappend option is disabled and the file will be overwritten.
/.bashrc file and:
Add following line, if you want to append the commands to the history file instead of overwriting:
Or add the following line, if you want to disable the append option and want to overwrite the file on exit:
4. Store Bash History Immediately
Bash by default only saves the session to the bash history file once the session ends. To change this default behavior and make it to instantly save each command you have executed, you can make use of PROMPT_COMMAND.
/.bashrc file and add the following line:
Now whenever you execute any command, it will be immediately added to the history file.
5. Control Bash History
We can control the way bash saves our command history through the HISTCONTROL variable. We can specify it to ignore duplicate entries, and/or to ignore entries with leading white spaces.
- ignorespace – eliminates commands that begin with a space history list.
- ignoredups – eliminate duplicate commands.
- ignoreboth – Enable both ignoredups and ignorespace
- erasedups- eliminate duplicates from the whole list
To apply these functions, open the
/.bashrc and add the following line with values separated by the colon as follows:
6. Ignore Specific Commands
We can also control which commands to ignore in history using a variable HISTIGNORE. It is a colon-separated list of patterns in which we can specify all the commands that we want to ignore from history.
For instance, if we do not want to list the basic commands such as history, ls, pwd commands in the history list, then we add the following line in the
With Linux bash command history, you can do a lot more than just repeating the old commands. In this article, we have learned how to use the bash history to view the commands that have executed previously and also learned to control the way bash saving the command history.
About the author
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.