【转】History命令用法

https://linuxtoy.org/archives/history-command-usage-examples.html

使用 HISTTIMEFORMAT 显示时间戳

当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:

# export HISTTIMEFORMAT='%F %T '
# history | more
1  2008-08-05 19:02:39 service network restart
2  2008-08-05 19:02:39 exit
3  2008-08-05 19:02:39 id
4  2008-08-05 19:02:39 cat /etc/redhat-release

注意:这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显示成设置 HISTTIMEFORMAT 变量的时间。[感谢 NightOwl 读者补充]

使用 Ctrl+R 搜索历史

Ctrl+R 是我经常使用的一个快捷键。此快捷键让你对命令历史进行搜索,对于想要重复执行某个命令的时候非常有用。当找到命令后,通常再按回车键就可以执行该命令。如果想对找到的命令进行调整后再执行,则可以按一下左或右方向键。

# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`red‘: cat /etc/redhat-release
[Note: Press enter when you see your command, which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

快速重复执行上一条命令

有 4 种方法可以重复执行上一条命令:

  1. 使用上方向键,并回车执行。
  2. 按 !! 并回车执行。
  3. 输入 !-1 并回车执行。
  4. 按 Ctrl+P 并回车执行。

从命令历史中执行一个指定的命令

在下面的例子中,如果你想重复执行第 4 条命令,那么可以执行 !4:

# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)

通过指定关键字来执行以前的命令

在下面的例子,输入 !ps 并回车,将执行以 ps 打头的命令:

# !ps
ps aux | grep yp
root     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

使用 HISTSIZE 控制历史命令记录的总行数

将下面两行内容追加到 .bash_profile 文件并重新登录 bash shell,命令历史的记录数将变成 450 条:

# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

使用 HISTFILE 更改历史文件名称

默认情况下,命令历史存储在 ~/.bash_history 文件中。添加下列内容到 .bash_profile 文件并重新登录 bash shell,将使用 .commandline_warrior 来存储命令历史:

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

使用 HISTCONTROL 从命令历史中剔除连续重复的条目

在下面的例子中,pwd 命令被连续执行了三次。执行 history 后你会看到三条重复的条目。要剔除这些重复的条目,你可以将 HISTCONTROL 设置为 ignoredups:

# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47  history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58  history | tail -4

使用 HISTCONTROL 清除整个命令历史中的重复条目

上例中的 ignoredups 只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将 HISTCONTROL 设置成 erasedups:

# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3
# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

使用 HISTCONTROL 强制 history 不记住特定的命令

将 HISTCONTROL 设置为 ignorespace,并在不想被记住的命令前面输入一个空格:

# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
#  service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

使用 -c 选项清除所有的命令历史

如果你想清除所有的命令历史,可以执行:

# history -c

命令替换

在下面的例子里,!!:$ 将为当前的命令获得上一条命令的参数:

# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg

补充:使用 !$ 可以达到同样的效果,而且更简单。[感谢 wanzigunzi 读者补充]

下例中,!^ 从上一条命令获得第一项参数:

# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg

为特定的命令替换指定的参数

在下面的例子,!cp:2 从命令历史中搜索以 cp 开头的命令,并获取它的第二项参数:

# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt

下例里,!cp:$ 获取 cp 命令的最后一项参数:

# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

使用 HISTSIZE 禁用 history

如果你想禁用 history,可以将 HISTSIZE 设置为 0:

# export HISTSIZE=0
# history
# [Note that history did not display anything]

使用 HISTIGNORE 忽略历史中的特定命令

下面的例子,将忽略 pwd、ls、ls -ltr 等命令:

# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79  export HISTIGNORE=”pwd:ls:ls -ltr:”
80  service httpd stop
81  history
[Note that history did not record pwd, ls and ls -ltr]

Linux基础知识之history的详细说明

转自:Linux公社

背景:history是Linux中常会用到内容,在工作中一些用户会突然发现其安装不了某个软件,于是寻求运维人员的帮助,而不给你说明他到底做了哪些坑爹的操作。此时你第一件要做的就是要查看其history命令历史。查看后兴许你就会发现他到底干了什么坑爹的操作。
history可以让你在一定情况下快速定位问题所在。

本文的history介绍及其实践都是来自CentOS7.2环境
[root@localhost ~]# cat /etc/RedHat-release
CentOS Linux release 7.2.1511 (Core)

history的介绍
history是shell的内置命令,其内容在系统默认的shell的man手册中。
history是显示在终端输入并执行的过命令,系统默认保留1000条。
[root@localhost ~]# history
1  ls
2  vi /etc/sysconfig/network-scripts/ifcfg-eno16777728
3  service network restart
4  ifconfig
5  ping www.linuxidc.com
6  systemctl disable firewalld.service
7  systemctl stop firewalld.service
8  exit
9  ls
10  type which
11  which ls
12  file /usr/bin/ls
13  which clock
14  file /usr/sbin/clock
15  man which
16  man what
17  man who
18  who
19  man who
20  man w
21  man who
22  who -q
23  man w

..
.

系统在关闭后会将现有history内容保存在文件~/.bash_history

与history相关的环境变量
HISTFILE          指定存放历史文件位置,默认位置在~/.bash_profile(针对用户)、
/etc/profile(针对全局,如果~/.bash_profile内没有相关环境变量内容则使用全局变量设置)
HISTFILESIZE      命令历史文件记录历史的条数
HISTSIZE          命令历史记录的条数,默认为1000
HISTTIMEFORMAT=”%F %T”  显示命令发生的时间
HISTIGNORE=”str1:str2:…” 忽略string1,string2历史
HISTCONTROL      包含一下4项,让哪一项生效只需要让其=下面一项即可
ignoredups:  忽略重复的命令;连续且相同方为“重复”
ignorespace:  忽略所有以空白开头的命令
ignoreboth:ignoredups,ignorespace
erasedups:    删除重复命令

让上述环境变量生效方式:
1、直接在当前shell内输入相关变量,比如我们不想留存命令历史,我们把HISTSIZE设置为0
[root@localhost ~]# HISTSIZE=0
[root@localhost ~]# history

经测试,成功。不过这种设置的局限性是其作用范围仅仅针对当前shell及其子shell,如果切换用户或登出再登入其设置失效。不过其特点是设置完立刻生效。
下面通过实验说明这个问题
[root@localhost ~]# bash
[root@localhost ~]# history
[root@localhost ~]# history

以上结果说明在当前shell内设置history的环境变量后,其作用范围为当前shell及子shell
Last login: Fri Jul 29 17:26:41 2016 from 10.1.250.62
[root@localhost ~]# history
1  history

重新登陆后原有的history环境变量失效
2、另一种让history环境变量生效的方式是修改~/.bash_profile文件
[root@localhost ~]# vi ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
HISTTIMEFORMAT=”%F %T ”        此为新添加的history环境变量,我添加了发生命令操作的时间
export PATH

wq保存退出。
[root@localhost ~]# history
1  history
2  ll
3  cd
4  hostory
5  history
6  vi ~/.bash_profile
7  history

由结果可知变量并没有生效,我们重新登录再尝试下。
[root@localhost ~]# history
1  2016-07-29 20:00:29 history
2  2016-07-29 20:00:29 ll
3  2016-07-29 20:00:29 cd
4  2016-07-29 20:00:29 hostory
5  2016-07-29 20:00:29 history
6  2016-07-29 20:00:29 vi ~/.bash_profile
7  2016-07-29 20:00:29 history
8  2016-07-29 20:00:29 logout
9  2016-07-29 20:00:33 history

通过上面的两个结果,我们可以发现第二种修改配置文件虽然也可以成功修改history环境变量但是其生效需要重新登陆,并不是改完就生效。但是它的特点是此修改始终有效,时效性为永久。

history命令的使用
-c: 清空命令历史
-d n: 删除历史中指定的命令,n表示命令号
#: 显示最近的#条历史
-a: 追加本次会话新执行的命令历史列表至历史文件,因为多终端所以如果想看当前都发生了什么操作就可以执行-a进行查看
-n: 读历史文件(本地数据)中未读过的行到历史列表(内存数据)
-r: 读历史文件(本地数据)附加到历史列表(内存数据)
-w: 保存历史列表(内存数据)到指定的历史文件(本地数据)
-s: 展开历史参数成一行,附加在历史列表后。用于伪造命令历史
下面来演示上面几种命令的使用
[root@localhost ~]# history
1  2016-07-29 20:00:29 history
2  2016-07-29 20:00:29 ll
3  2016-07-29 20:00:29 cd
4  2016-07-29 20:00:29 hostory
5  2016-07-29 20:00:29 history
6  2016-07-29 20:00:29 vi ~/.bash_profile
7  2016-07-29 20:00:29 history
8  2016-07-29 20:00:29 logout
9  2016-07-29 20:00:33 history
10  2016-07-29 20:07:41  cd
11  2016-07-29 20:07:44  ls
12  2016-07-29 20:07:50  history
13  2016-07-29 20:08:12 cat /etc/profile
14  2016-07-29 20:12:10 HISTCONTROL=ignorespace
15  2016-07-29 20:27:28 history -p
16  2016-07-29 20:27:31 history
17  2016-07-29 20:28:10 ls /etc
18  2016-07-29 20:28:14 history
19  2016-07-29 20:28:57 history

清空历史
[root@localhost ~]# history -c
[root@localhost ~]# history
2016-07-29 20:29:26 history

从历史文件读取之前的命令历史
[root@localhost ~]# history -r
[root@localhost ~]# history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 ll
5  2016-07-29 20:30:59 cd
6  2016-07-29 20:30:59 hostory
7  2016-07-29 20:30:59 history
8  2016-07-29 20:30:59 vi ~/.bash_profile
9  2016-07-29 20:30:59 history
10  2016-07-29 20:30:59 logout
11  2016-07-29 20:31:01 history

删除指定命令历史
[root@localhost ~]# history -d 4
[root@localhost ~]# history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 cd
5  2016-07-29 20:30:59 hostory
6  2016-07-29 20:30:59 history
7  2016-07-29 20:30:59 vi ~/.bash_profile
8  2016-07-29 20:30:59 history
9  2016-07-29 20:30:59 logout
10  2016-07-29 20:31:01 history
11  2016-07-29 20:32:50 history -d 4
12  2016-07-29 20:32:52 history

伪造历史命令
12345678910111213141516 [root@localhost ~]# history -s rm -rf /*  做下恶作剧
[root@localhost ~]# history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 cd
5  2016-07-29 20:30:59 hostory
6  2016-07-29 20:30:59 history
7  2016-07-29 20:30:59 vi ~/.bash_profile
8  2016-07-29 20:30:59 history
9  2016-07-29 20:30:59 logout
10  2016-07-29 20:31:01 history
11  2016-07-29 20:32:50 history -d 4
12  2016-07-29 20:32:52 history
13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
14  2016-07-29 20:34:00 history

我相信任谁输入history看到这个命令都会吓一身汗。
调用历史参数详解

#cmd !^ : 利用上一个命令的第一个参数做cmd的参数
#cmd !$ : 利用上一个命令的最后一个参数做cmd的参数
#cmd !* : 利用上一个命令的全部参数做cmd的参数
#cmd !:n : 利用上一个命令的第n个参数做cmd的参数
#!n :调用第n条命令
#!-n:调用倒数第n条命令
#!!:执行上一条命令
#!$:引用前一个命令的最后一个参数同组合键Esc,.
#!n:^ 调用第n条命令的第一个参数
#!n:$ 调用第n条命令的最后一个参数
#!m:n 调用第m条命令的第n个参数
#!n:* 调用第n条命令的所有参数
#!string:执行命令历史中最近一个以指定string开头的命令
#!string:^ 从命令历史中搜索以string 开头的命令,并获取它的第一个参数
#!string:$ 从命令历史中搜索以string 开头的命令,并获取它的最后一个参数
#!string:n 从命令历史中搜索以string 开头的命令,并获取它的第n个参数
#!string:* 从命令历史中搜索以string 开头的命令,并获取它的所有参数

下面通过实验来实践上面的历史参数的具体用法
[root@localhost ~]# history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 cd
5  2016-07-29 20:30:59 hostory
6  2016-07-29 20:30:59 history
7  2016-07-29 20:30:59 vi ~/.bash_profile
8  2016-07-29 20:30:59 history
9  2016-07-29 20:30:59 logout
10  2016-07-29 20:31:01 history
11  2016-07-29 20:32:50 history -d 4
12  2016-07-29 20:32:52 history
13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
14  2016-07-29 20:34:00 history
15  2016-07-29 20:40:32 ls
16  2016-07-29 20:40:33 cd
17  2016-07-29 20:40:45 ls /etc/passwd
18  2016-07-29 20:41:35 history

我们先使用!!来调用上一条命令

[root@localhost ~]# !!
history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 cd
5  2016-07-29 20:30:59 hostory
6  2016-07-29 20:30:59 history
7  2016-07-29 20:30:59 vi ~/.bash_profile
8  2016-07-29 20:30:59 history
9  2016-07-29 20:30:59 logout
10  2016-07-29 20:31:01 history
11  2016-07-29 20:32:50 history -d 4
12  2016-07-29 20:32:52 history
13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
14  2016-07-29 20:34:00 history
15  2016-07-29 20:40:32 ls
16  2016-07-29 20:40:33 cd
17  2016-07-29 20:40:45 ls /etc/passwd
18  2016-07-29 20:41:35 history

[root@localhost ~]# !h
history
1  2016-07-29 20:29:26 history
2  2016-07-29 20:30:59 history -r
3  2016-07-29 20:30:59 history
4  2016-07-29 20:30:59 cd
5  2016-07-29 20:30:59 hostory
6  2016-07-29 20:30:59 history
7  2016-07-29 20:30:59 vi ~/.bash_profile
8  2016-07-29 20:30:59 history
9  2016-07-29 20:30:59 logout
10  2016-07-29 20:31:01 history
11  2016-07-29 20:32:50 history -d 4
12  2016-07-29 20:32:52 history
13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
14  2016-07-29 20:34:00 history
15  2016-07-29 20:40:32 ls
16  2016-07-29 20:40:33 cd
17  2016-07-29 20:40:45 ls /etc/passwd
18  2016-07-29 20:41:35 history
19  2016-07-29 20:47:03 history
20  2016-07-29 20:48:22 history

大家感兴趣可以一个个实验。本文就介绍到这里。
常用的快捷键
重新调用前一个命令中最后一个参数:
!$
Esc, .(点击Esc键后松开,然后点击. 键)
这两个很常用,特别是Esc,.
我们在创建文件后,通常会对其进行修改或其他的读操作,这时候键入命令后利用上述快捷键即可快速补全所需命令。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注