Linux system history (view execution command history) command detailed explanation

historyCommand 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

historyThe basic syntax of the command is as follows:

history [OPTION]... [N]

Among them, OPTIONvarious options are indicated, Nindicating the number of historical commands to be displayed. If Nthe 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

historyThe 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 historycommands, you need to pay attention to the following items:

  • History command records are saved in ~/.bash_historyfiles by default. If you need to save to other files, you can use HISTFILEenvironment 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 -cthe 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 -athe 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 -wthe 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 -rthe 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 HISTIGNOREenvironment 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 historycommands, 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 historycommands more efficiently.

  1. Use Ctrl+Rthe search history command

Ctrl+RCommands 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.

  1. !Execute history commands using

!Can be used to execute historical commands. For example, the !lsmost recently executed lscommand beginning with .

  1. !!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

  1. 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.

  1. Use history | grepthe search history command

history | grepCommands can be searched in the command history. For example, history | grep lsall included lshistory commands can be found.

  1. Use HISTSIZEan environment variable to set the length of the command history record

HISTSIZEEnvironment variables can be used to set the length of command history records. For example, HISTSIZE=1000you can set the length of historical command records to 1000.

  1. Use history -w && history -cthe clear history command to record

history -w && history -cCan 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.

  1. Use fcthe edit history command

fccommand can be used to edit historical commands. For example, fc -e vimyou can use the vim editor to open the most recently executed command.

Guess you like

Origin blog.csdn.net/u012581020/article/details/131635514
Recommended