zoukankan      html  css  js  c++  java
  • linux history

    http://blog.sina.com.cn/s/blog_51b6521b0100io54.html

    root@linux ~]# history [n]
    [root@linux ~]# history [-c]
    [root@linux ~]# history [-raw] histfiles

    参数:
    n   :数字,意思是‘要列出最近的 n 笔命令列表’的意思!
    -c  :将目前的 shell 中的所有 history 内容全部消除
    -a  :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,
         则预设写入 ~/.bash_history
    -r  :将 histfiles 的内容读到目前这个 shell 的 history 记忆中;
    -w  :将目前的 history 记忆内容写入 histfiles 中!
    范例:
    范例一:列出目前记忆体内的所有 history 记忆
    [root@linux ~]# history
    # 前面省略
     1017  man bash
     1018  ll
     1019  history
     1020  history
    # 列出的资讯当中,共分两栏,第一栏为该指令在这个 shell 当中的代码,
    # 另一个则是指令本身的内容喔!至于会秀出几笔指令记录,则与 HISTSIZE 有关!

    范例二:列出目前最近的 3 笔资料
    [root@linux ~]# history 3
     1019  history
     1020  history
     1021  history 3

    范例三:立刻将目前的资料写入 histfile 当中
    [root@linux ~]# history -w
    # 在预设的情况下,会将历史纪录写入 ~/.bash_history 当中!
    [root@linux ~]# echo $HISTSIZE
    1000

    在正常的情况下,当我们以 bash 登入 Linux 主机之后,系统会主动的由家目录的 ~/.bash_history 读取以前曾经下过的指令,那么 ~/.bash_history 会记录几笔资料呢?这就与你 bash 的 HISTSIZE 这个变数设定值有关了!在预设的FC4 底下,是会记录 1000 笔资料的! 那么假设我这次登入主机后,共下达过 100 次指令,‘等我登出时, 系统就会将 101~1100 这总共 1000 笔历史命令更新到 ~/.bash_history 当中。’ 也就是说,历史命令在我登出时,会将最近的HISTSIZE 笔记录到我的纪录档当中啦!当然,也可以用 history -w 强制立刻写入的!那为何用‘更新’两个字呢? 因为 ~/.bash_history 记录的笔数永远都是 HISTSIZE 那么多,旧的讯息会被主动的拿掉!仅保留最新的!

    那么 history这个历史命令只可以让我查询命令而已吗?呵呵!当然不止啊! 我们可以利用相关的功能来帮我们执行命令呢!举例来说啰:

    [root@linux ~]# !number
    [root@linux ~]# !command
    [root@linux ~]# !!
    参数:
    number  :执行第几笔指令的意思;
    command :由最近的指令向前搜寻‘指令串开头为 command’的那个指令,并执行;
    !!     :就是执行上一个指令(相当于按↑按键后,按 Enter)
    范例:
    [root@linux ~]# history
       66  manrm
       67 alias
       68  manhistory
       69 history
    [root@linux ~]# !66  <==执行第66 笔指令
    [root@linux ~]# !!  <==执行上一个指令,本例中亦即 !66
    [root@linux ~]# !al <==执行最近以 al 为开头的指令(上头列出的第 67 个)

    经过上面的介绍,瞭乎?历史命令用法可多了!如果我想要执行上一个指令,除了使用上下键之外,我可以直接以‘ !! ’来下达上个指令的内容,此外, 我也可以直接选择下达第 n 个指令,‘ !n’来执行,也可以使用指令标头,例如 ‘ !vi’来执行最近指令开头是 vi的指令列!相当的方便而好用!基本上 history 的用途很大的!但是需要小心安全的问题!尤其是 root 的历史纪录档案!因为不小心的 root 会将很多的重要资料在执行的过程中会被纪录在~/.bash_history当中,如果这个档案被解析的话,后果不堪呐!无论如何,使用 history 配合‘ !’曾经使用过的指令下达是很有效率的一个指令方法!!

  • 相关阅读:
    leetcode 309. Best Time to Buy and Sell Stock with Cooldown
    leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee
    leetcode 32. Longest Valid Parentheses
    leetcode 224. Basic Calculator
    leetcode 540. Single Element in a Sorted Array
    leetcode 109. Convert Sorted List to Binary Search Tree
    leetcode 3. Longest Substring Without Repeating Characters
    leetcode 84. Largest Rectangle in Histogram
    leetcode 338. Counting Bits
    git教程之回到过去,版本对比
  • 原文地址:https://www.cnblogs.com/wang985850293/p/5191962.html
Copyright © 2011-2022 走看看