Linux命令:history

显示历史(执行过的)命令。 

history [n]

history -c

history -d offset

history -anrw [filename]

history -p arg [arg ...]

history -s arg [arg ...]

不带任何参数显示所有执行过的命令。一个命令一行,每行前有行号。带*,表示曾被修改过。

n   ,显示最后(最近)执行的n命令。

-c ,清空当前(缓冲区中)历史命令。

-w,把缓存中的命令写入历史命令保存文件~/.bash_history

历史命名文件bash_history中保存的与终端上history列出的并不完全相同。

history列出的不仅有文件中的,还有本次登录后执行的命令。需要正确注销后才会写入文件
bash_history文件中保存的只有上一次正确注销后的命令。

-N: 显示历史记录中最近的N个记录;
-a:将历史命令缓冲区中命令写入历史命令文件中;
-r:将历史命令文件中的命令读入当前历史命令缓冲区;
-d <offset>:删除历史记录中第offset个命令
-n <filename>:读取指定文件

history缓冲区和记录文件

history命令读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入命令文件。

历史命令是被保存在内存中的,当退出或者登录shell时,会自动保存或读取。在内存中,历史命令仅能够存储1000条历史命令,该数量是由环境变量 HISTSIZE进行控制。默认是不显示命令的执行时间,命令的执行时间,history 已经记录,只是没有显示。

注:如想查询某个用户在系统上执行了什么命令,可以使用root用户身份登录系统,检查Home目录下的用户主目录下的“.bash_history文件,该文件记录了用户所使用的命令和历史信息。

 配置文件/etc/profile

HISTSIZE ,默认保存1k条,可以在环境变量配置文件/etc/profile中修改 HISTSIZE=xxx
HISTFILE,默认保存文件修改 HISTFILE=/path/FILENAME。 默认为~/.bash_history。
HISTCONTROL,剔除连续重复的条目 HISTCONTROL=ignoredups
HISTIGNORE,忽略指定命令HISTIGNORE=”pwd:ls:cd:”
HISTTIMEFORMAT,格式化输出HISTTIMEFORMAT=’%F %T ’ ,bt只能输出设置生效之后的

调用历史命令

方法1:  Ctrl+R。直接键入想要查询的命令,进行动态模糊查询:直接回车可以执行 。当前的提示符会变成搜索提示,见下图:

要取消查找,按Esc即可。

方法2:  使用上下箭头调用以前的历史命令

方法3:  使用 !n 重复执行第n条历史命令

方法4:  重复执行上一条命令。 !! , !-1 或Ctrl+p。不同是,Ctrl+p显示上一条命令但不执行;其他调用是直接执行。

方法5:  !word。重复执行最后一条以word开头的命令。使用word表示而不是str表示,意思是只能是不含空白的可见字符。


!:0 执行上一次命令中的单独命令
如:> ls -l /etc/motd, !:0 会执行ls


!?word 搜索包含word的
!$:p     显示上一个命令的最后一个参数。并不执行。
!*   扩展为上一命令的所有参数。

显示执行历史记录时间

1、默认情况下,history并不记录命令的执行时间,分析起来很困难。我们可以使用HISTTIMEFORMAT在历史中显示TIMESTAMP。
2、把上面的 export HISTTIMEFORMAT="%F %T " 命令加入到~/.bashrc中即可在登录后自动开启history时间戳了。添加完成后,执行source .bashrc 让其生效。仅针对当前账号有效。
3、其实HISTTIMEFORMAT的格式就是 strftime函数的格式,比如上面的"%F %T ",%F表示显示出Y-M-D格式的日期,%T表示显示出H-M-S这样格式的时间。
4、用Ctrl+R搜索历史命令;当你执行了一串相当长的命令之后,你只要用关键字搜索一下历史命令然后重新执行这条命令而不需要将整条命令再输一遍。

方法是:按下Ctrl+R然后输入关键字。如搜索”date”,则显示历史的命令中含有”date”的命令 date "+今天是:%F,%A,时间是:%T.准备吃饭去"。按回车,就可以重新执行这条命令了,而按左箭头键或右箭头键则可以直接修改,改完之后按回车执行,非常方便!

帮助

man history

 1        history [n]
 2        history -c
 3        history -d offset
 4        history -anrw [filename]
 5        history -p arg [arg ...]
 6        history -s arg [arg ...]
 7               With no options, display the command history list with line numbers.  Lines listed with a * have been modified.  An argument of n lists only the last n lines.  If  the
 8               shell  variable  HISTTIMEFORMAT  is  set  and  not null, it is used as a format string for strftime(3) to display the time stamp associated with each displayed history
 9               entry.  No intervening blank is printed between the formatted time stamp and the history line.  If filename is supplied, it is used as the name of the history file; if
10               not, the value of HISTFILE is used.  Options, if supplied, have the following meanings:
11               -c     Clear the history list by deleting all the entries.
12               -d offset
13                      Delete the history entry at position offset.
14               -a     Append the ``new'' history lines (history lines entered since the beginning of the current bash session) to the history file.
15               -n     Read  the  history lines not already read from the history file into the current history list.  These are lines appended to the history file since the beginning
16                      of the current bash session.
17               -r     Read the contents of the history file and use them as the current history.
18               -w     Write the current history to the history file, overwriting the history file's contents.
19               -p     Perform history substitution on the following args and display the result on the standard output.  Does not store the results in the  history  list.   Each  arg
20                      must be quoted to disable normal history expansion.
21               -s     Store the args in the history list as a single entry.  The last command in the history list is removed before the args are added.
22 
23               If  the  HISTTIMEFORMAT  variable is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment
24               character.  When the history file is read, lines beginning with the history comment character followed immediately by a digit are interpreted  as  timestamps  for  the
25               previous  history line.  The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is
26               supplied as an argument to -d, or the history expansion supplied as an argument to -p fails.

举例

history

显示历史命令列表,按执行的先后顺序从上到下显示。

本篇文章出自“国民时代”,转载请注明转载出处。
原文地址:https://www.cnblogs.com/ChinaGo/p/10609855.html