Typically when you type history from command line, it displays the command number and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.
# export HISTTIMEFORMAT='%F %T '
# history | more
1 2010-08-06 18:04:29 service network restart
2 2010-08-06 18:04:29 exit
3 2010-08-06 18:04:29 id
4 2010-08-06 18:04:29 cat /etc/redhat-release
This may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command "cat /etc/redhat-release" in the history that contained the word red.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release [Note: Press enter when you see your command, which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)
Sometimes you want to edit a command from history before executing it e.g. you can search for httpd, which will display "service httpd stop" from the command history, select this command and change the stop to start and re-execute it again as shown below.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop [Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it]
# service httpd start
In the following example, If you want to repeat the command #4, you can do !4 as shown below.
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is "ps aux | grep sshd".
# !ps
ps aux | grep sshd
root 4104 0.0 0.0 60708 2768 ? Ss 11:09 0:00 sshd: root@pts/0
root 4119 0.0 0.0 26992 1092 ? Ss Jan07 0:00 /usr/sbin/sshd
Append the following two lines to the ~/.bash_profile and relogin to the bash shell again to see the change. In this example, only 100 commands will be stored in the bash history.
# vi ~/.bash_profile
HISTSIZE=100
HISTFILESIZE=100
By default, history is stored in ~/.bash_history file. Add the following line to the ~/.bash_profile and relogin to the bash shell, to store the history command in ~/.command_history file instead of .bash_history file. This may be useful when you want to track commands executed from different terminals using different history file name.
# vi ~/.bash_profile
HISTFILE=/root/.command_history
# . ~/.bash_profile
In the following example, pwd was typed three times, when you do history, you can see all 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.
# pwd
# pwd
# pwd
# history | tail -4
51 pwd
52 pwd
53 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
54 history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
55 export HISTCONTROL=ignoredups
56 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
57 history | tail -3
The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38 pwd
39 service httpd stop
40 history | tail -3
# ls -ltr
# service httpd stop
# history | tail -6
37 export HISTCONTROL=erasedups
38 pwd
39 history | tail -3
40 ls -ltr
41 service httpd stop [Note that the previous service httpd stop after pwd got erased]
42 history | tail -6
When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below.
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note space at the beginning of service to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3
Sometime you may want to clear all the previous history, but want to keep the history moving forward.
# history -c
When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.
In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi !^
vi anaconda-ks.cfg
In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt
If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.
# export HISTSIZE=0
# history
# [Note that history did not display anything]
Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.
# export HISTIGNORE="pwd:ls:ls -ltr:"
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79 export HISTIGNORE="pwd:ls:ls -ltr:"
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr]