How To Use Linux Bash History Commands
While working on Linux, you spend most of the time on command line terminal. Mostly you use bash shell, which is default in most of the Linux distribution.
Also while working on terminal, you repeat most of the commands. Typing command on terminal is a good practice for the beginners.
Linux bash shell provides awesome history command. Which captures the history of the command run on terminal.
In this article, we will see how to use history feature provided by bash shell.
Setting History Defaults
Before using the history, its a good practice to adjust some bash settings to make it more useful.
HISTFILESIZE: Parameter configures how many commands are kept in the history file.
HISTSIZE: Controls the number stored in memory for the current session.
You can set reasonable cap for the size of history in memory for the current session, and have an even larger history saved to disk that you can examine at a later time.
Open your ~/.bashrc
file with your favirout editor to change these settings:
vim ~/.bashrc
If we want to have bash immediately add commands to our history instead of waiting for the end of each session (to enable commands in one terminal to be instantly be available in another), we can also set or append the history -a
command to the PROMPT_COMMAND
parameter, which contains commands that are executed before each new command prompt.
To do this correctly, we need to do a bit of a hack. We need to append to the history file immediately with history -a
, clear the current history in our session with history -c
, and then read the history file that we’ve appended to, back into our session history with history -r
.
You can add below line of code in ~/.bashrc
file.
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
To implement your changes immediatly, either re-loging to the system or source the file as given below.
source ~/.bashrc
Review your previous bash history
We use hostory
command to review the bash command history. This command will print out our recent commands (one command per line).
history
Output:
. . .
43 man bash
44 man fc
45 man bash
46 fc -l -10
47 history
48 ls -a
49 vim .bash_history
50 history
51 man vim
52 history 10
53 history
It also prints the history number for each command. Each command is associated with a number for easy reference. You will see why this is useful in a moment.
See only last 5 commands
You can use following command to print only last 5 commands:
history 5
Find a certain string in history command
Use grep
to filter command history. Lets take an example of finding command contains cd
command, as below:
history | grep cd
Output:
. . .
37 cd ..
39 cd Desktop/
61 cd /usr/bin/
68 cd
83 cd /etc/
Executing Commands from your Bash History
We can re-execute any of our history command by its number preceded by an exclamation poit (!).
For example: from my history command, if I want to re-execute man vim
command then I will simpley use below command:
!51
Above command will recall and execute the command associated with the history number 51.
2,778 total views, 1 views today