Shell学习笔记-history

history命令可以查看shell命令的历史记录. 这些记录默认存储在~/.bash_history中.1. 设置history命令显示的历史shell命令数history命令的运行机制是首先按照从~/.bash_history中读取HISTFILESIZE条历史记录到内存, 然后记录用户随后执行的shell命令, 使得内存中始终保存最多HISTSIZE条shell历史记录. 在结束会话时, 将内存中最近的HISTSIZE条记录合并到~/.bash_history中, 并且截断该文件使之只保留最近的HISTFILESIZE条shell历史记录. 关于HISTSIZE和HISTFILESIZE的详细介绍见 bash HISTSIZE vs. HISTFILESIZE?2. 显示时间戳设置export HISTTIMEFORMAT=’%F %T’会在执行history命令时显示时间戳

# export HISTTIMEFORMAT='%F %T '# history | more1  2008-08-05 19:02:39 service network restart2  2008-08-05 19:02:39 exit3  2008-08-05 19:02:39 id4  2008-08-05 19:02:39 cat /etc/redhat-release

3. 查询某个最近使用的命令使用CONTROL+R来查询某个最近使用的命令.4. 执行某个历史shell命令使用CONTROL+P或者上箭头来显示最近的一个shell命令.使用!!或者!-1来执行最近的一个shell命令.当使用history命令得到历史shell命令的编号时, 可以使用#n来执行第n个shell命令:

# history | more1  service network restart2  exit3  id4  cat /etc/redhat-release# !4cat /etc/redhat-releaseFedora release 9 (Sulphur)

?5. 设置存储shell历史记录的文件默认的存储文件为~/.bash_history, 可以通过~/.bash_profile设置:

# vi ~/.bash_profileHISTFILE=/root/.commandline_warrior

?6. 删除重复记录history命令的最主要作用是存储历史命令, 所以可以进行去重来存储更多的不同命令.去除连续的相同历史shell命令, 设置export HISTCONTROL=ignoredups:

# pwd# pwd# pwd# history | tail -444  pwd45  pwd46  pwd [Note that there are three pwd commands in history, afterexecuting pwd 3 times as shown above]47  history | tail -4# export HISTCONTROL=ignoredups# pwd# pwd# pwd# history | tail -356  export HISTCONTROL=ignoredups57  pwd [Note that there is only one pwd command in the history, even afterexecuting pwd 3 times as shown above]58  history | tail -4

删除重复历史shell命令(不管连续与否), 设置export HISTCONTROL=erasedups:

# export HISTCONTROL=erasedups# pwd# service httpd stop# history | tail -338  pwd39  service httpd stop40  history | tail -3# ls -ltr# service httpd stop# history | tail -635  export HISTCONTROL=erasedups36  pwd37  history | tail -338  ls -ltr39  service httpd stop[Note that the previous service httpd stop after pwd got erased]40  history | tail -6

?7. 清空历史记录使用history -c参考文献:[1].??bash HISTSIZE vs. HISTFILESIZE?[2]. 15 Examples To Master Linux Command Line History

Shell学习笔记-history

相关文章:

你感兴趣的文章:

标签云: