Linux tips-history command usage: 15 examples

Source: Internet
Author: User

Linux tips-history command usage: 15 Linux System Engineers often use the history command to view system operation history when analyzing system security or fault problems, this article will introduce 15 tips related to the history command through examples ...... 1. Use HISTTIMEFORMAT to display the timestamp. After you execute the history command from the command line, only the serial number of the executed command and the command itself are displayed. To view the timestamp of the command history, run the following command: # export HISTTIMEFORMAT = '% F % t' # history | more1 2008-08-05 19:02:39 service network restart2 2008-08-05 19:02:39 exit3 2008-08-05 19:02:39 id4 2008-08-05 19:02:39 cat/etc/redhat-release note: this function can only be used when the environment variable HISTTIMEFORMAT is set, and then the newly executed bash commands will be correctly timestamp. All commands earlier than this will display the time for setting the HISTTIMEFORMAT variable. 2. Using Ctrl + R to search for historical Ctrl + R is a frequently used shortcut key. This shortcut allows you to search for command history, which is useful when you want to execute a command repeatedly. After finding the command, you can usually press the Enter key to execute the command. If you want to adjust the command and then execute it, you can click the left or right arrow keys. # [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-releaseFedora release 9 (Sulphur) 3. you can repeat the previous command in four ways: Use the up arrow key and press enter to execute it. Press !! And press enter to execute. Enter! -1 and press enter to execute. Press Ctrl + P and press Enter. 4. Execute a specified command from the command history. In the following example, if you want to repeat 4th commands, You can execute them! 4: # history | more1 service network restart2 exit3 id4 cat/etc/redhat-release #! 4cat/etc/redhat-releaseFedora release 9 (Sulphur) 5. Run the previous command by specifying the keyword. In the example below, enter! Ps and press Enter. The command with ps headers will be executed :#! Psps aux | grep yproot 16947 0.0 0.1 36516 1264? Sl ypbindroot 17503 0.0 0.0 4124 740 pts/0 S + grep yp 6. use HISTSIZE to control the total number of lines in the History Command record and append the following two lines. bash_profile file and log on to bash shell again. The number of command history records will change to 450: # vi ~ /. Bash_profileHISTSIZE = 450 HISTFILESIZE = 450 7. Use HISTFILE to change the name of a historical file. By default, the command history is stored in ~ /. Bashhistory file. Add the following content to the. bashprofile file and log on to the bash shell again. The. commandline_warrior command history will be stored: # vi ~ /. Bash_profileHISTFILE =/root/. commandline_warrior 8. Use HISTCONTROL to remove consecutive duplicate entries from the command history. In the following example, the pwd command is executed three times in a row. After history is executed, you will see three duplicate entries. To remove these duplicate entries, you can set HISTCONTROL to ignoredups: # pwd # history | tail-444 pwd45 pwd46 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 # history | tail-356 export HISTCONTROL = ignoredups57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 t Imes as shown above] 58 history | tail-4 9. Use HISTCONTROL to clear duplicate entries in the history of the entire command. ignoredups in the previous example can only remove consecutive duplicate entries. To clear duplicate entries in the history of the entire command, you can set HISTCONTROL to erasedups: # export HISTCONTROL = erasedups # pwd # service httpd stop # history | tail-338 pwd39 service httpd stop40 history | tail-3 # ls-ltr # service httpd stop # history | tail-635 export HISTCONTROL = erasedups36 pwd37 history | tail-338 ls-ltr39 service httpd stop [Note that the previous service httpd stop after pwd got erased] 40 history | tail-6 10. use HISTCO NTROL forces history not to remember specific commands to set HISTCONTROL to ignorespace, and enter a space before the command that you do not want to remember: # 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-367 ls-ltr68 pwd69 history | tail-3 11. use the-c option to clear all command history. If you want to clear all command history, run the following command: # history-c 12. replace the command in the following example ,!!: $ Will get the parameter of the previous command for the current command: # ls anaconda-ks.cfganaconda-ks.cfg # vi !!: $ Vi anaconda-ks.cfg supplement: use! $ Can achieve the same effect, and is simpler. [Thanks to the wanzigunzi reader for adding] in the following example ,! ^ Get the first parameter from the previous command: # cp anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-ks.cfg # vi-5! ^ Vi anaconda-ks.cfg 13. Replace the specified parameter with a specific command in the example below ,! Cp: 2 search for a command starting with cp from the command history and obtain its second parameter: # cp ~ /Longname.txt/really/a/very/long/path/long-filename.txt # ls-l! Cp: 2ls-l/really/a/very/long/path/long-filename.txt in the following example ,! Cp: $ get the last parameter of the cp command: # ls-l! Cp: $ ls-l/really/a/very/long/path/long-filename.txt 14. use HISTSIZE to disable history. If you want to disable history, set HISTSIZE to 0: # export HISTSIZE = 0 # history # [Note that history did not display anything] 15. using HISTIGNORE to ignore specific commands in history, the following example ignores commands such as pwd, ls, ls-ltr: # export HISTIGNORE = "pwd: ls-ltr: "# pwd # ls-ltr # service httpd stop # history | tail-379 export HISTIGNORE =" pwd: ls-ltr: "80 service httpd stop81 history [Note that history did not record pwd, ls and ls-ltr]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.