ls | grep "error" # 過濾檔名
ps aux | grep nginx | grep -v grep # 精準查進程
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10 # 統計 Top10 IP
Unix 哲學:每個程式只做一件事,管道將它們串聯。
* 匹配任意字符 (含零個)? 匹配單一字符[abc] 匹配括號內任一字符rm report_2023_*.pdf、cp file?.txt /backup/警告:rm * 會刪除當前目錄所有非隱藏檔,請三思!
command > file 覆蓋寫入 stdoutcommand >> file 追加寫入command 2> err.log 導向 stderrcommand &> all.log 合併 stdout+stderr (Bash)history 列出全部,配合 grep 搜尋:history | grep ssh!! 重複上一條、!n 執行第 n 條、!?keyword? 反向搜尋執行~ 當前用戶家目錄 (cd ~ = cd). 當前目錄 (./script.sh 執行當前腳本).. 父目錄 (cd ../.. 往上兩層)cd ~/Documents、cp ../file.txt .firefox & # 瀏覽器後台跑,終端繼續可用
sleep 100 & # 後台睡眠
jobs # 查看後台任務
fg %1 # 將任務 1 調回前台
bg %1 # 暫停任務轉後台 (Ctrl+Z 後輸入 bg)
配合 nohup 或 screen/tmux 實現關閉終端後仍持續運行。
cmd1 && cmd2:cmd1 成功 (exit 0) 才執行 cmd2cmd1 || cmd2:cmd1 失敗才執行 cmd2./configure && make && sudo make install (任一步驟失敗停止)ping -c1 host || echo "host down"這 8 個技巧是 Linux 終端高手的肌肉記憶。刻意練習兩週,即可內化為直覺,大幅提升命令列操作速度與可靠性。進階者可進一步探索 readline 快捷鍵、bash-completion、fzf 模糊搜尋等現代工具。