Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 終端機 11 個必學高效操作技巧:快捷鍵、sudo!!、背景執行、nohup、at 排程

編輯:關於 Linux

熟練掌握終端機操作技巧,能將日常運維效率提升數倍。本文精選 11 個實戰高頻技巧,涵蓋編輯快捷鍵、權限提升、作業控制、離線執行、定時排程,附操作示範與記憶口訣。

終端機快捷鍵示意圖

1. 編輯列快捷鍵(Bash/Readline 預設綁定)

組合鍵動作記憶口訣
Ctrl+U剪切游標前至行首Up (向上/行首)
Ctrl+K剪切游標後至行尾Kill (殺向行尾)
Ctrl+Y貼上 (Yank)Yank
Ctrl+E游標跳至行尾End
Ctrl+A游標跳至行首Anchor (錨點/行首)
Alt+F跳至下一單字Forward
Alt+B跳回上一單字Backward
Alt+Backspace刪除前一單字向後刪字
Ctrl+W剪切游標後一單字Word kill
Shift+Insert貼上剪貼簿文字X11 標準貼上

實戰:輸入 sudo apt-get intall vim 發現拼錯,Alt+B 兩次跳到 intall,補 s 即可。

2. sudo !! — 以 root 重跑上一條指令

$ apt-get install htop
E: Could not open lock file - permission denied
$ sudo !!
sudo apt-get install htop   # 自動展開並以 sudo 執行

原理:!! 為歷史指令展開,代表上一條指令。

3. Ctrl+Z / fg — 暫停前景工作、切回背景

$ sudo nano /etc/nginx/nginx.conf
# 編輯到一半需查詢指令
Ctrl+Z
[1]+  Stopped                 sudo nano /etc/nginx/nginx.conf
$ nginx -t                    # 執行查詢
$ fg                          # 回到 nano 繼續編輯
  • 多次 Ctrl+Z 可疊加多個暫停工作,fg 依序恢復(後進先出)。
  • jobs 查看暫停工作列表,fg %2 指定恢復第 2 個。

4. nohup — 登出 SSH 後仍持續執行

nohup wget https://mirror.example.com/large.iso &
# 輸出導向 nohup.out,關閉終端機繼續下載
  • & 送入背景執行。
  • 輸出預設寫入 nohup.out,可用 > my.log 2>&1 指定檔案。

5. at — 指定時間一次性執行

$ at 22:30 tomorrow
at> /usr/local/bin/backup.sh
at> Ctrl+D
job 3 at Fri Jan 26 22:30:00 2024
  • atq 查看佇列,atrm 3 刪除任務 3。
  • 支援豐富時間格式:now + 1 hour、teatime (16:00)、midnight。

6. watch -n 1 'cmd' — 每秒重複執行並高亮變化

watch -n 1 'df -h /data'
watch -d -n 2 'ss -tulnp | grep :80'

7. !!:s/old/new — 歷史指令字串替換再執行

$ git commit -m "fix bug"
$ !!:s/fix/add feature
git commit -m "add feature bug"

8. cd - — 在最近兩個目錄間快速切換

$ cd /var/log/nginx
$ cd /etc/nginx
$ cd -   # 回 /var/log/nginx
$ cd -   # 回 /etc/nginx

9. !$ — 重用上一條指令的最後一個參數

$ mkdir -p /data/project/module1
$ cd !$   # 自動展開為 cd /data/project/module1

10. man cmd | col -b | grep -A5 -B5 keyword — man page 關鍵字搜尋並保留上下文

11. script /tmp/session.log — 錄製完整終端機會話

$ script /tmp/deploy.log
Script started, file is /tmp/deploy.log
$ ./deploy.sh
$ exit
Script done, file is /tmp/deploy.log
# 可回放:cat /tmp/deploy.log

將這 11 個技巧融入肌肉記憶,終端機操作從「打字」進化為「馴駕系統」。

Copyright © Windows教程網 All Rights Reserved