Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 8: Command Line History'''= When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun. =='''Hack 50. Display TIMESTAMP in history using HISTTIMEFORMAT'''== Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timestamp along with the command as shown below. # '''export HISTTIMEFORMAT=’%F %T ‘ ''' # history | more # ## 2008-08-05 19:02:39 service network restart # ## 2008-08-05 19:02:39 exit ## 2008-08-05 19:02:39 id ## 2008-08-05 19:02:39 cat /etc/redhat-release '''Note''': You can also setup the following alias to view the recent history''' '''commands. alias h1='history 10' alias h2='history 20' alias h3='history 30' =='''Hack 51. Search the history using Control+R'''== I strongly believe that 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. <nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt as shown below] '''(reverse-i-search)`red‘: cat /etc/redhat-release''' <nowiki>[Note: Press enter when you see your command, which will execute the command from the history]</nowiki> <nowiki># </nowiki>'''cat /etc/redhat-release''' Fedora release 9 (Sulphur) 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. <nowiki># </nowiki><nowiki>[</nowiki>'''Note''': Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] '''(reverse-i-search)`httpd‘: service httpd stop''' <nowiki>[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]</nowiki> '''<nowiki># </nowiki>service httpd start''' =='''Hack 52. 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. # Use the '''up arrow''' to view the previous command and press enter to execute it. # '''Type !! '''and press enter from the command line''' ''' # '''Type !-1 '''and press enter from the command line.''' ''' # '''Press Control+P '''will display the previous command, press enter''' '''to execute it =='''Hack 53. Execute a specific command from history'''== In the following example, If you want to repeat the command #4, execute !4 as shown below. # history | more # ## service network restart ## exit ## id ## cat /etc/redhat-release # '''!4 ''' cat /etc/redhat-release Fedora release 9 (Sulphur) =='''Hack 54. 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 yp’. {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <nowiki># </nowiki>'''!ps''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | colspan="3" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ps aux | grep yp | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>16947</center> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.1</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">36516</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1264</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ? |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sl | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">13:10</div> | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>0:00 ypbind</center> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">root</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <center>17503</center> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0.0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">0.0</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">4124</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">740</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| pts/0 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| S+ | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">19:19</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0:00 grep | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| yp | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} =='''Hack 55. 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 450 command will be stored in the bash history. <nowiki># </nowiki>vi ~/.bash_profile HISTSIZE=450 HISTFILESIZE=450 =='''Hack 56. 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. <nowiki># vi ~/.bash_profile HISTFILE=/root/.commandline_warrior</nowiki> =='''Hack 57. 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 # ## pwd ## pwd ## pwd ## history | tail -4 <nowiki>[</nowiki>'''Note''': There are three pwd commands in history, after executing pwd 3 times as shown above] # '''export HISTCONTROL=ignoredups ''' # pwd # pwd # pwd # history | tail -3 # ## export HISTCONTROL=ignoredups ## pwd ## history | tail -4 <nowiki>[</nowiki>'''Note''': There is only one pwd command in the history, even after executing pwd 3 times as shown above] =='''Hack 58. 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 # ## pwd ## service httpd stop ## history | tail -3 # ls -ltr # service httpd stop # history | tail -6 # ## export HISTCONTROL=erasedups ## pwd ## history | tail -3 ## ls –ltr ## service httpd stop ## history | tail -6 <nowiki>[</nowiki>'''Note''': The previous service httpd stop after pwd got erased] =='''Hack 59. 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 <nowiki>[</nowiki>'''Note''': There is a space at the beginning of service, to ignore this command from history] # history | tail -3 # ## ls –ltr ## pwd ## history | tail -3 =='''Hack 60. Clear all the previous history using option -c'''== Sometime you may want to clear all the previous history. However you may still want to keep the history moving forward. <nowiki># </nowiki>'''history -c''' =='''Hack 61. Substitute 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 =='''Hack 62. 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. <nowiki># </nowiki>ls -l !cp:$ ls -l /really/a/very/long/path/long-filename.txt =='''Hack 63. 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 # <nowiki>[</nowiki>'''Note''': History did not display anything] =='''Hack 64. 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 # ## export HISTIGNORE=”pwd:ls:ls -ltr:” ## service httpd stop ## history <nowiki>[</nowiki>'''Note''': History did not display pwd and ls]
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)