history linux 位置,Linux基础知识之history的详细说明
背景:history是Linux中常會(huì)用到內(nèi)容,在工作中一些用戶會(huì)突然發(fā)現(xiàn)其安裝不了某個(gè)軟件,于是尋求運(yùn)維人員的幫助,而不給你說(shuō)明他到底做了哪些坑爹的操作。此時(shí)你第一件要做的就是要查看其history命令歷史。查看后興許你就會(huì)發(fā)現(xiàn)他到底干了什么坑爹的操作。
history可以讓你在一定情況下快速定位問(wèn)題所在。
本文的history介紹及其實(shí)踐都是來(lái)自CentOS7.2環(huán)境
[root@localhost ~]# cat /etc/RedHat-release
CentOS Linux release 7.2.1511 (Core)
history的介紹
history是shell的內(nèi)置命令,其內(nèi)容在系統(tǒng)默認(rèn)的shell的man手冊(cè)中。
history是顯示在終端輸入并執(zhí)行的過(guò)命令,系統(tǒng)默認(rèn)保留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
...
..
.
系統(tǒng)在關(guān)閉后會(huì)將現(xiàn)有history內(nèi)容保存在文件~/.bash_history
與history相關(guān)的環(huán)境變量
HISTFILE? ? ? ? ? 指定存放歷史文件位置,默認(rèn)位置在~/.bash_profile(針對(duì)用戶)、
/etc/profile(針對(duì)全局,如果~/.bash_profile內(nèi)沒(méi)有相關(guān)環(huán)境變量?jī)?nèi)容則使用全局變量設(shè)置)
HISTFILESIZE? ? ? 命令歷史文件記錄歷史的條數(shù)
HISTSIZE? ? ? ? ? 命令歷史記錄的條數(shù),默認(rèn)為1000
HISTTIMEFORMAT="%F %T"? 顯示命令發(fā)生的時(shí)間
HISTIGNORE="str1:str2:..." 忽略string1,string2歷史
HISTCONTROL? ? ? 包含一下4項(xiàng),讓哪一項(xiàng)生效只需要讓其=下面一項(xiàng)即可
ignoredups:? 忽略重復(fù)的命令;連續(xù)且相同方為“重復(fù)”
ignorespace:? 忽略所有以空白開(kāi)頭的命令
ignoreboth:ignoredups,ignorespace
erasedups:? ? 刪除重復(fù)命令
讓上述環(huán)境變量生效方式:
1、直接在當(dāng)前shell內(nèi)輸入相關(guān)變量,比如我們不想留存命令歷史,我們把HISTSIZE設(shè)置為0
[root@localhost ~]# HISTSIZE=0
[root@localhost ~]# history
經(jīng)測(cè)試,成功。不過(guò)這種設(shè)置的局限性是其作用范圍僅僅針對(duì)當(dāng)前shell及其子shell,如果切換用戶或登出再登入其設(shè)置失效。不過(guò)其特點(diǎn)是設(shè)置完立刻生效。
下面通過(guò)實(shí)驗(yàn)說(shuō)明這個(gè)問(wèn)題
[root@localhost ~]# bash
[root@localhost ~]# history
[root@localhost ~]# history
以上結(jié)果說(shuō)明在當(dāng)前shell內(nèi)設(shè)置history的環(huán)境變量后,其作用范圍為當(dāng)前shell及子shell
Last login: Fri Jul 29 17:26:41 2016 from 10.1.250.62
[root@localhost ~]# history
1? history
重新登陸后原有的history環(huán)境變量失效
2、另一種讓history環(huán)境變量生效的方式是修改~/.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環(huán)境變量,我添加了發(fā)生命令操作的時(shí)間
export PATH
wq保存退出。
[root@localhost ~]# history
1? history
2? ll
3? cd
4? hostory
5? history
6? vi ~/.bash_profile
7? history
由結(jié)果可知變量并沒(méi)有生效,我們重新登錄再嘗試下。
[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
通過(guò)上面的兩個(gè)結(jié)果,我們可以發(fā)現(xiàn)第二種修改配置文件雖然也可以成功修改history環(huán)境變量但是其生效需要重新登陸,并不是改完就生效。但是它的特點(diǎn)是此修改始終有效,時(shí)效性為永久。
history命令的使用
-c: 清空命令歷史
-d n: 刪除歷史中指定的命令,n表示命令號(hào)
#: 顯示最近的#條歷史
-a: 追加本次會(huì)話新執(zhí)行的命令歷史列表至歷史文件,因?yàn)槎嘟K端所以如果想看當(dāng)前都發(fā)生了什么操作就可以執(zhí)行-a進(jìn)行查看
-n: 讀歷史文件(本地?cái)?shù)據(jù))中未讀過(guò)的行到歷史列表(內(nèi)存數(shù)據(jù))
-r: 讀歷史文件(本地?cái)?shù)據(jù))附加到歷史列表(內(nèi)存數(shù)據(jù))
-w: 保存歷史列表(內(nèi)存數(shù)據(jù))到指定的歷史文件(本地?cái)?shù)據(jù))
-s: 展開(kāi)歷史參數(shù)成一行,附加在歷史列表后。用于偽造命令歷史
下面來(lái)演示上面幾種命令的使用
[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
我相信任誰(shuí)輸入history看到這個(gè)命令都會(huì)嚇一身汗。
調(diào)用歷史參數(shù)詳解
#cmd !^ : 利用上一個(gè)命令的第一個(gè)參數(shù)做cmd的參數(shù)
#cmd !$ : 利用上一個(gè)命令的最后一個(gè)參數(shù)做cmd的參數(shù)
#cmd !* : 利用上一個(gè)命令的全部參數(shù)做cmd的參數(shù)
#cmd !:n : 利用上一個(gè)命令的第n個(gè)參數(shù)做cmd的參數(shù)
#!n :調(diào)用第n條命令
#!-n:調(diào)用倒數(shù)第n條命令
#!!:執(zhí)行上一條命令
#!$:引用前一個(gè)命令的最后一個(gè)參數(shù)同組合鍵Esc,.
#!n:^ 調(diào)用第n條命令的第一個(gè)參數(shù)
#!n:$ 調(diào)用第n條命令的最后一個(gè)參數(shù)
#!m:n 調(diào)用第m條命令的第n個(gè)參數(shù)
#!n:* 調(diào)用第n條命令的所有參數(shù)
#!string:執(zhí)行命令歷史中最近一個(gè)以指定string開(kāi)頭的命令
#!string:^ 從命令歷史中搜索以string 開(kāi)頭的命令,并獲取它的第一個(gè)參數(shù)
#!string:$ 從命令歷史中搜索以string 開(kāi)頭的命令,并獲取它的最后一個(gè)參數(shù)
#!string:n 從命令歷史中搜索以string 開(kāi)頭的命令,并獲取它的第n個(gè)參數(shù)
#!string:* 從命令歷史中搜索以string 開(kāi)頭的命令,并獲取它的所有參數(shù)
下面通過(guò)實(shí)驗(yàn)來(lái)實(shí)踐上面的歷史參數(shù)的具體用法
[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
我們先使用!!來(lái)調(diào)用上一條命令
[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
大家感興趣可以一個(gè)個(gè)實(shí)驗(yàn)。本文就介紹到這里。
常用的快捷鍵
重新調(diào)用前一個(gè)命令中最后一個(gè)參數(shù):
!$
Esc, .(點(diǎn)擊Esc鍵后松開(kāi),然后點(diǎn)擊. 鍵)
這兩個(gè)很常用,特別是Esc,.
我們?cè)趧?chuàng)建文件后,通常會(huì)對(duì)其進(jìn)行修改或其他的讀操作,這時(shí)候鍵入命令后利用上述快捷鍵即可快速補(bǔ)全所需命令。
總結(jié)
以上是生活随笔為你收集整理的history linux 位置,Linux基础知识之history的详细说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一道阿姆斯特朗回旋好题( Convex
- 下一篇: NATAPP vscode SSH远程连