history查询历史命令

简介:

history 命令用于查询已执行的历史命令。

常用参数:

n :数字,表示列出最近n行命令

-c :将目前的shell中的所有的history内容删除

-a:将目前新增的history命令新增入histfile中,若没有加hisfiles,则默认写入~/.bash_history

-r:将histfiles的内容读到目前这个shell 的history记忆中。

-w:将目前的history记忆内容写入histfiles中。

 

 

查看所有history记忆中的命令:

1
2
3
4
5
6
7
8
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # history
1   df -h
2 reboot
ssh  esggy-qa-n013
4 yum  install  nfs-utils
5 yum  install  parted
6 yum  install  nfs-utils

 

查看history的最后6条命令:

1
2
3
4
5
6
7
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # history  6
395  su  - trafodion
396 hive
397  history
398  history  n
399  history  3
400  history   6

 

将目前的已执行的命令添加到histfile中,默认为~/.bash_history并查看文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # history -w
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # cat ~/.bash_history | tail -n 10
clear
hive
su  - trafodion
hive
history
history  n
history  3
history  6
echo  $HISTORY
history  -w

将新增的history命令加到histfile中,并查看文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # history -a
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # cat ~/.bash_history | tail -n 10
history  n
history  3
history  6
echo  $HISTORY
history  -w
echo  $HISTORY
cat  ~/.bash_history
cat  ~/.bash_history |  tail  10
cat  ~/.bash_history |  tail  -n 10
history  -a

 

查看$HISTSIZE变量大小(~/.bash_history文件能记录的命令数量由$HISTSIZE决定):

1
2
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # echo $HISTSIZE
1000

 

使用!执行命令:

! 命令行号 (执行history中指定行号的命令)

! 字符串 (执行最近以指定字符串开头的命令)

!! (执行上一条命令) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # history 5
406  cat  ~/.bash_history |  tail  -n 10
407  history  -a
408  cat  ~/.bash_history |  tail  -n 10
409   echo  $HISTSIZE
410  history  5
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # !410
history  5
406  cat  ~/.bash_history |  tail  -n 10
407  history  -a
408  cat  ~/.bash_history |  tail  -n 10
409  echo  $HISTSIZE
410  history  5
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # !!
history  5
406  cat  ~/.bash_history |  tail  -n 10
407  history  -a
408  cat  ~/.bash_history |  tail  -n 10
409  echo  $HISTSIZE
410  history  5
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~] # !ec
echo  $HISTSIZE
1000



本文转自 天黑顺路 51CTO博客,原文链接:http://blog.51cto.com/mjal01/1963004,如需转载请自行联系原作者
相关文章
|
24天前
|
存储 安全 Linux
|
存储 Linux 应用服务中间件
history的操作你知道几个
history的操作你知道几个
113 0
history的操作你知道几个
Confluence 6.15 修改历史(Change-History)宏
修改历史(Change-History)宏显示了页面一个的更新历史:版本号,作者,日期和备注。这些内容将会在同一栏中进行显示。 屏幕截图:Confluence 中的修改历史(Change-History)宏 为了向页面中插入修改历史宏:  从编辑工具栏中,选择 插入(Insert)   > 其他宏(Other Macros)找到并且选择需要的宏。
775 0
|
Shell
history命令解析_学习笔记
时间:2017.11.13 作者:李强 参考:man,info,magedu讲义 声明:以下英文纯属个人翻译,英文B级,欢迎纠正,盗版不纠,才能有限,希望不误人子弟为好。 1、使用目的与场景     实现快速操作命令的一种方式 2、官方说明     Display or manipulate the history list. 3、写在前面     首先这里有两个概念history list和history file。
903 0