Built right into your command shell is a simple record of the commands that you have recently used. This command history can be quite useful as you try to figure out how you did something amazing or perhaps a mistake. Reviewing the history can be really useful. Your history can also be a security issue if somehow you have sensitive information recorded in your history and your account is compromised. Overall, I am confident that you will find “history” to be an excellent tool!
Show Your Full Command History
Just typing the command “history” will print to the screen all of your recorded history.
$ history 1 pwd 2 ls 3 ping 10.2.1.42 ... 15 history 16 cat /etc/hosts 17 df -h 18 man ls 19 pwd 20 mkdir mydir1 21 cd mydir1 22 touch file1 23 history $
You can also specify how many lines of history you would like to have output by specifying the commands you wish to see after the history command
$ history 5 22 touch file1 23 df -h 24 cd mydir1 25 pwd 26 history 5
Running Commands From Bash History
Of course, you could always copy and paste the commands from your history display to run them again. But you can also do it the way it was designed to be run. You can rerun the last command using !!
Example: Run the last bash command from your bash history using !!. In this case, the last command is “pwd”.
$ !! pwd /root/mydir1
Rerunning command from bash history can be done by using an exclamation mark (!) and specifying the command number that you wish to rerun.
$ !25 pwd /root/mydir1
You can also add additional text to your command.
Example: Find a command from bash history –in this case, command 22– and run it again but with additional text.
$ history ... 22 touch file1 ... $ !22 file2 touch file1 file2
ReRun the Last Instance of a Command Containing a Specific String from Bash History
Let’s imagine that you know that the command you want to rerun from your history contains some text that you want to search on. You can run it instantly by typing !<string>. You need to be really sure that you know what you are doing when you do this or you could run the wrong command. Remember that this will run the last instance of whatever command contains that particular text string.
Example: Rerun the last entry in bash command history containing the string “touch”
$ !touch touch file1 file2
Editing Command Line History
Let’s imagine that we want to slightly alter a command that we recently ran. We can alter the bash command history.
Example: I run an ls command with specific options. Then I run the command again from command history but alter the command using s/alth/l.
$ ls -alth total 8.0K dr-xr-x--- 9 jeff jeff 4.0K Jun 15 03:12 .. drwxr-xr-x 2 jeff jeff 4.0K Jun 15 01:51 . -rw-r--r-- 1 jeff jeff 0 Jun 15 01:51 target.txt -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file1 -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file2 -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file3 $ !ls:s/alth/l ls -l total 0 -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file1 -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file2 -rw-r--r-- 1 jeff jeff 0 May 29 02:53 file3 -rw-r--r-- 1 jeff jeff 0 Jun 15 01:51 target.txt
Edit Commands from Bash History with an Editor
Edit a Command from bash history using the vi editor with FC command.
By changing your FCEDIT variable, you can change your FC editor to vi, vim, emacs or gedit for example.
Example: I edit command 1010 and add a www. to the front of the domain name.
1010 dig uptimemadeeasy.com ... $ fc 1010 dig www.uptimemadeeasy.com
Example: We specify the editor that we want to use with the -e option to edit command #35 from our bash history, then we run the command.
$ history ... 35 touch file1 file2 ... $ fc -e vi 35 touch file1 file2 file3
Search Through Bash Command History
While you can easily run history and pipe it to grep on something you are searching on, there are other options specifically built for searching the bash command line history. The first option is to use the up and down arrows to scroll through the past commands one by one.
Another option is to use <ctrl+r> directly from your bash shell. After typing <ctrl+r>, type in the string that you are searching for. It will show you the most recent command instance containing that string. If it doesn’t show you the command that you are looking for, type <ctrl+r> again and again and it roll through all of the instances of the string. Once you find the command that you want to run, hit the enter key and it will run the command for you.
Bash Command Line History Variables
HISTFILE – Shows the path and filename of your history file.
$ echo $HISTFILE /home/jeff/.bash_history
HISTFILESIZE – Shows the max number of commands stored in your bash_history. After it reaches this number, older commands will be discarded. If you want more or fewer commands stored, change the variable to a different number.
$ echo $HISTFILESIZE 1000
HISTCMD – The history number of your current command.
$ echo $HISTCMD 1007
Remove Command from Bash History
Removing commands from the bash command history is as simple as using the -d option to the “history” command. Just list the command number and it will be removed.
Example: delete commands 876, 459, 321, 65 from the bash command history.
$ history -d 876 459 321 65
Example: clear bash command history completely from all past commands.
$ history -c
Of course, you could always just remove your .bash_history file which would do the same thing.
$ rm -f $HOME/.bash_history
Other History Options
history -w | write the command history from memory into $HOME/.bash_history |
history -a | append the lines entered since starting the current bash session to the history file (.bash_history). |
Other Resources
There are other loctions where you can get more information on bash command line history:
Latest posts by Jeff Staten (see all)
- Configure Your HP Procurve Switch with SNTP - May 5, 2015
- Configuring HP Procurve 2920 Switches - May 1, 2015
- Troubleshooting Sendmail - November 28, 2014