前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >history命令

history命令

作者头像
咻一咻
发布2020-05-29 11:40:23
5900
发布2020-05-29 11:40:23
举报
文章被收录于专栏:咻一咻咻一咻

history命令学习

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

代码语言:javascript
复制
[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.
命令历史
环境变量
代码语言:javascript
复制
HISTSIZE:命令历史的条数
HISTFILE:默认为~/.bash_history
HISTFILESIZE:HISTFILE文件记录历史的条数
history 常用命令
代码语言:javascript
复制
-d   删除指定的命令
-c   清空命令
-a   手工追加当前会话的命令历史到历史文件中去
 n   显示最近的n条命令
调用历史命令
代码语言:javascript
复制
!#    重复执行第#条命令
!!    重复执行上一条命令
!str  执行指定str开头的命令(最后一个)
!?    获得最后执行的状态码
控制命令历史的记录方式

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

代码语言:javascript
复制
ignoredups   忽略重复
ignorespace  忽略空白开头
ignoreboth   上面2个都启用

实战

使用 HISTTIMEFORMAT 显示时间戳

代码语言:javascript
复制
# 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 控制历史命令记录的总行数

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

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

使用 HISTFILE 更改历史文件名称

代码语言:javascript
复制
默认情况下,历史命令存放在~/.bash_history文件中。如下,重新定位历史命令存放位置

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

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

代码语言:javascript
复制
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 清除整个命令历史中的重复条目

代码语言:javascript
复制
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 不记住特定的命令

代码语言:javascript
复制
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

代码语言:javascript
复制
如果想禁用history,可以将HISTSIZE设置为0:

$ export HISTSIZE=0
$ history 

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

代码语言:javascript
复制
忽略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 当中,如果这个档案被解析的话,后果不堪设想!
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • history命令学习
    • 命令历史
      • 环境变量
      • history 常用命令
      • 调用历史命令
      • 控制命令历史的记录方式
  • 实战
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档