linux之history使用技巧

听闻少年二字,当与平庸相斥。这篇文章主要讲述linux之history使用技巧相关的知识,希望能为你提供帮助。
/etc/bashrc配置history的变量

> vim /etc/bashrc export HISTTIMEFORMAT="%Y-%m-%d:" > source /etc/bashrc > history 10062021-05-19:vim /etc/bashrc 10072021-05-19:history 10082021-05-19:source /etc/bashrc 10092021-05-19:history

> vim /etc/bashrc export HISTTIMEFORMAT="%F %T:" > source /etc/bashrc > history 10092021-05-19 22:35:24:history 10102021-05-19 22:36:40:vim /etc/bashrc 10112021-05-19 22:37:02:source /etc/bashrc 10122021-05-19 22:37:07:history

> vim /etc/bashrc export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami`" > source /etc/bashrc > history 10122021-05-19 22:37:07 root:history 10132021-05-19 22:37:41 root:vim /etc/bashrc 10142021-05-19 22:38:06 root:source /etc/bashrc 10152021-05-19 22:38:08 root:history

> vim /etc/bashrc export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `who am i | awk \'{print $1,$5}\'`" > source /etc/bashrc > history 10212021-05-19 22:42:59 root (171.43.176.3):history 10222021-05-19 22:43:03 root (171.43.176.3):source /etc/bashrc 10232021-05-19 22:43:06 root (171.43.176.3):vim /etc/bashrc 10242021-05-19 22:43:19 root (171.43.176.3):source /etc/bashrc 10252021-05-19 22:43:21 root (171.43.176.3):history

history设置历史命令记录数
> vim /etc/bashrc # 保存1000条记录 export HISTSIZE=1000 > source /etc/bashrc

指定不同终端执行的命令都存储在同一个文件中
> vim /etc/bashrc export HISTFILE=/var/log/.cmd_log > source /etc/bashrc

忽略重复命令
> vim /etc/bashrc export HISTCONTROL=ignoredups > source /etc/bashrc

多个终端同时操作时,避免命令覆盖,采用追加方式
> shopt -s histappend

实时写入
> PROMPT_COMMAND=\'history -a\'

完全清除命令执行操作的历史记录
  • 清理历史记录(只是清理了表层,可在.bash_history找到回)
> history

  • 清理记录历史的记录文件内容
> echo " " > .bash_history

关于history的小技巧
  • !number 执行第几条命令
  • !command 从最近的命令查到以command开头的命令执行
  • !! 执行上一条
【linux之history使用技巧】原文链接:https://rumenz.com/rumenbiji/linux-history-skill.html
微信公众号:入门小站

    推荐阅读