If you often use the Linux command line, then using the history command can effectively improve your efficiency. This article will introduce you to 15 usages of the history command by way of examples.
Display timestamps using HISTTIMEFORMAT
When you execute the history command from the command line, usually only the sequence number of the executed command and the command itself are displayed. If you want to see the timestamp of the command history, you can do:
# export HISTTIMEFORMAT='%F %T '
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/redhat-release
Note: This function can only be used when the HISTTIMEFORMAT environment variable is set, and those newly executed bash commands will be stamped with the correct timestamp. All previous commands will display the time the HISTTIMEFORMAT variable was set. [Added thanks to NightOwl readers]
Use Ctrl+R to search history
Ctrl+R is a shortcut I use a lot. This shortcut allows you to search through the command history, useful when you want to repeat a command. When a command is found, it is usually executed by pressing the Enter key again. If you want to adjust the found command and then execute it, you can press the left or right arrow key.
# [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)
Quickly repeat the previous command
There are 4 ways to repeat the previous command:
Use the up arrow key and press Enter to execute.
Press !! and enter to execute.
Type !-1 and press Enter to execute.
Press Ctrl+P and press Enter to execute.
Execute a specified command from the command history
In the following example, if you want to repeat the fourth command, you can do !4:
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)
Execute the previous command by specifying the keyword
In the following example, typing !ps and pressing Enter will execute commands starting with ps:
# !ps
ps aux | grep yp
root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp
Use HISTSIZE to control the total number of lines recorded by the history command
Append the following two lines to the .bash_profile file and log in to the bash shell again, and the number of records in the command history will become 450:
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
Use HISTFILE to change the history file name
By default, the command history is stored in the ~/.bash_history file. Add the following to the .bash_profile file and re-login to the bash shell, the .commandline_warrior will be used to store the command history:
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
Use HISTCONTROL to remove consecutive duplicate entries from the command history
In the example below, the pwd command is executed three times in a row. After executing history you will see three duplicate entries. To weed out these duplicate entries, you can set HISTCONTROL to ignoredups:
# pwd
# pwd
# pwd
# history | tail -4
44 pwd
45 pwd
46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47 history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56 export HISTCONTROL=ignoredups
57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58 history | tail -4
Use HISTCONTROL to clear duplicate entries from entire command history
The ignoredups in the above example can only remove consecutive duplicate entries. To clear the entire command history of duplicate entries, set HISTCONTROL to erasedups:
# 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
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
39 service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6
Use HISTCONTROL to force history not to remember specific commands
Set HISTCONTROL to ignorespace and enter a space before commands you don't want to be remembered:
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3
Use the -c option to clear all command history
If you want to clear all command history, you can execute:
# history -c
command substitution
In the following example, !!:$ will get the arguments of the previous command for the current command:
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
ADDED: Use !$ to achieve the same effect, but much simpler. [Thanks to wanzigunzi reader for addition]
In the following example, !^ gets the first argument from the previous command:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
Substitute the specified parameter for a specific command
In the following example, !cp:2 searches the command history for commands starting with cp and gets its second argument:
# 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 following example, !cp:$ gets the last parameter of the cp command:
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt
Disable history with HISTSIZE
If you want to disable history, set HISTSIZE to 0:
# export HISTSIZE=0
# history
# [Note that history did not display anything]
Use HISTIGNORE to ignore specific commands in history
The following example will ignore pwd, ls, ls -ltr, etc. commands:
# 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]