You are on page 1of 6

2/17/13

15 Examples To Master Linux Command Line History

Home About Free eBook Archives Best of the Blog Contact
Ads by Google

Command Line Linux

Linux RedHat Server

Ubuntu Linux System

Unix Linux

15 Examples To Master Linux Command Line History
by Ramesh Natarajan on August 11, 2008
86 Like 96 Tw eet 116

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.

1. Display timestamp 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 timepstamp along with the command as shown below.
#epr HSTMFRA=% % ' xot ITIEOMT'F T #hsoy|mr itr oe 1 20-80 1:23 srientokrsat 080-5 90:9 evc ewr etr 2 20-80 1:23 ei 080-5 90:9 xt 3 20-80 1:23 i 080-5 90:9 d 4 20-80 1:23 ct/t/ehtrlae 080-5 90:9 a ecrda-ees

2. Search the history using Control+R
www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/ 1/25

this may be your most frequently used feature of history. #[rs Cr+ fo tecmadpop. Use the up arrow to view the previous command and press enter to execute it. which will display service httpd stop from the command history. Type !! and press enter from the command line Type !-1 and press enter from the command line. select this command and change the stop to start and reexecute it again as shown below.2/17/13 15 Examples To Master Linux Command Line History I strongly believe. Execute a specific command from history In the following example.com/2008/08/15-examples-to-master-linux-command-line-history/ 2/25 . 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. 2. www. Following are the 4 different ways to repeat the last executed command. you can do !4 as shown below. #[rs Cr+ fo tecmadpop. Repeat previous command quickly using 4 different methods Sometime you may end up repeating the previous commands for various reasons. executed the previous command starting with ps. If you want to repeat the command #4. In the following example. 1. #hsoy|mr itr oe 1 srientokrsat evc ewr etr 2 ei xt 3 i d 4 ct/t/ehtrlae a ecrda-ees #! 4 ct/t/ehtrlae a ecrda-ees Fdr rlae9(upu) eoa ees Slhr 5. typing !ps and enter. 3. Press Control+P will display the previous command. you can simply search history using a keyword and re-execute the same command without having to type it fully. When you’ve already executed a very long command. For e. which is ‘ps aux | grep yp’. Pes tlR rm h omn rmt wihwl dslytervreisac pop] hc il ipa h ees--erh rmt (ees--erh`e' ct/t/ehtrlae rvreisac)rd: a ecrda-ees [oe Pesetrwe yuseyu cmad Nt: rs ne hn o e or omn. I searched for red. which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red. hc il ipa h omn o o o dt eoe xctn t #sriehtdsat evc tp tr 3.g.bfr eeuigi] omn. 4. press enter to execute it 4. In the following example. Press Control+R and type the keyword. wihwl eeuetecmadfo tehsoy hc il xct h omn rm h itr] #ct/t/ehtrlae a ecrda-ees Fdr rlae9(upu) eoa ees Slhr Sometimes you want to edit a command from history before executing it. you can search for httpd. Pes tlR rm h omn rmt wihwl dslytervreisac pop] hc il ipa h ees--erh rmt (ees--erh`tp' sriehtdso rvreisac)htd: evc tp tp [oe Pesete lf arwo rgtarwkywe yuseyu Nt: rs ihr et ro r ih ro e hn o e or cmad wihwl dslytecmadfryut ei.thegeekstuff.

#pd w #pd w #pd w #hsoy|ti itr al 4 4 pd 4 w www.ahpoie i /bs_rfl HSFL=ro/cmadiewrir ITIE/ot. as I’m interested in finding out how you are using this feature.thegeekstuff. #v ~. Control the total number of lines in the history using HISTSIZE Append the following two lines to the . Eliminate the continuous repeated entry from history using HISTCONTROL In the following example pwd was typed three times.bash_history file.commandline_warrior file instead of . to store the history command in . you can see all the 3 continuous occurrences of it. I can see this getting used when you want to track commands executed from different terminals using different history file name. To eliminate duplicates. I’m yet to figure out a practical use for this.omnln_aro If you have a good reason to change the name of the history file. 61 24 ro ot 153 00 00 42 70 .ahpoie i /bs_rfl HSSZ=5 ITIE40 HSFLSZ=5 ITIEIE40 7. Add the following line to the . Change the history file name using HISTFILE By default. when you do history. please share it with me. #v ~. .2/17/13 15 Examples To Master Linux Command Line History #!s p p ax|ge y s u rp p ro ot 197 00 01 356 16 ? 64 . set HISTCONTROL to ignoredups as shown below.com/2008/08/15-examples-to-master-linux-command-line-history/ 3/25 . In this example. only 450 command will be stored in the bash history.bash_profile and relogin to the bash shell again to see the change. 14 70ps0 4 t/ S l S + 1:0 31 1:9 91 00 ybn :0 pid 00 ge y :0 rp p 6. . 8.bash_history file. history is stored in ~/.bash_profile and relogin to the bash shell.

