有个姑娘叫 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
'多了操作的用户以及操作的时间,是不是变好看了'
目录
相关文章
|
6月前
|
存储 安全 PHP
ctf.show 愚人杯2
ctf.show 愚人杯
55 0
|
6月前
|
存储 数据安全/隐私保护 索引
ctf.show 愚人杯1
ctf.show 愚人杯
28 0
|
Web App开发 前端开发 JavaScript
一晚上做了一个xpath终结者:xpath-helper-plus
作为一个资深『xpath』提取工程师,想要快速从页面中拿到数据,肯定需要借助一些工具, 而最初接触的就是`xpath-helper`这块浏览器插件。使用一段时间后,发现笔者有一些特别的需求,想在此基础上扩展一下。 于是乎就学习了如何开发chrome插件?如何使用自己属性的vue来开发?经过双休一顿文档、gayhub了解之后,有了这样一款工具。
283 0
|
算法
每日一题冲刺大厂第十三天 cow
大家好,我是泡泡,给大家带来每日一题的目的是为了更好的练习算法,我们的每日一题这个月进度是数据结构,让大家练到各种各样的数据结构题目,熟悉数据结构的增删改查,一年以后,蜕变成为一个不一样的自己!
77 0