Linux命令幫助及history命令的使用

1.Linux命令幫助的獲取詳解

在Linux中獲取命令幫助時,內部命令和外部命令的獲取方式是有區別的:

 (1)內部命令:#help COMMAND

?            #man bash

 (2)外部命令:<1> # COMMAND –help

                   # COMMAND -h


               <2>  使用手冊(manual)

                   # man COMMAND

               <3> 信息頁

                   # info COMMAND

                <4>  程序自身的幫助文檔等

                       README

                       INSTALL

                       ChangeLog


內部命令本身一開機就會隨bash加載到內存中

而外部命令只會運行后才會加載到內存中可以用hash命令看到

當前shell為bash如果想要查看bash幫助信息,可以通過man bash 來查看,同時我們知道bash提供了許多內部命令如cd ,l等,如果用man bash 來查看幫助信息實際看到的為bash的幫助信息,因此內部命令不通過man來查看。

[root@localhost ~]# echo $PATH

/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


 hash表的作用:大大提高命令的調用速率。

 hash的參數:

[root@redhat ~]# hash  //輸入hash或hash -l 可以查看hash表的內容,我剛開機所以為空

hash: hash table empty

[root@redhat ~]# hash -l

hash: hash table empty

 

當我執行過2條命令后再看:

[root@redhat ~]# hash  //hash表會記錄下執行該命令的次數,以及命令的絕對路徑

hits command

   1 /bin/cat

   1 /bin/ls

[root@redhat ~]# hash -l  //加參數-l既可以看到hash表命令的路徑,也可以看到它的名字,說不定會有別名

builtin hash -p /bin/cat cat

builtin hash -p /bin/ls ls

 

[root@redhat ~]# hash -p /bin/ls bb  //添加hash表,可以看到我把ls命令重新寫了一遍,改名為bb

當前shell為bash如果想要查看bash幫助信息,可以通過man bash 來查看,同時我們知道bash提供了許多內部命令如cd ,l等,如果用man bash 來查看幫助信息實際看到的為bash的幫助信息,因此內部命令不通過man來查看。

[root@localhost ~]# echo $PATH

/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


 hash表的作用:大大提高命令的調用速率。

 hash的參數:

[root@redhat ~]# hash  //輸入hash或hash -l 可以查看hash表的內容,我剛開機所以為空

hash: hash table empty

[root@redhat ~]# hash -l

hash: hash table empty

 

當我執行過2條命令后再看:

[root@redhat ~]# hash  //hash表會記錄下執行該命令的次數,以及命令的絕對路徑

hits command

   1 /bin/cat

   1 /bin/ls

[root@redhat ~]# hash -l  //加參數-l既可以看到hash表命令的路徑,也可以看到它的名字,說不定會有別名哦

builtin hash -p /bin/cat cat

builtin hash -p /bin/ls ls

 

[root@redhat ~]# hash -p /bin/ls bb  //添加hash表,可以看到我把ls命令重新寫了一遍,改名為bb

[root@redhat ~]# bb    //當我執行bb時就是執行ls

anaconda-ks.cfg        icmp_echo_ignore_aly~  pub.key

dead.letter        icmp_echo_ignore_alz~  rpmbuild

icmp_echo_ignore_all~  install.log       RPM-GPG-KEY-useradd

icmp_echo_ignore_alw~  install.log.syslog     RPM-GPG-KEY-westos

icmp_echo_ignore_alx~  passwd

 

[root@redhat ~]# hash -t ls  //-t參數可以查看hash表中命令的路徑,要是hash表中沒有怎么辦?

/bin/ls

[root@redhat ~]# hash -t df  //我沒使用過df,執行hash,就會提示找不到該命令

-bash: hash: df: not found

 

[root@redhat ~]# hash -r  //清楚hash表,清楚的是全部的

[root@redhat ~]# hash -l

hash: hash table empty

 

[root@redhat ~]# hash -l

builtin hash -p /bin/cat cat

builtin hash -p /bin/ls ls

[root@redhat ~]# hash -d cat   //清楚其中的某一條

[root@redhat ~]# hash -l

builtin hash -p /bin/ls ls


2.history命令

〈一〉history是管理命令歷史,每次執行的命令都會紡織在內存緩存中,直到退出當前終端后保存在歷史文件中。

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 那么多,舊的訊息會被主動的拿掉!僅保留最新的

〈二〉簡單常用的調用歷史中的命令。

[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 個)

原創文章,作者:Zzuimeng,如若轉載,請注明出處:http://www.www58058.com/28485

(0)
ZzuimengZzuimeng
上一篇 2016-08-05 16:15
下一篇 2016-08-05 16:15

相關推薦

  • 2016/10/19作業:軟鏈接與硬鏈接的區別

    軟鏈接和硬鏈接的區別 linux中鏈接分為兩種,一種是硬鏈接(Hard Link),一種是符號鏈接(Symbolic Link),其中,符號鏈接又稱為軟鏈接。 硬鏈接: 在Linux文件系統中 ,磁盤中的所有文件都有一個編號,這個編號稱為索引節點(Inode)。在Linux中,多個同一索引節點可以有多個文件名,這就是硬鏈接。硬鏈…

    Linux干貨 2016-10-20
  • keepalived相關配置示例(二)

    虛擬服務器配置實驗: 兩臺centos6主機為web service系統為CentOS6.8 n1:172.16.253.87 n2:172.16.252.5都安裝httpd yum install -y httpd[root@n1 ~]#vim /var/www/html/index.html<h1>Real Server 1</h1&g…

    Linux干貨 2017-06-28
  • N-28作業第一周

    N-28作業第一周小結:以前看存儲視頻里面講存儲架構里不清楚的部分居然開始清晰。基礎命令需要多花時間熟悉。

    2017-12-05
  • 用戶、組及其管理

    用戶和組管理    Linux是一個多用戶、多任務的操作系統。多用戶、多任務就是可以在系統上建立多個用戶,多個用戶可以在同一時間內登錄同一臺主機的系統執行不同的任務,而互不影響。例如某臺linux服務器上有4個用戶,分別是root、www、ftp和mysql,在同一時間內root用戶可能在管理維護系統,www用戶可能在修改自己的程序和操作…

    Linux干貨 2016-08-04
  • Homework Week-10 CentOS啟動相關及bash腳本編程

    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) 2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;   (1) 為硬盤新建兩個主分區;并為其安裝grub;   (2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;   (3) 為rootfs提供…

    Linux干貨 2016-10-24
欧美性久久久久