If you often use Linux command lines, using the history (history) command can effectively improve your efficiency. This article describes the 15 use cases of the history Command. Use HISTTIMEFORMAT to display the timestamp when you execute the history command from the command line...
If you often use Linux command lines, using the history (history) command can effectively improve your efficiency. This article describes the 15 use cases of the history Command.
Display timestamp with HISTTIMEFORMAT
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 | more
1 2008-08-05 19:02:39 service network restart
2 19:02:39 exit
3 2008-08-05 19:02:39 id
4 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 tagged with the correct timestamp. All commands earlier than this will display the time for setting the HISTTIMEFORMAT variable. [Thank you for adding the following content to the readers]
Use Ctrl + R to search for history
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-release
Fedora release 9 (Sulphur)
Repeat the previous command
You can run the previous command again in four ways:
Use the up arrow key and press enter to execute.
Press !! And press enter to execute.
Enter! -1 and press enter to execute.
Press Ctrl + P and press enter.
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 | more
1 service network restart
2 exit
3 id
4 cat/etc/redhat-release
#! 4
Cat/etc/redhat-release
Fedora release 9 (Sulphur)
Execute previous commands by specifying keywords
In the example below, enter! Ps and press Enter. the following command is executed to start with ps:
#! Ps
Ps aux | grep yp
Root 16947 0.0 0.1 36516 1264? Sl ypbind
Root 17503 0.0 0.0 4124 740 pts/0 S + grep yp
Use HISTSIZE to control the total number of historical Command Records
Append the following two lines to the. bash_profile file and log on to the bash shell again. The number of command history records will change to 450:
# Vi ~ /. Bash_profile
History size = 450
HISTFILESIZE = 450
Use HISTFILE to change the name of a historical file
By default, the command history is stored in ~ /. Bash_history file. Add the following content to the. bash_profile file and log on to the bash shell again. the. commandline_warrior command history will be stored:
# Vi ~ /. Bash_profile
HISTFILE =/root/. commandline_warrior
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
# Pwd
# Pwd
# History | tail-4
44 pwd
45 pwd
46 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
# Pwd
# Pwd
# History | tail-3
56 export HISTCONTROL = ignoredups
57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58 history | tail-4
Use HISTCONTROL to clear duplicate entries in the history of the entire command
In the previous example, ignoredups 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-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
[Note that the previous service httpd stop after pwd got erased]
40 history | tail-6
Use HISTCONTROL to force history not to remember specific commands
Set HISTCONTROL to ignorespace and enter a space before the command 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 use the-c option to clear all command history
To clear all command history, run the following command:
# History-c
Command replacement
In the following example ,!! : $ Get the parameters of the previous command for the current command:
# Ls anaconda-ks.cfg
Anaconda-ks.cfg
# Vi !! : $
Vi anaconda-ks.cfg
Supplement: use! $ Can achieve the same effect, and is simpler. [Thanks to wanzigunzi.]
In the following example ,! ^ Obtain the first parameter from the previous command:
# Cp anaconda-ks.cfg anaconda-ks.cfg.bak
Anaconda-ks.cfg
# Vi-5! ^
Vi anaconda-ks.cfg
Replaces 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: 2
Ls-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
Disable history with HISTSIZE
If you want to disable history, you can set HISTSIZE to 0:
# Export HISTSIZE = 0
# History
# [Note that history did not display anything]
Use HISTIGNORE to ignore specific commands in history
In the following example, commands such as pwd, ls, and ls-ltr are ignored:
# Export HISTIGNORE = "pwd: ls-ltr :"
# Pwd
# Ls
# Ls-ltr
# Service httpd stop
# History | tail-3
79 export HISTIGNORE = "pwd: ls-ltr :"
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls-ltr]
If you often use Linux commands, using the history Command will undoubtedly improve your work efficiency.
The History command is mainly used to display the History Command Records and issue commands in the History records.
1> History command syntax:
[Test @ linux] # history [n]
[Test @ linux] # history [-c]
[Test @ linux] # history [-raw] histfiles
Parameters:
N: Number. list the last n commands.
-C: removes all history content in the current shell.
-A: adds the newly added history Command to histfiles. if histfiles is not added,
Write by default ~ /. Bash_history
-R: read the content of histfiles to the history memory of the current shell.
-W: write the current history memory into histfiles
In Linux, when you enter and execute commands in shell (console), shell will automatically record your commands to the history list, which is generally stored in the user directory. bash_history file. By default, 1000 entries are saved. you can change this value.
If you type history, history will show you the first 1000 historical commands you are using and mark them, you will see a list of numbers quickly rolled out from the screen. You may not need to view all the projects in the 1000 commands. you can also add a number to list the latest n commands.
In linux, the history command not only allows us to query historical commands, but also allows us to execute commands using related functions.
2> run specific historical commands
History lists all history commands saved by bash and signs them. we can run specific historical commands in the way of "exclamation mark and number.
Syntax description:
[Test @ linux] # [! Number] [! Command] [!]
Parameter description:
Number: the meaning of the instruction;
Command: The beginning of a command.
! : The meaning of the previous command!
3> History Command practice
List all history records:
[Test @ linux] # history
Only the last 10 records are listed:
[Test @ linux] # history 10 (note that there are spaces between history and 10)
Use the command record number to execute the command and execute the 99th commands in the history list
[Test @ linux] #! 99 (! There is no space in the middle of "and" 99)
Repeat the previous command
[Test @ linux] #!
Run the last command starting with rpm (!? ? It indicates a String, which can be input at will. Shell searches forward from the last historical command, and the first matched command will be executed .)
[Test @ linux] #! Rpm
List all history records on a screen:
[Test @ linux] # history | more
Clear all history commands of history now
[Test @ linux] # history-c
In addition to the history command, you can also use the up and down arrow keys to read the Command history at the shell or GUI terminal prompt (the down arrow will read forward) until you find the required command. This allows us to easily edit a previous command without having to repeat similar commands.
The History command is really useful! But be careful about security issues! Especially the root history file, which is a favorite of hackers! Because the careless root will record a lot of important information during execution ~ /. In bash_history, if this file is parsed, the consequences will be unimaginable.