Linux命令:history

Posted chinago

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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文件中保存的只有上一次正确注销后的命令。

 配置文件/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只能输出设置生效之后的

调用历史命令

Ctrl+R  直接键入想要查询的命令,进行动态模糊查询:直接回车可以执行 

使用上下箭头调用以前的历史命令
使用 !n 重复执行第n条历史命令
使用 !! , !-1 重复执行上一条命令

Ctrl+p ,调取上一条命令。
使用 !str 重复执行最后一条以str开头的命令

帮助

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 files 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

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

以上是关于Linux命令:history的主要内容,如果未能解决你的问题,请参考以下文章

修改linux系统history命令的条数和格式

Linux命令:history

Linux命令学习-history命令

Linux系统history命令小技巧

Linux基础:history命令

linux命令历史