com/2008/08/15-examples-to-master-linux-command-line-history/ 4/25 . I can see lot of junior sysadmins getting excited about this. #epr HSCNRLinrsae xot ITOTO=goepc #l -t s lr #pd w # sriehtdso [oeta teei asaea tebgnigo srie evc tp tp Nt ht hr s pc t h einn f evc. as a best practice.thegeekstuff. But. t inr ti cmadfo hsoy o goe hs omn rm itr] #hsoy|ti itr al 3 6 l -t 7 s lr 6 pd 8 w 6 hsoy|ti 9 itr al 3 11. It is good to understand how ignorespace works.2/17/13 15 Examples To Master Linux Command Line History 4 pd 5 w 4 pd[oeta teeaetrepdcmad i hsoy atr 6 w Nt ht hr r he w omns n itr. Clear all the previous history using option -c www. 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. #epr HSCNRLeaeus xot ITOTO=rsdp #pd w #sriehtdso evc tp tp #hsoy|ti itr al 3 3 pd 8 w 3 sriehtdso 9 evc tp tp 4 hsoy|ti 0 itr al 3 #l -t s lr #sriehtdso evc tp tp #hsoy|ti itr al 6 3 epr HSCNRLeaeus 5 xot ITOTO=rsdp 3 pd 6 w 3 hsoy|ti 7 itr al 3 3 l -t 8 s lr 3 sriehtdso 9 evc tp tp [oeta tepeiu sriehtdso atrpdgteae] Nt ht h rvos evc tp tp fe w o rsd 4 hsoy|ti 0 itr al 6 10. Force history not to remember a particular command using HISTCONTROL When you execute a command. Erase duplicates across the whole history using HISTCONTROL The ignoredups shown above removes duplicates only if they are consecutive commands. set the HISTCONTROL to erasedups as shown below. don’t hide purposefully anything from history. To eliminate duplicates across the whole history. vn fe eeuigpd3tmsa sonaoe xctn w ie s hw bv] 5 hsoy|ti 8 itr al 4 9. as they can hide a command from the history. fe eeuigpd3tmsa sonaoe xctn w ie s hw bv] 4 hsoy|ti 7 itr al 4 #epr HSCNRLinrdp xot ITOTO=goeus #pd w #pd w #pd w #hsoy|ti itr al 3 5 epr HSCNRLinrdp 6 xot ITOTO=goeus 5 pd[oeta teei ol oepdcmadi tehsoy ee atr 7 w Nt ht hr s ny n w omn n h itr.

f #v !: i !$ v aaod-scg i ncnak.e vi command). the !^ next to the vi command gets the first argument from the previous command (i.com/2008/08/15-examples-to-master-linux-command-line-history/ 5/25 .f 13. but want to keep the history moving forward.e cp command) to the current command (i.x /elyavr/ogpt/ogflnm. #l aaod-scg s ncnak. !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case.!p$ s l c: l . #hsoyitr c 12.a aaod-scg ncnak. which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.f #v ! i ^ v aaod-scg i ncnak.f ncnak. set the HISTSIZE to 0 as shown 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.x s l ral//eyln/ahln-ieaett 14. #l . you may want to execute a different command but use the same parameter from the command that you’ve just searched. Use HISTIGNORE to www. the !!:$ next to the vi command gets the argument from the previous command to the current command.thegeekstuff.f In the example below.!p2 s l c: l .x s l ral//eyln/ahln-ieaett In the example below./elyavr/ogpt/ogflnm.f aaod-scg ncnak. 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./elyavr/ogpt/ogflnm.f. Substitute a specific argument for a specific command. Subtitute words from history commands When you are searching through history. #epr HSSZ= xot ITIE0 #hsoy itr #[oeta hsoyddntdslyayhn] Nt ht itr i o ipa ntig 15. #c aaod-scgaaod-scgbk p ncnak.2/17/13 15 Examples To Master Linux Command Line History Sometime you may want to clear all the previous history. In the example below. In the example below.x p /ogaett ral//eyln/ahln-ieaett #l . #c ~lnnm. 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.

I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. 50 Most Frequently Used Linux Commands (With Examples) www. I used to write lot of code on C shell and Korn shell.com/2008/08/15-examples-to-master-linux-command-line-history/ Awk Introduction – 7 Awk Print Examples Advanced Sed Substitution Examples 6/25 . by Ramesh Natarajan. naturally I’m a huge fan of Bash command line and shell scripting. 15 years back. I spend most of my time on Linux environment. If you’ve been thinking about mastering Bash..2/17/13 15 Examples To Master Linux Command Line History specify all the commands that you want to ignore from the history. s n s lr Recommended Reading Bash 101 Hacks. when I started working on Linux as system administrator. when I was working on different flavors of *nix. So. you might also like. 1. you have to provide the exact command that you would like to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. I found it! — 15 Practical Linux Find Command Examples 86 Tw eet 116 Like 96 Share Comment If you enjoyed this article. Based on my Bash experience. Unix LS Command: 15 Practical Examples Get a Grip on the Grep! – 15 Practical Grep Command Examples Linux Crontab: 15 Awesome Cron Job Examples Mommy. #epr HSINR=pdl:s-t: xot ITGOE"w:sl lr" #pd w #l s #l -t s lr #sriehtdso evc tp tp #hsoy|ti itr al 3 7 epr HSINR=pdl:s-t: 9 xot ITGOE"w:sl lr" 8 sriehtdso 0 evc tp tp 8 hsoy 1 itr [oeta hsoyddntrcr pd l adl -t] Nt ht itr i o eod w.thegeekstuff. do yourself a favor and read this book. I pretty much automated every possible task using Bash shell scripting. which will help you take control of your Bash command line and shell scripting. Later years. Awesome Linux Articles Following are few awesome 15 examples articles that you might find helpful. 50 Linux Sysadmin Tutorials 2. So.