Query historical commands and use (history)

Source: Internet
Author: User

1. What is history?

In the bash function, it can remember the commands used. The biggest benefit of this function is that it can query previous actions! So that you can know the operation steps, you can track the commands you have issued as a debugging tool!

Ii. Save History

So where is the command record? In. bash_history in the home directory! However, it should be noted that ,~ /. Bash_history records the commands that were run before the previous login, And the commands run during this login are cached in the memory. After you successfully log out of the system, this command is recorded in memory. bash_history!

Iii. Call History commands

We have learned the advantages of history from above, so let's learn about its usage!

History [n]

[Root @ instructor Desktop] # history [-c]

[Root @ instructor Desktop] # history [-raw] histfiles

Options and parameters:

N: Number, meaning to list the last n command line tables!

-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 to histfiles!

Example 1: List all history memories in the current Memory

[Root @ instructor Desktop] # history

# Omitted earlier

1017 man bash

1018 ll

1019 history

1020 history

The information listed is divided into two columns. The first column is the code of the command in this shell, and the other is the content of the command itself! The number of Command records displayed depends on the HISTSIZE!

Example 2: List the latest three data records

[Root @ instructor Desktop] # history 3

1019 history

1020 history

1021 history 3

Example 3: immediately write the current data to histfile

[Root @ instructor Desktop] # history-w

# By default, the history will be written ~ /. Bash_history!

[Root @ instructor Desktop] # echo $ HISTSIZE

1000

Iv. Tips for historical use

1 ,! Use

(1 !! Repeat the previous command

(2! Repeat the previous command starting with "character"

(3! Num repeats the corresponding command according to the serial number in the output of the history command.

(4 !? Abc repeats the previous command that contains abc.

Success! -N: The command before repeating n commands

2. Key combination

(1) Use the up and down keys to browse the commands previously executed

(2) Type ctr + r to search for commands in the command history.

[Root @ instructor Desktop] #

(Reverse-I-search )'':

(Reverse-I-search) 'H': cat/etc/shadow

Press enter to execute this command

(3) re-call the parameters in the previous command.

Esc +. (click Esc and then)

Note:

History is stored in the user's home directory in each user's own History. After a user logs on, the command is stored in the memory and can only be seen after logon.

5. How to Ensure the situation of history at every login launch

In the desired user's home directory, edit ~ /. Bash. logout: add the history-c command to save and exit.

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.