Linux 指令行藏無數寶藏,以下 11 個冷門指令能大幅提升桌面/伺服器管理效率。
sudo !! — 以 root 重跑上一條指令$ apt-get update
E: Could not open lock file ... Permission denied
$ sudo !!
sudo apt-get update
[sudo] password for user: ...
python -m SimpleHTTPServer — 秒架靜態檔案伺服器# 當前目錄以 HTTP 埠 8000 服務
python -m SimpleHTTPServer
# 瀏覽器訪問 https://localhost:8000

mtr — ping + traceroute 二合一網路診斷# 安裝
sudo apt-get install mtr # Debian/Ubuntu
yum install mtr # RHEL/CentOS
# 使用
mtr google.com

長指令、多行腳本直接在編輯器修改再執行,極大減少輸入錯誤。
nl — 為檔案內容加行號$ cat one.txt
fedora
debian
arch
slack
suse
$ nl one.txt
1 fedora
2 debian
3 arch
4 slack
5 suse
shuf — 隨機抽樣行/檔案$ ls | shuf # 亂序列出
$ ls | shuf -n1 # 隨機挑 1 個
$ ls | shuf -n3 # 隨機挑 3 個
ss — 現代化 socket 統計(替代 netstat)# 顯示所有 TCP 連線
ss -t -a
# 顯示監聽埠
ss -l -n -p
# 顯示連線狀態統計
ss -s

timeout — 限制指令執行時間timeout 5s ping google.com # 5 秒後自動殺掉
disown — 將工作從 shell 作業控制移除long_running_task &
disown -h %1 # 關閉終端機也不會收到 SIGHUP
tee — 同時輸出到標準輸出與檔案echo "log entry" | tee -a /var/log/mylog.log
watch — 定期重複執行指令並高亮差異watch -n 1 -d 'free -h' # 每秒更新記憶體使用,高亮變化
watch -n 2 'gpustat -c' # 監控 GPU 狀態
善用這些小工具,讓日常運維事半功倍。