QQ叫紫珊 发表于 2018-5-19 09:49:30

Linux命令:history

  history命令简介:


linux的history命令的作用是,记录执行过的命令。 用法: history
n为数字,列出最近的n条命令 -c 将目前shell中的所有history命令消除 history [-raw] histfiles
-a 将目前新增的命令写入histfiles, 默认写入~/.bash_history -r
将histfiles内容读入到目前shell的history记忆中 -w 将目前history记忆的内容写入到histfiles


1.命令格式:
history
   n为数字


2.命令功能:

1.查看命令历史
2.清空命令历史
3.另存命令历史


3.命令参数:
      -c:清空命令历史
      -d : 删除指定位置的命令

      -w:保存命令历史至历史文件中
  

  
4.使用实例:


  实例一:查看最后10个命令历史
  # history 10
1218passwd tom
1219file useradd
1220cd /etc/default/
1221ll
1222file useradd
1223cat useradd
1224q
1225man history
1226man 1 history
1227history 10
  



  实例一:使用! 执行历史命令。
   1、 ! number 执行第几条命令

  2、 ! command 从最近的命令查到以command开头的命令执行

  3、 !! 执行上一条
  

  # !1222
file useradd
useradd: ASCII text
  

  # !fil
file useradd
useradd: ASCII text
  

  # !!

  file useradd
useradd: ASCII text
  

  实例二: history配置修改
  eg1、history记录的行数
  # echo $HISTSIZE
    1000
  

  eg2、默认记录1000行 配置文件在/etc/profile中修改
  # cat /etc/profile -n
  48    HISTSIZE=1000
  

  eg3、历史命令文件记录在 ~/.bash_history中
    想要让linux的history命令显示时间,history是默认不带时间, 在/etc/profile 中增加
export HISTTIMEFORMAT="%y-%m-%d %H:%M:%S "  

  eg4、查看.bash_history
head ~/.bash_history
实例三:同一账号同时多次登录写入history
  当以bash登录系统时,系统会从~/.bash_history读取以前运行的命令,

  当注销时,把最新的1000(HISTSIZE)条命令更新到~/.bash_history文件中。

  当同一账号,同时登录多个bash时,只有最后一个退出的会写入bash_history,其他的都被覆盖。
  

  history -w 强制立刻写入,仅保留最新的。
  

实例四:Ctrl+r 反向查询历史命令
  使用Ctrl+r反向查询历史命令,将匹配的最新一条显示出来 如果还想继续向上查询,继续按Ctrl+r
  

  # history 6
   1238cat .bash_history
   1239rpm -q bind-lib
  

  (reverse-i-search)`rpm': rpm -q bind-lib         <<== Ctrl+r后输入rpm
  

  

  
页: [1]
查看完整版本: Linux命令:history