history命令

history命令学习

如果你经常使用Linux命令,那么使用history命令无疑会提升你的工作效率。
主要用于显示历史指令记录内容, 下达历史纪录中的指令 。

[root@zhang user1]# help history
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.

    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.

    Options:
      -c    clear the history list by deleting all of the entries 清空命令历史
      -d offset delete the history entry at offset OFFSET. 删除指定位置的命令历史

      -a    append history lines from this session to the history file 追加历史行到历史文件中
      -n    read all history lines not already read from the history file 从历史文件中读取所有命令历史
      -r    read the history file and append the contents to the history
        list 读取所有命令历史文件追加history 列表中
      -w    write the current history to the history file 
        and append them to the history list 写当前的历史记录到文件中去

      -p    perform history expansion on each ARG and display the result
        without storing it in the history list 
      -s    append the ARGs to the history list as a single entry

    If FILENAME is given, it is used as the history file.  Otherwise,
    if $HISTFILE has a value, that is used, else ~/.bash_history.

    If the $HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
命令历史
环境变量
HISTSIZE:命令历史的条数
HISTFILE:默认为~/.bash_history
HISTFILESIZE:HISTFILE文件记录历史的条数
history 常用命令
-d   删除指定的命令
-c   清空命令
-a   手工追加当前会话的命令历史到历史文件中去
 n   显示最近的n条命令
调用历史命令
!#    重复执行第#条命令
!!    重复执行上一条命令
!str  执行指定str开头的命令(最后一个)
!?    获得最后执行的状态码
控制命令历史的记录方式

主要和HISTCONTROL这个环境变量有关(/etc/profile

ignoredups   忽略重复
ignorespace  忽略空白开头
ignoreboth   上面2个都启用

实战

使用 HISTTIMEFORMAT 显示时间戳

# export HISTTIMEFORMAT='%F %T'
# history | more
    1  2018-1-18 12:05:23ls
    2  2018-1-18 12:05:23vim /etc/sudoers
    3  2018-1-18 12:05:23ls
    4  2018-1-18 12:05:23cd /home/ruanyang/
    5  2018-1-18 12:05:23ls
    6  2018-1-18 12:05:23cd 下载
    7  2018-1-18 12:05:23ls
    8  2018-1-18 12:05:23cd ctags-5.8/
备注:这个环境变量的声明只能作用于当前的bash,所以如果长久有效的话,需要将其定义到/etc/profile文件中

使用 HISTSIZE 控制历史命令记录的总行数

将这两行内容追加到/etc/profile文件中,当你再次重新登录bash时,历史命令总行数会变成100000

export HISTSIZE=100000
export HISTFILESIZE=100000
备注:如果我们想要看到更多的历史命令时,不妨使用这个变量

使用 HISTFILE 更改历史文件名称

默认情况下,历史命令存放在~/.bash_history文件中。如下,重新定位历史命令存放位置

export HISTFILE=/.logs/history_${LOGNAME}
备注:这样可以将每个用户的历史文件清晰的使用文件名来标记,方便分析

使用 HISTCONTROL 从命令历史中剔除连续重复的条目


HISTCONTROL=ignoredups剔除连续的相同命令的条目,仅剩余一条,如下:
# export HISTCONTROL=ignoredups
# cd
# cd
# cd
我们现在来看看效果吧!

# history | tail -n 5
  133  2018-1-18 17:51:30history 
  134  2018-1-18 18:09:54export HISTCONTROL=ignoredups
  135  2018-1-18 18:09:59cd
  136  2018-1-18 18:10:04history 
  137  2018-1-18 18:22:12history | tail -n 5
三个cd变成一个了

使用 HISTCONTROL 清除整个命令历史中的重复条目

HISTCONTROL=erasedups历史中的重复命令被清除到仅剩距离当前时间最近的那条命令

# export HISTCONTROL=erasedups
# pwd
# cd
# ls
# pwd
# pwd
看看效果吧!pwd剔除剩一个啦

# history | tail -10
  100  2018-1-18 18:34:47export HISTCONTROL=erasedups
  101  2018-1-18 18:34:58cd
  102  2018-1-18 18:34:58ls
  103  2018-1-18 18:35:07pwd
  104  2018-1-18 18:35:20history | tail -10

使用 HISTCONTROL 强制 history 不记住特定的命令

HISTCONTROL=ignorespace,在不想被记住的命令前面输入一个空格

$ export HISTCONTROL=ignorespace
$ ls
$  cd
$ pwd
注意,我在cd前输入了一个空格,我们一起来看看效果吧!cd消失啦!

$ history | tail -5
 1997  more sources.list
 1998  export HISTCONTROL=ignorespace
 1999  ls
 2000  pwd
 2001  history | tail -5

使用HISTSIZE禁用history

如果想禁用history,可以将HISTSIZE设置为0:

$ export HISTSIZE=0
$ history 

使用HISTIGNORE忽略历史中的特定命令

忽略pwd、ls命令:

$ export HISTIGNORE="pwd:ls:"
$ pwd
$ cd
$ ls
看看效果吧!

$ history 4
 1998  export HISTIGNORE="pwd:ls:"
 1999  cd
 2000  history 5
  • 除了使用history命令,在 shell 或 GUI 终端提示下,你也可以使用上下方向键来翻阅命令历史(向下箭头会向前翻阅),直到你找到所需命令为止。这可以让我们很方便地编辑前面的某一条命令,而不用重复输入类似的命令。
  • History命令的用途确实很大!但需要小心安全的问题!尤其是 root 的历史纪录档案,这是黑客们的最爱!因为不小心的 root 会将很多的重要资料在执行的过程中会被纪录在 ~/.bash_history 当中,如果这个档案被解析的话,后果不堪设想!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值