有个姑娘叫 history

简介: 有个姑娘叫 history
history命令用于显示用户以前执行过的历史命令,并且能对历史命令进行追加和删除等操作。

常用参数

-a:    将当前shell会话的历史命令追加到命令历史文件中,命令历史文件是保存历史命令的配置文件
-c:    清空当前历史命令列表
-d:    删除历史命令列表中指定序号的命令
-n:    从命令历史文件中读取本次Shell会话开始时没有读取的历史命令
-r:    读取命令历史文件到当前的Shell历史命令内存缓冲区
-s:    将指定的命令作为单独的条目加入命令历史内存缓冲区。在执行添加之前先删除命令历史内存缓冲区中最后一条命令
-w:    把当前的shell历史命令内存缓冲区的内容写入命令历史文件

history的一些用法

[root@localhost ~]# history 20     # 显示最后20条历史命令
  234  ls gcc/
  235  tar czvf gcc.tar.gz gcc/
  236  ll
  237  scp gcc.tar.gz 192.168.10.150:/root
  238  yum -y install kernel-headers
  239  find /var/cache/yum/ -name "*.rpm"  -exec mv {} gcc/ \;
  240  cd g
  241  cd gcc/
  242  ll
  243  cp kernel-headers-3.10.0-1160.6.1.el7.x86_64.rpm 192.168.10.150:/root
  244  scp kernel-headers-3.10.0-1160.6.1.el7.x86_64.rpm 192.168.10.150:/root
  245  poweroff
  246  hostnamectl
  247  poweroff
  248  history
  249  history --help
  250  man history
  251  history man
  252  man history
  253  history 20

[root@localhost ~]# !242    # 执行第242条历史命令(上面可以看到第242条命令是ll)
ll   # 哼,那些内容少儿不宜,不给看~~~

[root@localhost ~]# !!    # 再次执行上一条历史命令
ll   # 没错,少儿不宜,还是不给你看

[root@localhost ~]# history -c    # 清空当前历史命令列表
[root@localhost ~]# history
    1  history     # 这下干净了

修改history命令默认保存的数量

[root@localhost ~]# echo $HISTSIZE
1000      # 系统默认保留的是1000条历史命令
[root@localhost ~]# sed -i 's/^HISTSIZE=1000/HISTSIZE=3000/' /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
3000      # 将默认的1000改为了3000

来给history穿衣服

上面咱也看到了,没有衣服的history是赤裸裸的,只有编号和命令,咱们现在给她穿衣服
[root@localhost ~]# vim /etc/profile.d/history.sh    # /etc/profile.d/下的.sh结尾的文件会在用户login或切换用户的时候自动执行
#!/bin/bash
export HISTFILE=$HOME/.bash_history   # 用户登录机器后每敲一个命令都会被记录到HISTFILE指定的文件中,而且是以追加的方式写入的 
export HISTSIZE=1200       # history命令最多输出1200行
export HISTFILESIZE=1200   # .bash_history文件中最多保留1200行
export HISTCONTROL=ignoredups   # 从命令历史中剔除连续重复的条目
# export HISTCONTROL=erasedups  # 清除整个历史命令中的重复条目
# export HISTCONTROL=ignorespace  # 强制 history不记住特定的命令,在不想被记住的命令前面输入一个空格
export HISTTIMEFORMAT="`whoami` %FT%T "  # 指定history的输出格式
shopt -s histappend      # 由于bash的history文件默认是覆盖,如果存在多个终端,最后退出的会覆盖以前历史记录,这个是改为追加形式
PROMPT_COMMAND="history -a"    # 实时追加history,不必等用户退出才将内存中的history记录到文件

[root@localhost ~]# source /etc/profile.d/history.sh

让我们重新认识一下history

[root@localhost ~]# history
    1  root 2020-12-11 11:42:51 history
    2  root 2020-12-11 11:44:02 echo $HISTSIZE
    3  root 2020-12-11 11:44:35 sed -i 's/^HISTSIZE=1000/HISTSIZE=3000/' /etc/profile
    4  root 2020-12-11 11:44:40 source /etc/profile
    5  root 2020-12-11 11:44:51 echo $HISTSIZE
    6  root 2020-12-11 11:45:34 vim /etc/profile
    7  root 2020-12-11 12:18:58 vim /etc/profile.d/history.sh
    8  root 2020-12-11 12:20:18 source /etc/profile.d/history.sh
    9  root 2020-12-11 12:20:21 history
'多了操作的用户以及操作的时间,是不是变好看了'
目录
相关文章
大声说出你对女神的爱!Geek is A choice. Girls make difference.
女王节来了,我们采访了来自于阿里云智能一线的6位geek girl,用两天的时间近距离观察她们快乐工作的,还在银泰百货的支持下绽放她们认真生(chou)活(mei)的光芒。 雏恬 我不想做被保护的女生,我想做改变世界的极客。

热门文章

最新文章