面對數 GB 的日誌檔、資料庫備份或需傳輸的大檔案,split 是 Linux 內建的高效分割工具,支援依位元組大小、行數切割,並可自訂前綴與尾碼格式 (字母/數字)。本文透過實測範例,完整說明分割、合併、驗證與常用參數技巧。
split [選項] [輸入檔] [輸出前綴]
# 輸出檔名格式:前綴 + 尾碼 (預設 aa, ab, ac... 或 00, 01, 02...)
| 參數 | 說明 | 範例 |
|---|---|---|
-b SIZE, --bytes=SIZE | 依大小切割 (K/M/G 等單位) | -b 20M, --bytes=50M |
-l LINES, --lines=LINES | 依行數切割 | -l 100, -l 1000 (預設 1000 行) |
-d, --numeric-suffixes | 尾碼改用數字 (00, 01...) | split -d file prefix |
-a N, --suffix-length=N | 指定尾碼長度 (預設 2) | -a 3 → 000, 001... |
--verbose | 顯示處理進度 |
| 需求 | 指令 | 輸出範例 |
|---|---|---|
| 預設 1000 行分割 | split mylog | xaa, xab, xac... (各 1000 行) |
| 每 20MB 分割 | split -b 20M logdata | xaa (20M), xab (20M), xaf (1.6M)... |
| 每 50MB 分割 + 自訂前綴 | split --bytes=50M logdata mydatafile | mydatafileaa (50M), mydatafileab (50M)... |
| 每 1500 行分割 + 前綴 | split -l 1500 testfile importantlog | importantlogaa (1500), importantlogab (1091) |
| 數字尾碼 00,01... | split -d testfile | x00, x01, x02... |
tar czf - /var/log | split -b 100M - backup_log_ (邊打包邊分割,不佔暫存空間)split -d -a 4 -b 100M bigfile part_ → part_0000, part_0001...cat part_* | ssh user@remote "cat > bigfile" (跨主機傳輸合併)split 以簡潔參數覆蓋「大小/行數」二維切割需求,配合 -d 數字尾碼與 cat 合併,構成大檔案處理、傳輸、備份的基石工具鏈。掌握上述參數組合,即可輕鬆應對 TB 級資料切分與遷移場景。