Linux基礎(chǔ)命令-alias設(shè)置別名_Linux學(xué)習(xí)中的博客-CSDN博客
文章目錄
前言
一、history命令介紹
二、語法及參數(shù)
?三、參考實例
總結(jié)
前言
? ? ? ? 在工作中,經(jīng)常會需要多次重復(fù)某一個命令,或者需要查看之前執(zhí)行過什么命令,那么都少不了history這個命令,下面一起來看看吧。
一、history命令介紹
? ? ? ? history的英文單詞來源于它自己,主要功能是用于顯示與管理歷史記錄。linux系統(tǒng)默認會記錄用戶所執(zhí)行過的所有命令,可以使用history命令查閱它們,也可以對其命令進行修改和刪除操作。
二、語法及參數(shù)
1)用help或man查看history命令
語法:history 【參數(shù)】/ history 【參數(shù)】文件
[root@localhost ~]# help history
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 參數(shù) [參數(shù)...]
選項:
-c 刪除所有條目從而清空歷史列表。
-d 偏移量 從指定位置刪除歷史列表。
-a 將當前繪畫的歷史行追加到歷史文件中
-n 從歷史文件中讀取所有未被讀取的行
-r 讀取歷史文件并將內(nèi)容追加到歷史列表中
中
-w 將當前歷史寫入到歷史文件中
并追加到歷史列表中
-p 對每一個 ARG 參數(shù)展開歷史并顯示結(jié)果
而不存儲到歷史列表中
-s 以單條記錄追加 ARG 到歷史列表中
2)命令的常用參數(shù):
-a | 寫入命令記錄 |
-c | 清空命令記錄 |
-d | 刪除指定序號的命令記錄 |
-n | 讀取命令記錄 |
-r | 讀取命令記錄到緩沖區(qū) |
-s | 將指定的命令添加到緩沖區(qū) |
-w | 將緩沖區(qū)信息寫入到歷史文件 |
?三、參考實例
1)顯示歷史記錄,且再次執(zhí)行其中的一個命令;
[root@localhost ~]# history
1 rpm -ivh httpd
2 rpm -ivh http
3 which rpm
4 rpm -qa | wc -l
5 which rz
6 rpm -qf /usr/bin/rz
7 rpm -e lrzsz --nodeps
8 which rz
9 rpm -p
10 rpm repolist
[root@localhost ~]# !3 #!數(shù)字
which rpm
/usr/bin/rpm
[root@localhost ~]# vim /etc/bashrc #ctrl+r 快捷鍵從下往上搜索,右方向鍵確定
(reverse-i-search)`vim': vim /etc/bashrc
[root@localhost ~]# !cd #!字符串
cd ~
?有三種方式可以再次執(zhí)行歷史記錄的命令:
- 第一個方式是:!數(shù)字
- 第二個方式是: ctrl+r 快捷鍵從小往上搜索,右方向鍵確定
- 第三個方式是: !字符串
2)打印3行歷史記錄內(nèi)容;
[root@localhost ~]# history 3
46 history --help
47 history
48 history 3
3)將本次緩沖區(qū)信息寫入到歷史文件中; 歷史記錄是有一個本地用戶文件的(~/.bash_history)
將文件給刪除了,文件內(nèi)的內(nèi)容也一并刪除了,不過用戶重新登入會自動生成一個相同文件出來。
[root@localhost ~]# history -w
[root@localhost ~]# tail -n 3 .bash_history
history
history 3
history -w
?4)將歷史文件中的信息讀入到當前緩沖區(qū)中;
[root@localhost ~]# history -r
[root@localhost ~]# history
1 yum grouplist
2 df -h
3 yum grouplist
5)將本次緩沖區(qū)信息追加寫入到歷史記錄中;
[root@localhost ~]# history -a
6)清空本次緩存區(qū);
[root@localhost ~]# history -c
[root@localhost ~]# history
1 history
7)如果要設(shè)置一個用戶的密碼,但是又擔心顯示在歷史記錄會被看到,可以用-d刪除;
[root@localhost ~]# useradd user1
[root@localhost ~]# echo "123456" | passwd --stdin user1
更改用戶 user1 的密碼 。
passwd:所有的身份驗證令牌已經(jīng)成功更新。
[root@localhost ~]# history 2
4 echo "123456" | passwd --stdin user1
5 history 2
[root@localhost ~]# history -d 4
[root@localhost ~]# history 3
4 history 2
5 history -d 4
6 history 3
8)因為history默認只能保留1000條歷史記錄的數(shù)量,所以若要增加數(shù)量到2000,可以通過修改變量寫入到文件里;
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# vim .bash_profile
[root@localhost ~]# tail -n1 .bash_profile
export HISTSIZE=2000
[root@localhost ~]# source .bash_profile
[root@localhost ~]# echo $HISTSIZE
2000
[root@localhost ~]#
總結(jié)
? ? ? ? history遇到重復(fù)執(zhí)行的命令,可以繼續(xù)用來執(zhí)行,并且這個命令的參數(shù)上不會太多,只有幾個常用到的。文章來源:http://www.zghlxwxcb.cn/news/detail-421000.html
????????如果覺得內(nèi)容還行,請一定要點個贊再走,謝謝!文章來源地址http://www.zghlxwxcb.cn/news/detail-421000.html
到了這里,關(guān)于Linux基礎(chǔ)命令-history歷史記錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!