虚拟主机域名注册-常见问题服务器知识 → 操作系统

如何从 Linux 中清除命令行历史记录

  你可以使用历史记录命令来清除所有的历史记录或选定的命令行。在本教程中,您将学习如何在Linux、MacOS和类似Unix的系统中清除bash历史记录中的特定命令。

  如何查看行号的历史记录?

  只需输入history命令:

  $ history

  默认情况下,历史记录存储在~/.bash_history文件中。但是,名为$ HISTFILE的bash变量定义了保存命令历史记录的文件名。如果未设置,则退出shell时不会保存命令历史记录。使用printf命令/ echo命令显示当前设置:

  echo "$HISTFILE"

  printf "Bash history file - %s\n" $HISTFILE

  如何在Linux中删除历史记录中的单条命令编号为1013的命令

  ## Delete the bash history entry at offset OFFSET ##

  history -d offset

  history -d number

  history -d 1013

  验证一下:

  $ history

  Linux bash shell从历史文件中删除单个命令

  在位置OFFSET处删除bash历史记录条目。负偏移量从历史记录列表的末尾算起。

  如何删除所有历史记录?

  想删除所有的bash历史记录?试试下面的语法命令:

  $ history -c

  添加上面的命令到你的〜/ .bash_logout中的文件,以便在注销的时候进行清理:

  $ cat /dev/null > ~/.bash_history && history -c

  将-c传递给history命令删除所有条目$ {} HISTFILE要清除历史记录列表。

  提示:像专家一样控制bash历史记录

  首先,您可以通过在〜/ .bashrc文件中附加以下配置选项来增加bash历史记录的大小:

  ## Set the maximum number of lines contained in the history file ##

  HISTFILESIZE=5000000

  ## Set the number of commands to remember in the command history ##

  HISTSIZE=10000

  ## Append it ##

  shopt -s histappend

  ######

  # Controlling how commands are saved on the history file ##

  # ignoreboth means: ##

  # a) Command which begin with a space character are not saved in the history list ##

  # b) Command matching the previous history entry to not be saved (avoid duplicate commands) ##

  ######

  HISTCONTROL=ignoreboth

  保存并关闭文件。

  在哪里可以找到有关历史命令的更多信息?

  通过键入以下命令来阅读bash手册页:

  $ man bash

  另一个选项是键入以下命令:

  $ help history

  示例输出:




免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:bkook@qq.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
【 双击滚屏 】 【 推荐朋友 】 【 收藏 】 【 打印 】 【 关闭 】 【 字体: 】 
上一篇:美国服务器的性能怎么样
下一篇:怎样设置服务器系统启动或重新启动时执行命令或脚本
  >> 相关文章
没有相关文章。
0

在线
客服

在线客服服务时间:9:00-18:00

客服
热线

19899115815
7*24小时客服服务热线

关注
微信

关注官方微信
顶部