linux history命令


历史命令:history
那么什么又是历史命令? 曾经做过 的举动我们可以将他记录下来喔!那就是历史命令啰~底下分别来谈一谈这两个玩意儿。 前面我们提过 bash 有提供命令历史的服务!那么如何查询我们曾经下达过的命令呢?就使用 history 啰!当然,如果觉得 histsory 要输入的字符太多太麻烦,可以使用命令别名来设定呢! 不要 跟我说还不会设定呦!
[peter@study ~]$ alias h='history'
如此则输入 h 等于输入 history 啰!好了,我们来谈一谈 history 的用法吧!
[peter@study ~]$ history [n]
[peter@study ~]$ history [-c]
[peter@study ~]$ history [-raw] histfiles
选项与参数:
n :数字,意思是要列出最近的 n 笔命令行表的意思!
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 命令新增入 histfiles 中,若没有加 histfiles , 则默认写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中;
-w :将目前的 history 记忆内容写入 histfiles 中!
范例一:列出目前内存内的所有 history 记忆
[peter@study ~]$ history
# 前面省略
1017 man bash
1018 ll
1019 history
1020 history

# 列出的信息当中,共分两栏,第一栏为该命令在这个 shell 当中的代码, # 另一个则是命令本身的内容喔!至于会秀出几笔命令记录,则与 HISTSIZE 有关!
范例二:列出目前最近的 3 笔资料
[peter@study ~]$ history 3
1019 history
1020 history
1021 history 3
范例三:立刻将目前的资料写入 histfile 当中
[peter@study ~]$ history -w
# 在默认的情况下,会将历史纪录写入 ~/.bash_history 当中!
[peter@study ~]$ echo ${HISTSIZE}
1000
在正常的情况下,历史命令的读取与记录是这样的:
当我们以 bash 登入 Linux 主机之后,系统会主动的由家目录的 ~/.bash_history 读取以前曾经下过的命令, 那么 ~/.bash_history 会记录几笔数据呢?这就与你 bash 的 HISTFILESIZE 这个变量设定值有关了!
假设我这次登入主机后,共下达过 100 次命令, 等我注销时, 系统就会将 101~1100 这总共 1000 笔历 史命令更新到 ~/.bash_history 当中。
也就是说,历史命令在我注销时,会将最近的 HISTFILESIZE 笔记 录到我的纪录文件当中啦!
当然,也可以用 history -w 强制立刻写入的!那为何用更新两个字呢? 因为 ~/.bash_history 记录的笔 数永远都是 HISTFILESIZE 那么多,旧的讯息会被主动的拿掉! 仅保留最新的!
那么 history 这个历史命令只可以让我查询命令而已吗?呵呵!当然不止啊! 我们可以利用相关的
功能来帮我们执行命令呢!举例来说啰:
[peter@study ~]$ !number
[peter@study ~]$ !command
[peter@study ~]$ !!
选项与参数:
number :执行第几笔命令的意思;
command :由最近的命令向前搜寻命令串开头为 command的那个命令,并执行;
:就是执行上一个命令(相当于按↑按键后,按 Enter) !!
[peter@study ~]$ history
66 man rm
67 alias
68 man history
69 history
[peter@study ~]$ !66 #执行第 66 笔命令
[peter@study ~]$ !! #执行上一个命令,本例中亦即 !66
[peter@study ~]$ !al #执行最近以 al 为开头的命令(上头列出的第 67 个)
经过上面的介绍,瞭乎?历史命令用法可多了!如果我想要执行上一个命令, 除了使用上下键之外, 我可以直接以 !! 来下达上个命令的内容,此外, 我也可以直接选择下达第 n 个命令, !n 来执行,也可以使用命令标头,例如 !vi 来执行最近命令开头是 vi 的命令列!相当的方便而 好用!
基本上 history 的用途很大的!但是需要小心安全的问题!尤其是 root 的历史纪录文件,这是 Cracker 的最爱!因为不小心的 root 会将很多的重要数据在执行的过程中会被纪录在 ~/.bash_history 当中, 如果这个文件被解析的话,后果不堪吶!无论如何,使用 history 配合 ! 曾经使用过的命令下 达是很有效率的一个命令下达方法!
同一账号同时多次登入的 history 写入问题
有些朋友在练习 linux 的时候喜欢同时开好几个 bash 接口,这些 bash 的身份都是 root 。 这样会 有 ~/.bash_history 的写入问题吗?想一想,因为这些 bash 在同时以 root 的身份登入, 因此所有 的 bash 都有自己的 1000 笔记录在内存中。因为等到注销时才会更新记录文件,所以啰, 最后注 销的那个 bash 才会是最后写入的数据。唔!如此一来其他 bash 的命令操作就不会被记录下来了 (其 实有被记录,只是被后来的最后一个 bash 所覆盖更新了) 。
由于多重登入有这样的问题,所以很多朋友都习惯单一 bash 登入,再用工作控制 (job control, 第四 篇会介绍) 来切换不同工作! 这样才能够将所有曾经下达过的命令记录下来,也才方便未来系统管理 员进行命令的 debug 啊!
无法记录时间
历史命令还有一个问题,那就是无法记录命令下达的时间。由于这 1000 笔历史命令是依序记录的, 但是并没有记录时间,所以在查询方面会有一些不方便。如果读者们有兴趣,其实可以透过 ~/.bash_logout 来进行 history 的记录,并加上 date 来增加时间参数,也是一个可以应用的方向喔!
有兴趣的朋友可以先看看情境模拟题一吧!Tips
经常需要设计在线题目给学生考试用,所以需要登入系统去设计环境,设计完毕 后再将该硬盘分派给学生来考试使用。 只是,经常很担心同学不小心输入 history 就会得知要考试的重点文件 与命令,因此就得要使用 history -c; history -w 来强迫更新纪录文件了! 提供给您参考!

history命令可以列出以前运行过的命令列表

history

history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Display or manipulate the history list.
Display the history list with line numbers, prefixing each modified
entry with a `*'. An argument of N lists only the last N entries.
Options:
-c clear the history list by deleting all of the entries
-d offset delete the history entry at position OFFSET.
-a append history lines from this session to the history file
-n read all history lines not already read from the history file and append them to the history list
-r read the history file and append the contents to the history list
-w write the current history to the history file
-p perform history expansion on each ARG and display the result without storing it in the history list
-s append the ARGs to the history list as a single entry
If FILENAME is given, it is used as the history file. Otherwise,
if HISTFILE has a value, that is used, else ~/.bash_history.
If the HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry. No time stamps are printed otherwise.
Exit Status:
Returns success unless an invalid option is given or an error occurs.

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读