Linux運維基礎之history命令詳解

Linux運維基礎之history命令詳解

Linux 命令 history  是一個方便的工具,它能顯示使用者之前在終端中執行過的所有命令列表。該列表儲存在歷史檔案中,使用歷史命令可以檢視該檔案,從而更容易回憶和重複使用過去的命令。

該命令通常用於跟蹤在終端中執行的操作,尤其是在排除故障或重複複雜命令序列時。它可以讓使用者快速回憶和重新執行以前的命令,而無需再次鍵入,從而節省時間和精力。有些使用者還利用歷史記錄來審計或審查系統中的操作,從而有助於系統管理和安全。

history 命令的一般語法:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history [OPTIONS...]
history [OPTIONS...]
history [OPTIONS...]

1. 顯示使用過的命令

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history [number]
history [number]
history [number]

這條命令顯示一定數量的以前執行過的命令。

示例:

Linux 中的 history 5 命令將顯示最近在終端中執行的 5 條命令。下面是輸出示例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
501 git status
502 cd /var/www/html
503 ls -la
504 nano myfile.txt
505 history 5
501 git status 502 cd /var/www/html 503 ls -la 504 nano myfile.txt 505 history 5
501  git status
502  cd /var/www/html
503  ls -la
504  nano myfile.txt
505  history 5

每行顯示歷史記錄中的一條命令,前面有一個唯一的數字,代表該命令在歷史檔案中的位置。在本例中,顯示了最後執行的五條命令,包括 history 5 命令本身。

2. 從歷史記錄列表中刪除命令

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history -d [number]
history -d [number]
history -d [number]

使用 -d 選項刪除歷史記錄列表中的命令。

示例:

刪除前:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 ls
2 cd Documents/
3 touch file.txt
4 nano file.txt
5 rm file.txt
6 history
1 ls 2 cd Documents/ 3 touch file.txt 4 nano file.txt 5 rm file.txt 6 history
1  ls
2  cd Documents/
3  touch file.txt
4  nano file.txt
5  rm file.txt
6  history

執行 history -d 5

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 ls
2 cd Documents/
3 touch file.txt
4 nano file.txt
5 history
1 ls 2 cd Documents/ 3 touch file.txt 4 nano file.txt 5 history
1  ls
2  cd Documents/
3  touch file.txt
4  nano file.txt
5  history

可以看到,第 5 條命令( rm file.txt )已從歷史記錄中刪除。

3. 在歷史記錄中搜尋使用過的命令

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history | grep [text]
history | grep [text]
history | grep [text]

history | grep 命令可幫助查詢與文字模式匹配的命令。

例如:

Linux 中的 history | grep cat 命令將搜尋命令歷史記錄,並顯示所有包含單詞 “cat” 的行。grep 命令會過濾 history 命令的輸出,只顯示包含指定模式(本例中為 “cat”)的行。下面是一個輸出示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
42 cat file.txt
73 cat /etc/passwd
100 cat documents/note.txt | more
123 concatenate file1.txt file2.txt > combined.txt
150 cat logs/error.log | grep "ERROR"
42 cat file.txt 73 cat /etc/passwd 100 cat documents/note.txt | more 123 concatenate file1.txt file2.txt > combined.txt 150 cat logs/error.log | grep "ERROR"
42  cat file.txt
73  cat /etc/passwd
100  cat documents/note.txt | more
123  concatenate file1.txt file2.txt > combined.txt
150  cat logs/error.log | grep "ERROR"

在這個示例中,每一行都以代表命令在歷史記錄中位置的數字開頭,然後是命令本身。它包括 “cat” 出現的每一個例項,無論它是 cat 命令本身,還是其他單詞或命令的一部分。

4. 清除歷史記錄列表

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history -c
history -c
history -c

-c (clear) 選項用於清除歷史記錄列表中的所有命令。

5. 用事件編號執行命令

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
!number
!number
!number

使用該命令可以執行歷史記錄列表中帶有事件編號的命令。無需編寫完整的命令。

示例:

假設歷史記錄中的第 10 個命令是 ls -l,它以長格式列出當前目錄下的檔案。如果執行 !10 命令,輸出結果可能如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
total 24
drwxr-xr-x 5 user user 4096 Aug 6 10:00 Documents
drwxr-xr-x 2 user user 4096 Aug 6 09:45 Downloads
drwxr-xr-x 2 user user 4096 Aug 6 09:45 Music
drwxr-xr-x 2 user user 4096 Aug 6 09:45 Pictures
total 24 drwxr-xr-x 5 user user 4096 Aug 6 10:00 Documents drwxr-xr-x 2 user user 4096 Aug 6 09:45 Downloads drwxr-xr-x 2 user user 4096 Aug 6 09:45 Music drwxr-xr-x 2 user user 4096 Aug 6 09:45 Pictures
total 24
drwxr-xr-x 5 user user 4096 Aug  6 10:00 Documents
drwxr-xr-x 2 user user 4096 Aug  6 09:45 Downloads
drwxr-xr-x 2 user user 4096 Aug  6 09:45 Music
drwxr-xr-x 2 user user 4096 Aug  6 09:45 Pictures

該輸出顯示了當前目錄的內容,就像直接執行 ls -l 命令一樣。

6. 將更改寫入 bash_history 檔案

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
history -w [filename]
history -w [filename]
history -w [filename]

使用此選項可將當前會話中的所有更改寫入 bash_history 檔案。

示例:

Linux 中的 history -w file.txt 命令不會在終端中產生輸出。相反,它會將當前歷史命令寫入名為 file.txt 的檔案。

因此,如果你開啟 file.txt,可能會看到如下內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 cd /home/user/documents
2 ls -la
3 sudo apt-get update
4 nano file.txt
5 history -w file.txt
1 cd /home/user/documents 2 ls -la 3 sudo apt-get update 4 nano file.txt 5 history -w file.txt
1 cd /home/user/documents
2 ls -la
3 sudo apt-get update
4 nano file.txt
5 history -w file.txt

現在,該檔案包含之前在終端中執行的命令列表,你可以使用任何文字編輯器檢視或編輯它。

更多 Linux 命令

下面羅列了最常見的一些 Linux 命令,您可以根據自己的需要查閱對應命令的詳細解析:

目錄操作 rmdir · cd · pwd · exa · ls
檔案操作 cat · cp · dd · less · touch · ln · rename · more · head
檔案系統操作 chown · mkfs · locate
網路 ping · curl · wget · iptables · mtr
搜尋和文字處理 find · grep · sed · whatis · ripgrep · fd · tldr
系統資訊和管理 env · history · top · who · htop · glances · lsof
使用者和會話管理 screen · su · sudo · open

此外,我們還整理 Linux 命令列大全,以幫助大家全面深入地學習 Linux。

評論留言