Detailed instructions on how to use history commands in Linux

Source: Internet
Author: User

Linux history command usage details when you are playing Linux, If you often use command lines to control your Linux system, the effective use of the command history mechanism will greatly improve the efficiency. As a matter of fact, once you have mastered the 15 Linux history commands I have provided below, you will find it even more fun to use the history commands of history. 1. Use HISTTIMEFORMAT to display TIMESTAMP in history. Generally, when you type history in the command line, the terminal displays the command you just entered and its number. For the purpose of reviewing the command, displaying the timestamp with the command will be helpful, as shown below. # Export HISTTIMEFORMAT = '% F % t' www.2cto.com # 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: you can also set an alias statement to view recent historical commands] alias h1 = 'History 10' alias h2 = 'History 20' alias h3 = 'History 30' 2. Press Ctrl + R searching for historical commands I am very confident that this should be your most commonly used history feature, after you execute a long string of commands, you only need to search for historical commands with keywords and re-execute the command without losing the entire command. Press Ctrl + R and enter a keyword. In the following example, When I search for "red", the previous command "cat/etc/redhat-release" containing "red" is displayed ". [Note: press Ctrl + R at the command line prompt, and the terminal will display the following prompt-reverse-I-search ‖] (reverse-I-search) 'red ': cat/etc/redhat-release [NOTE: When you see the command you want, press the Enter key, you can run this command again] # cat/etc/redhat-release Fedora release 9 (Sulphur). Sometimes you need to edit it before executing a history command. for example, you can search for "httpd" as follows, display the historical command "service httpd stop" on the terminal, change "stop" to "start", and then execute it [Note: press Ctrl + R at the command prompt. The prompt "reverse-I-search"] (reverse-I-search) "httpd": service httpdstop [Note: after you see the command you want, press the left or right-click button to execute this Before a command, edit it] # service httpd start 3, four different methods to quickly execute the previous command. Sometimes for some reason, you need to execute the previous command, the following four methods can be used to repeat the last command: 1. use up arrow to view the previous command and press enter to execute it. 2. enter it in the command line !! Press enter. 3. enter it in the command line! -1 and press Enter. 4. Press Ctrl + P to display the previous command and press enter to execute it. 4. Execute a specific command in the previous command in the following example. If you want to execute the fourth command again, run it! 4 then # history | more 1 service network restart 2 exit 3 id 4 cat/etc/redhat-release #! 4 cat/etc/redhat-release Fedora release 9 (Sulphur) 5. Execute historical command input starting with a specific word! And the first few letters of the command you want to re-execute. In the example below, enter! Ps, press enter, and run "psaux | grep yp" www.2cto.com # starting with "ps" in the history command #! Ps psaux | grep yp root 16947 0.0 0.0 36516 1264? Sl ypbind root 17503 0.0 0.0 4124 740 pts/0 S + grep yp 6. Use HISTSIZE to control the total number of historical commands and add the following two rows. bash_profile and then log on to bash again. bash_profile). In this example, only 450 commands can be stored in the bash command history. # Vi ~ /. Bash_profile HISTSIZE = 450 HISTFLESIZE = 450 7. Use HISTFILE to change the historical file name. By default, the command history is stored in. in the bash_history file, add the following line. in the bash_profile file, log on to the shell again, then. the commandline_warrior file will be replaced. the bash_history file is used to store historical commands. You can use this command to track commands executed on different terminals. At that time, you only need to save the commands executed on different terminals in different historical files. # Vi ~ /. Bash_profile HISTFILE =/root /. commandline_warrior 8. Use HISTCONTROL to remove consecutive duplicate entries in the command history. In the following example, pwd is input three times. When you use history, you will see these three commands appear consecutively. Set HISTCONTROL to ignoredups to eliminate repeated commands: www.2cto.com # pwd # history | tail-4 44 pwd 45 pwd 46 pwd 47 history | tail-4 [Note: after the above pwd is executed three times, there are three pwd commands in history] # export HISTCONTROL = ignoredups # pwd # history | tail-3 56 export HISTCONTROL = ignoredups 57 pwd 58 history | tail-4 [Note: even if the above pwd has been executed three times, there is only one pwd command in history] 9. Use HISTCONTROL to remove duplicate commands throughout the history. The ignoredups command above removes consecutive duplicates, to remove duplicate commands in the entire command history, set HISTCONTROL 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 40 history | tail-6 [Note: previously, the servicehttpd stop command after pwd was removed] 10. When using HISTCONTROL to force history to ignore a specific command, you can set HISTCONTROL to I Gnorespace and add a space before the command to instruct history to ignore this command. It is foreseeable that many junior system administrators will be excited that they can successfully hide a command from history. It is good to know how ignorespace works. However, in practice, it is best not to mask any commands from history. # Export HISTCONTROL = ignorespace # ls-ltr # pwd # service httpd stop [Note: Add a space before the service, to ignore it in the command history] # history | tail-3 www.2cto.com 67 ls-ltr 68 pwd 69 history | tail-3 11. Use the c option to clear all historical commands. Sometimes you may to clear previous commands. And you want history to continue working: # history-c 12. Replace the content in the command history. When you search for historical commands, you may want to execute a command with the same parameters as the previous command you just found. In this example, after vi "!!: $ "You can use the parameters of the previous command as the parameters of the current command. # Ls anaconda-ks.cfg # vi !!: $ Vi anaconda-ks.cfg in the following example, followed by the parameter "after vi! ^ "Pass the first parameter of the previous command to the current command. # Cp anaconda-ks.cfg anaconda-ks.cfg.bak # vi! ^ Vi anaconda-ks.cfg 13. Replacing specific parameters for a specific command in the following example ,! Cp: 2 searches for commands starting with cp in the command history, and uses the second parameter as the parameter of the current command (ls-l. Www.2cto.com # 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: $ "searches for commands starting with cp and returns the last parameter of the command (in this example, it is still the second parameter) replace it with "ls-l. # 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 and prevent bashshell from recording your command, set HISTSIZE to 0 as follows. # Export HISTSIZE = 0 # history # [Note: no information is displayed after history is executed.] 15. Use HISTIGNORE to let history ignore certain commands during storage. Sometimes you don't want to see Basic commands such as "pwd" and "ls" in the record, you can use HISTIGNORE to ignore these commands. Note that "ls" is added to HISTIGNORE, and "ls-l" is ignored only ". Be sure to accurately write the commands to be ignored. # Export HISTIGNORE = "pwd: ls-ltr:" # pwd www.2cto.com # ls-ltr # service httpd stop # history | tail-3 79 export HISTIGNORE = "pwd: ls: ls-ltr: "80 service httpd stop 81 history [NOTE: The history command does not display pwd and ls] This article is from qingteng Garden

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.