Original URL: http://os.51cto.com/art/201205/335040.htm
When you are playing Linux, if you often use the command line to control your Linux system, then the efficient use of the command history mechanism will make the efficiency gains greatly. In fact, once you have mastered the 15 examples of Linux history commands I gave below, you will find it more fun to use the history command line.
1. Use Histtimeformat to show timestamp in history
Typically, when you type history on the command line, the command and its number you just entered will appear in the terminal. It would be helpful to display a timestamp together with the command for the purposes of the Review command, as shown below.
# export histtimeformat= '%F%T '
# 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 the alias statement to view recent history commands]
Alias h1= ' History 10 '
Alias h2= ' History 20 '
Alias h3= ' History 30 '
2. Search history Commands with CTRL + R
I'm pretty sure this is the most common history feature you'll ever use, and when you've executed a fairly long list of commands, you just need to search for the historical command with the keyword and then re-execute the command without having to lose the whole command again. To do this: Press CTRL +r and enter the keywords. In the following example, I search for "red", which displays the command "Cat/etc/redhat-release" with "red" in the previous command.
[Note: At the command line prompt, press CTRL+R, the terminal will display the following prompt ―reverse-i-search‖]
(reverse-i-search) ' Red ': cat/etc/redhat-release
[Note: You can re-execute this command when you see the command you want and press ENTER]
#cat/etc/redhat-release
Fedora Release 9 (sulphur)
And sometimes you need to edit it before executing a history command. For example, you can search for "httpd" as follows, the terminal displays the history command "service httpd stop", select it to change "stop" to "start" and then execute it
[Note: At the command prompt, press ctrl+r, the prompt will be displayed ‖reverse-i-search‖]
(reverse-i-search) ' httpd ': Service httpdstop
[Note: When you see the command you want, press the left or right button to edit it before executing this command]
# service httpd Start
3. Four different ways to quickly execute previous commands
Sometimes for some reason you need to execute the previous command, the following four methods can be used to repeat the last executed command:
1. Use up arrow to view the previous command and press ENTER to execute.
2. On the command line, enter!! and press ENTER.
3. Enter!-1 in the command line and press ENTER.
4. Press Ctrl+p to display the previous command and press ENTER to execute.
4. Perform specific commands in the history command
In the example below, if you want to execute the fourth command again, execute! 4 can
#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 a historical command beginning with a specific word
Input! And the first few letters of the command you want to re-execute. In the example below, enter! PS, enter, perform the "PS" in the history command "Psaux | grep YP "
#!ps
Psaux | grep YP
Root 16947 0.0 0.0 36516 1264? Sl 13:10 0:00 Ypbind
Root 17503 0.0 0.0 4124 740 pts/0 s+ 19:19 0:00 grep YP
6. Control the total number of historical commands with Histsize
Add the following two lines to the. Bash_profile and then re-login to bash (Translator Note: use source. bash_profile) To see what has changed, in this case, the Bash command history can only store 450 commands.
#vi ~/.bash_profile
histsize=450
histflesize=450
7. Use Histfile to change the history file name
By default, the command history is stored in the. bash_history file, the following line is added to the. bash_profile file, and the. commandline_warrior file is replaced by the. Bash_ The history file is used to store historical commands. You can use this command to track commands executed in different terminals by storing the commands executed in different terminals in different historical files.
#vi ~/.bash_profile
Histfile=/root/.commandline_warrior
8. Use Histcontrol to eliminate consecutive duplicate entries in the command history
In the following example, the PWD is entered three times, and when you use history, you will see that the three commands appear consecutively. Set Histcontrol to Ignoredups to eliminate duplicate commands:
#pwd
# pwd
# pwd
# History | Tail-4
Pwd
Pwd
Pwd
History | Tail-4
[Note: After the PWD has been executed three times, there are three PWD commands in history]
# Export Histcontrol=ignoredups
# pwd
# pwd
# pwd
# History | Tail-3
Histcontrol=ignoredups Export
The history of the PWD | 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 repetitive commands throughout history
The above ignoredups removes repeated commands to eliminate repetitive commands throughout the command history, setting Histcontrol to Erasedups
#export histcontrol=erasedups
# pwd
# Service HTTPD Stop
#history | Tail-3
The PWD
Service httpd Stop
History | Tail-3
# ls-ltr
# Service HTTPD Stop
# History | Tail-6
Export Histcontrol=erasedups
Pwd
Panax Notoginseng | Tail-3
Ls–ltr
Service httpd Stop
History | Tail-6
[Note: The SERVICEHTTPD stop command after the PWD has been removed]
10. Use Histcontrol to force history to ignore a specific command
When executing a command, you can set Histcontrol to ignorespace and precede the command with an empty glyd to indicate that history ignores this command. Predictably, many junior system administrators will be thrilled that they can successfully hide a command from history.
It's good to know how ignorespace works. In practice, however, it is best not to conceal any order from the history.
#export Histcontrol=ignorespace
# ls–ltr
# pwd
# Service HTTPD Stop
[Note: Add a space in front of the service to ignore it in the command history]
# History | Tail-3
Ls–ltr
Pwd
History | Tail-3
11. Clear all History commands using the C option
Sometimes you may want to erase previous historical commands. And you want to keep history working:
#history –c
12. Replace the contents of the command history
When you search for history commands, you may want to execute a command that has the same parameters as the history command you just found. In this example, the "!!:$" after VI can make the arguments of the previous command the parameters of the current command.
#ls anaconda-ks.cfg
Anaconda-ks.cfg
# VI!!:$
VI anaconda-ks.cfg
In the following example, the parameter "!^" immediately following the VI passes the first argument of the previous command to the current command.
#cp anaconda-ks.cfg Anaconda-ks.cfg.bak
Anaconda-ks.cfg
# VI!^
VI anaconda-ks.cfg
13. Replace specific parameters for specific commands
In the following example,!cp:2 looks for a command that begins with CP in the command history and takes its second argument as a parameter of the current command (LS-L).
#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:$" looks for a command previously preceded by the CP and replaces the last parameter of the directive (which is still the second parameter in this case) with "Ls–l".
#ls-L!cp:$
Ls-l/really/a/very/long/path/long-filename.txt
14. Disable history with Histsize
If you want to disable history and don't let Bashshell record your commands, set Histsize to 0 as follows.
#export histsize=0
# History
# [Note: No information is displayed after performing the history]
15. Use Histignore to let history ignore certain instructions when storing
Sometimes you don't want to see basic instructions such as "pwd", "LS" in the records, and you can ignore these instructions with Histignore.
Note adding "LS" to Histignore, ignoring "LS" without ignoring "ls–l". Be sure to write down exactly the instructions that you want to ignore.
#export histignore= "pwd:ls:ls–ltr:"
#pwd
# ls
# ls–ltr
# Service HTTPD Stop
# History | Tail-3
histignore= Export "pwd:ls:ls-ltr:"
Service httpd Stop
Bayi History
[Note: The history command does not show pwd and LS]
How to use the history command in "Go" Linux