歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux基礎系列3(history命令)

Linux基礎系列3(history命令)

日期:2017/3/3 11:08:02   编辑:Linux技術

history顧名思義,歷史的意思,是Linux中的歷史命令,通過它,可以讓你在平常使用Linux的過程中更快捷的達到自己想要的結果。以下為history幾個常用的命令選項和幾個有關history的環境變量

用法: history [-c] [-d offset]

history -anrw [filename] 或

history -ps arg [arg...]

-c: 清空命令歷史

-d: 刪除歷史中指定的命令;

#: 顯示最近的#條歷史

-a: 追加本次會話新執行的命令歷史列表至歷史文件

-n: 讀歷史文件中未讀過的行到歷史列表

還沒有清空歷史列表時是這樣的

339  head 10 /etc/passwd
  340  head 10 /etc/passwd 
  341  head -n 10 /etc/passwd 
  342  head -n 10 /etc/passwd | tail -n 5 
  343  head -n 10 /etc/passwd | tail -n 5 | sort -d: -n -k3 
  344  head -n 10 /etc/passwd | tail -n 5 | sort -t: -n -k3 
  345  head -n 10 /etc/passwd | tail -n 5 | sort -t: -n -k3 | cut -d: -f 1
  346  md5
  347  md5sum 
  348  useradd shui
  349  su - xiaoshui
  350* gpasswd shui man
  351  man gpasswd
  352  man history 
  353  type history
  354  help history
然後history -c 清空歷史列表,在使用history查看
350* gpasswd shui man
  351  man gpasswd
  352  man history          
  353  type history
  354  help history
  355  history
[root@localhost ~]# history -c
[root@localhost ~]# history 
    1  history
-d用於刪除指定的歷史命令,操作如下

[root@localhost etc]# history 
    1  ifconfig 
    2  cd /etc/
    3  ls
    4  history 
[root@localhost etc]# history -d 2
[root@localhost etc]# history
    1  ifconfig 
    2  ls
    3  history 
    4  history -d 2
    5  history
可以看出原來的2號命令 cd /etc 命令不見了-a追加歷史列表中的命令至歷史文件,在當前shell進程下所輸入的命令雖然會愛歷史列表中出現,但不會立即寫入歷史文件,如果想寫入 執行history -a,如下所示

寫入前

HISTCONTROL=""
#1469616878
clear
#1469616886
name=xiaoshui
#1469616889
echo $name
#1469616891
bash
寫入後
clear
#1469616886
name=xiaoshui
#1469616889
echo $name
#1469616891
bash
cat .bash_history 
ifconfig '=
ifconfig 
ls
cd
history 
history -a

有關history的環境變量

HISTFILE:定義歷史文件

[root@localhost ~]# echo $HISTFILE
/root/.bash_history
HISTSEZE:定義命令歷史最多命令條數
[root@localhost ~]# echo $HISTSIZE
1000
HISTFILESIZE:定義歷史文件中最大命令條數

[root@localhost ~]# echo $HISTFILESIZE
1000
HISTTIMEFORMAT :歷史命令時間格式
[root@localhost ~]# HISTTIMEFORMAT=" %F % T"
153   2016-07-27 % THISTTIMEFORMAT=“%F %T“
  154   2016-07-27 % THISTTIMEFORMAT=" %F % T"
  155   2016-07-27 % Tdate
  156   2016-07-27 % Thistory
HISTIGNORE="STR1:STR2" 忽略string1,string2歷史

[root@localhost ~]# HISTIGNORE="passwd"
[root@localhost ~]# passwd
Changing password for user root.
New password: 
BAD PASSWORD: No password supplied
154   2016-07-27 % THISTTIMEFORMAT=" %F % T"
  155   2016-07-27 % Tdate
  156   2016-07-27 % Thistory 
  157   2016-07-27 % THISTIGNORE="passwd"
  158   2016-07-27 % Thistory
[root@localhost ~]#
HISTCONTROLignoredups:忽略重復的命令;連續且相同方為“重復”

ignorespace:忽略所有以空白開頭的命令

ignoreboth:ignoredups, ignorespace

[root@localhost ~]# HISTCONTROL="ignoreboth"
[root@localhost ~]#  ls
1  100  12  anaconda-ks.cfg  history_log  test  text
[root@localhost ~]# pwd
/root
[root@localhost ~]# pwd
/root
[root@localhost ~]# pwd
/root
[root@localhost ~]#
158   2016-07-27 % Thistory
  159   2016-07-27 % THISTIGNORE=""
  160   2016-07-27 % THISTCONTROL="ignoreboth"
  161   2016-07-27 % Tpwd
  162   2016-07-27 % Thistory
走的匆忙,環境變量HISTTIMEFORMAT中的%T誤在中間輸了一個空格,然後就成這樣了,可以充當反面教材吧。正確輸入的方法是%T,把空格去掉即可。本文出自 “學無止境” 博客,請務必保留此出處http://dashui.blog.51cto.com/11254923/1830885

Copyright © Linux教程網 All Rights Reserved