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键后松开,然后点击. 键)

提高速度 history 的利用的更多相关文章

  1. VBA提高速度的技巧

    此贴原转自EH论坛,我自己有所修改 [编者按]速度是程序设计永恒的热门话题,虽然速度技巧在各种语言之间可以相互借鉴,但差别有时也会很大,比如VC中由于字符串的存储方式决定了判断空串使用len函数更快, ...

  2. Intellij Idea配置提高速度

    主要介绍一下Intellij Idea的关于速度和最大最大方法数目 提高速度 1.命令: 2.找到./Library/Preferences/IntelliJIdea2016.1/idea.vmopt ...

  3. SQL Server通过整理索引碎片和重建索引提高速度

    本文章转载:http://database.51cto.com/art/201108/282408.htm SQL Server数据库中,当索引碎片太多时,就会拖慢数据库查询的速度.这时我们可以通过整 ...

  4. 嵌入式学习笔记(综合提高篇 第一章) -- 利用串口点亮/关闭LED灯

    1      前言 从踏入嵌入式行业到现在已经过去了4年多,参与开发过的产品不少,有交换机.光端机以及光纤收发器,停车场出入缴费系统,二维码扫码枪,智能指纹锁以及数字IC芯片开发等; 涉及产品中中既有 ...

  5. 优化Windows电脑常见方法,提高速度,释放硬盘C盘

    开始,我们首先让电脑变得易于使用一,提高开机速度常见的使电脑变卡的原因是:一台电脑同时安装了多个杀毒软件.一台电脑安装多个杀毒软件不仅占用你电脑大量内存.有时甚至会产生冲突,这会导致电脑运行非常缓慢, ...

  6. msysgit之git bash方便的复制粘贴,默认工作目录,窗口大小,提高速度等小窍门

    桌面图标点鼠标右键,选择属性,修改工作目录到自己的git目录: 打开快速编辑,这时候鼠标左键可以随意选取文本,enter键复制.鼠标右键是粘贴.另外,粘贴的快捷键是 inerst键 修改默认窗口大小 ...

  7. 在Android中使用并发来提高速度和性能

    Android框架提供了很实用的异步处理类.然而它们中的大多数在一个单一的后台线程中排队.当你需要多个线程时你是怎么做的? 众所周知,UI更新发生在UI线程(也称为主线程).在主线程中的任何操作都会阻 ...

  8. python小工具:用python操作HP的Quality Center (二)----- 用异步方式提高速度

    上接第一篇 http://www.cnblogs.com/sdet/p/6874631.html 在python中,很简单地能把http请求通过异步的方式发送,以下代码在python 3.6.0上运行 ...

  9. Android studio通过连接蓝叠模拟器大幅提高速度!

    因为我的电脑是X200,集成显卡,不支持Opengl 2.0,很多安卓模拟器都不能用(夜神.海马玩,mumu等)最后发现蓝叠. 首先WIN+R,CMD,输入ADB,如果没有命令信息,说明我们需要在PA ...

随机推荐

  1. 01 Django基础知识

    相关概念 软件框架 一个公司是由公司中的各部部门来组成的,每一个部门拥有特定的职能,部门与部门之间通过相互的配合来完成让公司运转起来. 一个软件框架是由其中各个软件模块组成的,每一个模块都有特定的功能 ...

  2. Primer C++第五版 读书笔记(一)

    Primer C++第五版 读书笔记(一) (如有侵权请通知本人,将第一时间删文) 1.1-2.2 章节 关于C++变量初始化: 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义 ...

  3. arm页表在linux中的融合

    参考:arm-linux内存页表创建 arm的第一级页表条目数为4096个,对于4K页第二级目录条目个数为256个,一级二级条目都是每个条目4字节. 在linux下二级分页如下:虚拟地址——> ...

  4. 解决获取View的width和Height为0的4种方法

    很经常当我们动态创建某些View时,需要通过获取他们的width和height来确定别的view的布局,但是在onCreate()获取view的width和height会得到0.view.getWid ...

  5. 20,序列化模块 json,pickle,shelve

    序列化模块 什么叫序列化? 将原本的字典,列表等内容转换成一个字符串的过程叫做序列化. 序列化的目的? 数据结构 通过序列化 转成 str. str 通过反序列化 转化成数据结构. json: jso ...

  6. python 学习分享-字典篇

    python字典(Dictionary) dict是无序的 key必须是唯一切不可变的 a={'key1':'value1','key2':'value2'} 字典的增删改查 a['key3']='v ...

  7. 让读者快速了解RocketMQ消息中间件需要解决哪些问题

    本文首先引出消息中间件通常需要解决哪些问题,在解决这些问题当中会遇到什么困难,Apache RocketMQ作为阿里开源的一款高性能.高吞吐量的分布式消息中间件否可以解决,规范中如何定义这些问题.然后 ...

  8. python 计算日期间隔

    from datetime import date a = date(2011,11,24) b = date(2011,11,17) print(a-b)

  9. 【bzoj4016】[FJOI2014]最短路径树问题 堆优化Dijkstra+DFS树+树的点分治

    题目描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最小的那条路径( ...

  10. Installing Metasploit Framework on Ubuntu 14.04 LTS and Debian 7

    原文链接:http://www.darkoperator.com/installing-metasploit-in-ubunt/ This Guide covers the installation ...