How to use the history command in Linux

  
                

Through the history command under Linux, we can view the used commands, that is, the history commands. Today, this article will introduce the specific usage of history under Linux. Let's take a look at it.

First, what is the history

In the bash function, it can memory used commands, this feature is the biggest benefit is that you can query once did move!

So you can know your running steps, then you can track the commands you have issued as a tool for debugging!

Second, the preservation of History

So where is the command recorded? In the .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 that are run this time are cached in memory. When you successfully log out of the system, the command The memory will be recorded in .bash_history!

Third, call the history command

Next, learn the usage of the history history command.

The code is as follows:

history [n]

[root@jb51 Desktop]#history [-c]

[root@jb51 Desktop]# History [-raw] histfiles

Options and arguments:

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

-c : Eliminate all history content in the current shell

-a : Add the currently added history command to histfiles, if no histfiles are added,

By default, ~/.bash_history

-r: reads the contents of histfiles into the history memory of the current shell;

-w : writes the current history memory contents In the histfiles!

Example 1, listing all history records in the current memory

The code is as follows:

[root@jb51 Desktop]#history

#前Omit

1017 man bash

1018 ll

1019 history

1020 history

Among the listed information, there are two columns The first column is the code for the command in this shell, and the other is the content of the command itself! As for how many command records will be displayed, it is related to HISTSIZE!

Example 2, listing the current 3 data

The code is as follows:

[root@jb51 Desktop]#history 3

1019 history< Br>

1020 history

1021 history 3

Example 3, immediately write the current data to the histfile

The code is as follows:

[ Root@jb51 Desktop]#history -w

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

[root@jb51 Desktop]#echo $HISTSIZE

1000

Fourth, the use of history

1,! Use

(1)! ! Repeat the previous command

(2)! Character Repeat the previous command starting with “character" character

(3)! Num Repeat the corresponding command according to the serial number in the history command output

(4)! ? Abc repeats the previous command containing abc

(5)! -n Repeat the command before n commands

2. Key combination

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

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

The code is as follows:

[root@jb51 Desktop]#

(reverse-i-search)`‘:

(reverse-i-search)`h’: cat /etc/shadow

Press Enter to execute the command

(3) To recall the parameters in the previous command

Esc + . (Click the Esc key, then click the . key)

Note:

History is stored in each user's own history and is located in the user's home directory.

After the user logs in, the execution command is stored in the memory and can only be seen after logging in.

Five, how to ensure that each time you log in the situation history

In the user's home directory, edit ~/.bash.logout add a history-c save and exit.

The above is the usage of the history command under Linux. This article only briefly introduces the basic knowledge of the history command. In fact, there are many ways to use the history command. I will not introduce them here.

Copyright © Windows knowledge All Rights Reserved