zl程序教程

您现在的位置是:首页 >  系统

当前栏目

CentOS 显示历史执行过的命令以及用户历史命令缓存文件

centos文件缓存执行命令 用户 显示 以及
2023-09-14 09:02:30 时间

1、history命令用于显示历史执行过的命令

执行 history命令能显示出当前用户在本地计算机中执行过的最近 1000 条命令记录。

如果觉得 1000 不够用,还可以自定义/etc/profile 文件中的HISTSIZE 变量值。

在使用 history 命令时,如果使用-c 参数则会清空所有的命令历史记录。

[root@bigdata-senior01 /]# history
   36  cat ~/.bash_history
   37  history
   38  cat ~/.bash_history
   39  cat ~/.bash_history | less
   40  history
   41  ls
   42  free -h
   43  history

2、使用“!编码数字”的方式来重复执行某一次的命令。

[root@bigdata-senior01 /]# !42
free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        156M        1.5G        8.5M        178M        1.5G
Swap:          2.0G          0B        2.0G

 

3、历史命令会被保存到用户家目录中的.bash_history 文件中。Linux 系统中以点(.)开头的文件均代表隐藏文件,这些文件大多数为系统服务文件,可以用 cat 命令查看其文件内容。

查看自己的home下的用户目录的历史命令记录
“~”波浪线,表示用户家目录
[root@bigdata-senior01 /]# cat ~/.bash_history
~”波浪线:
[root@bigdata-senior01 ~]# su - es 上一次登录:六 1月 5 21:48:54 CST 2019pts/0 上 [es@bigdata-senior01 ~]$ pwd /home/es [es@bigdata-senior01 ~]$ cd / [es@bigdata-senior01 /]$ ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [es@bigdata-senior01 /]$ pwd / [es@bigdata-senior01 /]$ cd ~ [es@bigdata-senior01 ~]$ pwd /home/es

 4、在histrory记录中加入时间戳

export HISTTIMEFORMAT="%F %T "

[root@bigdata-senior01 ~]# export | grep HIST
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T "

history显示:

 1024  2019-01-05 22:17:21 export HISTTIMEFORMAT="%F %T "
 1025  2019-01-05 22:17:24 history
 1026  2019-01-05 22:19:00 export
 1027  2019-01-05 22:19:09 export | grep HIST
 1028  2019-01-05 22:19:41 history | less
 1029  2019-01-05 22:19:53 history