背景: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,.
我们在创建文件后,通常会对其进行修改或其他的读操作,这时候键入命令后利用上述快捷键即可快速补全所需命令。

Linux基础知识之history的详细说明的更多相关文章

  1. Linux基础知识梳理

    Linux基础知识梳理 Linux内核最初只是由芬兰人林纳斯?托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的.Linux是一套免费使用和自由传播的类Unix操作系统,是 ...

  2. Linux基础知识与基础命令

    Linux基础知识与基础命令 系统目录 Linux只有一个根目录,没有盘符的概念,文件目录是一个倒立的树形结构. 常用的目录功能 bin 与程序相关的文件 boot 与系统启动相关 cdrom 与Li ...

  3. [转帖]linux基础知识大纲

    linux基础知识大纲 https://blog.csdn.net/CSDN___LYY/article/details/80810403 1.Linux操作系统概述Linux操作系统的发展过程.创始 ...

  4. Linux基础知识入门

    [Linux基础]Linux基础知识入门及常见命令.   前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1: ...

  5. 运维之Linux基础知识(三)

    运维之Linux基础知识(三) 1. 查看文本 cat tac more less head tail 1.1 cat 连接并显示文件 cat -n:在显示的时候,将每一行编号 -E:显示结束符$ - ...

  6. Linux基础知识第九讲,linux中的解压缩,以及软件安装命令

    目录 Linux基础知识第九讲,linux中的解压缩,以及软件安装命令 一丶Linux Mac Windows下的压缩格式简介 2.压缩以及解压缩 3.linux中的软件安装以及卸载 1.apt进行安 ...

  7. Linux基础知识第八讲,系统相关操作命令

    目录 Linux基础知识第八讲,系统相关操作命令 一丶简介命令 2.磁盘信息查看. 3.系统进程 Linux基础知识第八讲,系统相关操作命令 一丶简介命令 时间和日期 date cal 磁盘和目录空间 ...

  8. Linux基础知识第七讲,用户权限以及用户操作命令

    目录 Linux基础知识第七讲,用户权限以及用户操作命令 一丶简介linux用户,用户权限,组的概念. 1.1 基本概念 1.2 组 1.3 ls命令查看权限. 二丶用户权限修改命令 1.chmod ...

  9. linux基础知识的总结

    例如以下内容是我对linux基础知识的总结,由于本人在初期学习linux的时候走了不少的弯路,对于基础的掌握耗费了不少的时间,所以为了后来者对linux的基础部分有个清晰的了解,特对基础知识进行了总结 ...

随机推荐

  1. Flume入门

    1.Flume是什么? ○ Flume是由cloudera开发的实时日志收集系统    ○ 核心概念是由一个叫做Agent(代理节点)的java进程运行在日志收集节点    ○ Flume在0.94. ...

  2. #ifndef用于避免多重包含

    原因:C中,某一个重要的头文件可能被多个文件包含,如果编译的多个文件均包含了同一个头文件, 就可能存在,一个头文件被多次包含的问题. 用法:这个问题,一般用#ifndef来解决. 1.定义一个与文件名 ...

  3. Nginx用户认证配置方法详解(域名/目录)

    Nginx超级强大它可以单独为一个域名设置用户认证,方法也很简单我们只要生成用户认证的用户名和密码,然后再Nginx添加auth认证配置即可 Nginx可以为某一个域名单独加用户认证,具体做法如下: ...

  4. Synplify9.6.2破解(转帖)

    Synplify9.6.2破解(转帖)   转载自:http://www.cnblogs.com/mark-sun/archive/2012/02/26/2368773.html Abstract本文 ...

  5. 【Android】8.4 让主题自适应不同的Android版本

    分类:C#.Android.VS2015: 创建日期:2016-02-17 一.简介 默认情况下,高版本提供的主题不能在低版本的Android系统上运行.但是,通过自定义主题,可以让你的系统自适应各自 ...

  6. Fragment里面的ListView的item点击没反应

    近期做一个消息列表,发现fragment的listview载入消息出来后,每条消息都点击不了.经过询问和查下已解决 开发中非经常见的一个问题,项目中的listview不不过简单的文字,经常须要自定义l ...

  7. nginx学习之epoll

    https://blog.csdn.net/mmshixing/article/details/51848673 首先说一下传统的I/O多路复用select和poll,对比一下和epoll之间的区别: ...

  8. AI落地企业业务的一些问题

    这两年大家都在喊未来已来,软件企业不管有没有算法工程师都竖起了AI的大旗,传统企业不管现在OA现状如何都想在数据转型.智能制造.机器智能方面尝尝鲜,感觉好像和前两年的互联网+风潮有点像,最近半年我在某 ...

  9. 07、在 Windows10 上获得屏幕分辨率

    因为在 win10 上,app 在运行的时候,默认不是占满全屏幕,并且 windows runtime 当前没有提供获取 屏幕尺寸的 api.在桌面 win32 api 中,可以获取屏幕尺寸,不过因为 ...

  10. 一款纯css3实现的翻转按钮

    之前为大家介绍了好多纯css3实现的很漂亮的按钮.今天小编要给各网友再分享一款纯css3实现的翻转按钮.实现中给出了两种的翻转特效,一种是基于按扭的左边缘为中心线,另一种是基于按钮的中间为中心线.我们 ...