Linux command history

Linux command history

Commands that have been typed will be recorded by Linux, and 1000 historical commands can be recorded by default. These commands are saved in .bash_historyfiles in the user's home directory. .bash_historyCommands run in the current shell are saved to the file only when the user exits the current shell normally .

[root@centos-01 ~]# ls /root/.bash_history 
/root/.bash_history
[root@centos-01 ~]# cat !$

If the history command has not changed the environment variable, it can print the last 1000 historical commands by default.

[root@centos-01 ~]# history

The number of historical commands is determined by the environment variable HISTSIZE

[root@centos-01 ~]# echo $HISTSIZE
1000

/etc/profileThe environment variable HISTSIZE can be modified in the file

[root@centos-01 ~]# vi /etc/profile

If you modify the environment variable HISTSIZE and want it to take effect, you need to re-enter the terminal. Or execute the command source /etc/profileto make it take effect.

[root@centos-01 ~]# source /etc/profile

Clear the history command in the current memory

[root@centos-01 ~]# history -c
[root@centos-01 ~]# history
    1  history
[root@centos-01 ~]# cat .bash_history

As you can see, the history commands in the file are not emptied


Modify the format in which the history command is saved

[root@centos-01 ~]# history
    1  history
    2  cat .bash_history 
    3  vi /etc/profile
    4  source /etc/profile 
    5  source /etc/profile
    6  $HISTSIZE
    7  echo $HISTSIZE
    8  history
[root@centos-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@centos-01 ~]# history 
    1  2018/05/06 00:11:46 history
    2  2018/05/06 00:12:57 cat .bash_history 
    3  2018/05/06 00:16:12 vi /etc/profile
    4  2018/05/06 00:18:16 source /etc/profile 
    5  2018/05/06 00:18:34 source /etc/profile
    6  2018/05/06 00:19:41 $HISTSIZE
    7  2018/05/06 00:19:57 echo $HISTSIZE
    8  2018/05/06 00:20:27 history
    9  2018/05/06 00:24:36 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
   10  2018/05/06 00:24:54 echo $HISTTIMEFORMAT
   11  2018/05/06 00:28:57 history

In the newly opened terminal, look again at the environment variable HISTTIMEFORMAT

[root@centos-01 ~]# echo $HISTTIMEFORMAT

[root@centos-01 ~]#

It is found that modifying the environment variable HISTTIMEFORMAT before is only valid for the current terminal and invalid for other terminals.


To make the environment variable HISTTIMEFORMAT set valid, you can edit the /etc/profilefile.

[root@centos-01 ~]# vim /etc/profile

You can HISTSIZE=5000start a new line below and add

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

save and exit

[root@centos-01 ~]# source !$
source /etc/profile

Open a new terminal and view the environment variable HISTTIMEFORMAT

[root@centos-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

Permanently save history commands, you can increase hidden permissions

chattr +a ~/.bash_history

  • " !!"Consecutive two '!', means to execute the previous instruction
[root@centos-01 ~]# ls
anaconda-ks.cfg
[root@centos-01 ~]# !!
ls
anaconda-ks.cfg
  • "!n" (n is a number), means to execute the nth instruction in the history command. For example, " !1002" means to execute the 1002nd command in the command history.

  • "!string" (the string is greater than or equal to 1), look for the first command in the history command that starts with this string. For example, " !echo" means to execute the last command starting with "echo" in the command history.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325729529&siteId=291194637
Recommended