history
Command is a commonly used command in Linux/Unix systems, which is used to view the history of commands executed by the current user on the command line. This command allows users to view, search, edit and execute previously executed commands, providing users with convenient and fast operation methods. This article will explain the various usages and precautions of commands in a comprehensive and detailed manner history
, and provide some practical tips and tricks.
command syntax
history
The basic syntax of the command is as follows:
history [OPTION]... [N]
Among them, OPTION
various options are indicated, N
indicating the number of historical commands to be displayed. If N
the parameter is omitted, all historical commands are displayed by default.
For example, to display the last 10 historical commands:
history 10
Or show all history commands:
history
Common options
history
The command has many options, some of which are commonly used are described below:
-
-c
: Clear the historical command records. -
-a
: Append the commands executed in the current session to the history command record. -
-w
: Write historical command records to the historical command file (default is~/.bash_history
). -
-r
: Read the command records in the history command file and add them to the current command history. -
-n
: Display the latest N historical commands. -
-p
: Display historical commands starting with the specified string. -
-s
: Add the specified string to the command history.
Here are some examples:
- Clear command history:
history -c
- Append the commands executed in the current session to the history command record:
history -a
- Write historical command records to the historical command file:
history -w
- Read command records from the history command file and add them to the current command history:
history -r
- Display the last 10 historical commands:
history -n 10
- Display historical commands starting with the specified string:
history -p "ls"
- Add the specified string to the command history:
history -s "echo 'Hello, World!'"
Precautions
When using history
commands, you need to pay attention to the following items:
-
History command records are saved in
~/.bash_history
files by default. If you need to save to other files, you can useHISTFILE
environment variables to specify. -
All executed commands, including sensitive information such as passwords and private keys, are saved in the historical command record. To protect privacy, sensitive information should not be saved to the history command record.
-
If you need to clear the historical command record, you can use
history -c
the command. However, this does not delete the historical command files, it just empties the command history for the current session. -
If you need to append the commands executed in the current session to the historical command record, you can use
history -a
the command. However, this doesn't immediately write the command to the history command file, it just appends the command to the current session's history. -
If you need to write historical command records to the historical command file, you can use
history -w
the command. However, this does not immediately clear the history of the current session, it just writes the historical command records to the historical command file. -
If you need to read the command records in the historical command file and add them to the current command history, you can use
history -r
the command. However, this overwrites the current session's history, so it should be used with caution. -
If you want to exclude certain commands from the command history, you can use
HISTIGNORE
environment variables. This variable specifies a colon-separated list of commands that will be ignored. -
If you need to add comments in the command history, you can use
#
characters. When executing the command,#
everything after the character will be ignored and will not be saved in the history command record. -
When using
history
commands, you should pay attention to the order of historical command records. Historical command records are arranged in reverse order of execution time, with the most recently executed command at the top.
Practical tips and tricks
In addition to the common options and precautions mentioned above, some practical tips and tricks are introduced below to help users use history
commands more efficiently.
- Use
Ctrl+R
the search history command
Ctrl+R
Commands can be searched in the command history. Just press Ctrl+R
, then enter part of the command you want to search for to find recent commands containing that string.
!
Execute history commands using
!
Can be used to execute historical commands. For example, the !ls
most recently executed ls
command beginning with .
!!
Execute the previous command using
!!
Can be used to execute the previous command. For example, if the previous command was ,ls -l
the command will be executed.!!
ls -l
- Use
!$
the last argument referencing the previous command
!$
Can be used to refer to the last argument of the previous command. For example, if the previous command was ls -l /var/log
, !$
it will be quoted /var/log
.
- Use
history | grep
the search history command
history | grep
Commands can be searched in the command history. For example, history | grep ls
all included ls
history commands can be found.
- Use
HISTSIZE
an environment variable to set the length of the command history record
HISTSIZE
Environment variables can be used to set the length of command history records. For example, HISTSIZE=1000
you can set the length of historical command records to 1000.
- Use
history -w && history -c
the clear history command to record
history -w && history -c
Can be used to clear history command records. This command writes the historical command records of the current session into the historical command file, and clears the historical command records of the current session.
- Use
fc
the edit history command
fc
command can be used to edit historical commands. For example, fc -e vim
you can use the vim editor to open the most recently executed command.