-c 建立歸檔、-x 解壓、-t 列出內容-f 指定檔名(必須緊跟其後)-v 顯示過程、-C 指定解壓目錄-z gzip 壓縮、-j bzip2 壓縮、-J xz 壓縮、-a 自動識別副檔名壓縮-r 追加檔案、--delete 刪除歸檔內檔案、--exclude 排除模式# 建立壓縮包
tar -czvf all.tar.gz *.txt
tar -cjvf all.tar.bz2 *.txt
# 解壓到指定目錄
tar -xzvf all.tar.gz -C /opt/app
# 僅解壓特定檔案
tar -xzvf all.tar.gz file1 file2
# 排除日誌檔
tar -czvf all.tar.gz --exclude="*.log" *
# 合併兩個 tar
tar -Af file1.tar file2.tar
gzip file → 產生 file.gz,**刪除原檔**gunzip file.gz → 還原,**刪除 .gz**-k 保留原檔(部分版本支援)-1~-9 壓縮等級(預設 -6)-l 列出壓縮資訊bzip2 file → file.bz2,預設刪除原檔-k 保留原檔zip -r all.zip dir/ 遞歸壓縮目錄,**保留原檔**unzip -l all.zip 列出內容unzip all.zip 解壓-u 更新壓縮包內檔案、-d 刪除壓縮包內檔案| 場景 | 推薦工具 |
|---|---|
| Linux 系統備份、原始碼發布 | tar + gzip/xz |
| 單一大型日誌/資料庫備份 | gzip / xz |
| 需最高壓縮比、CPU 允許 | bzip2 / xz |
| 與 Windows 用戶交換檔案 | zip |
掌握 tar 核心參數與壓縮搭配,覆蓋 90% 歸檔需求。gzip 速度快、通用性佳;bzip2/xz 壓縮比高;zip 兼容性最強。生產環境備份建議加上 --totals 統計大小、--checkpoint 進度顯示。