Linux Command History Advanced Features
“History” command is a very important command in Linux to check the last executed commands in your system. We can use “history” command in some advanced manner with using some extra functionality. You can find this command more enjoyable by using following document.
1. Display Time Format using HISTSIZEFORMAT
When you run simple “history” it displays only number of command and last running command. For auditing purpose the time for running command is important. Timestamp can be added in history command as shown below.
# export HISTTIMEFORMAT="%F %T "
# history | more
1 2012-09-12 10:48:00 ls
2 2012-09-12 10:48:07 history
3 2012-09-12 10:48:24 ls -ltr
4 2012-09-12 10:54:57 ls
5 2012-09-12 10:55:05 ls -ltr
6 2012-09-12 10:55:22 cat ff_unix
2. Search the history using Control+R
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.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 reverse –i-search prompt.]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you you see command, which will execute from history.]
[root@qmailprd ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.2 (Santiago)
Sometimes you want to edit a command from history before executing it. For 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
3. Repeat previous command quickly using 4 different methods
Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.
1. Use the up arrow to view the previous command and press enter to execute it.
2. Type !! and press enter from the command line
3. Type !-1 and press enter from the command line.
4. Press Control+P will display the previous command, press enter to execute it
4. Execute a specific command from history
In the following example, If you want to repeat the command #998, you can do !998 as shown below
997 vi pop3d-ssl
998 pwd
999 openssl s_client -connect localhost:993 -quiet
1000 history
1001 cat /etc/issue
1002 cat /etc/redhat-realease
1003 history
[root@qmailprd ~]# !998
pwd
/root
5. Execute previous command that starts with a specific word
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 http’.
[root@qmailprd ~]# !ps
ps -ef | grep http
root 1830 1 0 Aug03 ? 00:00:35 /usr/sbin/httpd
apache 22502 1830 0 Sep16 ? 00:00:00 /usr/sbin/httpd
apache 22503 1830 0 Sep16 ? 00:00:00 /usr/sbin/httpd
6. Control the total number of lines in the history using HISTSIZE
Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 1500 command will be stored in the bash history.
# vi ~/.bash_profile
HISTSIZE=1500
HISTFILESIZE=1500
7. Change the history file name using HISTFILE
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 .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
8. Eliminate the continuous repeated entry from history using HISTCONTROL
In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.
# 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
9. Erase duplicates across the whole history using HISTCONTROL
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
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
10. Force history not to remember a particular command using HISTCONTROL
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. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.
# 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
11. Clear all the previous history using option -c
Sometime you may want to clear all the previous history, but want to keep the history moving forward.
# history -c
12. Subtitute words from history commands
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
13. Substitute a specific argument for a specific command.
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
14. Disable the usage of history using HISTSIZE
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]
15. Ignore specific commands from the history using HISTIGNORE
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]
No comments:
Post a Comment