history命令简介:

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


1.命令格式:

history [n]   n为数字


2.命令功能:

1.查看命令历史

2.清空命令历史

3.另存命令历史


3.命令参数:

    -c:清空命令历史

    -d [n]: 删除指定位置的命令

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


4.使用实例:


实例一:查看最后10个命令历史

 [root@fox default]# history 10
 1218  passwd tom
 1219  file useradd
 1220  cd /etc/default/
 1221  ll
 1222  file useradd
 1223  cat useradd
 1224  q
 1225  man history
 1226  man 1 history
 1227  history 10



实例一:使用! 执行历史命令。

1、 ! number 执行第几条命令

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

3、 !! 执行上一条


[root@fox default]# !1222
file useradd
useradd: ASCII text


[root@fox default]# !fil
file useradd
useradd: ASCII text


[root@fox default]# !!

file useradd
useradd: ASCII text


实例二: history配置修改

  eg1、history记录的行数

    [root@fox tmp]# echo $HISTSIZE
    1000


  eg2、默认记录1000行 配置文件在/etc/profile中修改

   [root@fox tmp]# 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


      [root@fox ~]# history 6
     1238  cat .bash_history
     1239  rpm -q bind-lib


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