有時候 我們需要將一個檔案或目錄 切成許多份小檔 進行傳送
例如在用 email 寄信時 附件檔案有大小的限制
透過下列指令
split
md5sum (optional)
tar
Quick Ref - without md5sum
如果需要壓縮 在使用 tar 打包時
-z (gzip) -j (bzip2)
傳送前
>>> tar -cf - large_file_or_dir | split -b 20m -d - _backup.tar.
傳送後
>>> cat _backup.tar.* | tar -xf -
OR
>>> find -maxdepth 1 -type f -name '_backup.tar.*' | sort | xargs cat | tar -xf -
Quick Ref - with md5sum
傳送前
>>> tar -cf backup.tar large_file_or_dir # 打包
>>> md5sum backup.tar > backup.tar.md5
>>> split -b 20m -d backup.tar _backup.tar. # 分割
傳送後
>>> cat _backup.tar.* > backup.tar # 合併
OR
>>> find -maxdepth 1 -type f -name '_backup.tar.*' | sort | xargs cat > backup.tar # 合併
>>> md5sum -c backup.tar.md5 # 驗證
>>> tar -xf backup.tar # un tar
split 分割大檔案 用法
split 分割
>>> split --bytes=1m --numeric-suffixes --suffix-length=3 /path/to/large/file /path/to/output/file/prefix
一樣動作 用 short option
>>> split -b 1m -d -a 3 /path/to/larg/file /path/to/output/file/prefix
cat 結合
>>> cat prefix* > new-file-name
OR
>>> find -maxdepth 1 -type f -name 'prefix*' | sort | xargs cat > new-file-name
實例
將一個 hello.iso 檔, 用 20m 切, 分割後的檔名
- 以 hello_ 開頭
- 以 3位數字 (從000開始遞增) 結尾
>>> split -b 20m -d -a 3 hello.iso hello_
>>> ls hello_* hello_000 hello_001 hello_002
用 cat 將檔案重新組合
>>> cat hello_* > hello2.iso
OR
>>> find -maxdepth 1 -type f -name 'hello_*' | sort | xargs cat > hello2.iso
Optional 驗證檔案完整性 - md5sum
>>> md5sum hello.iso > hello.iso.md5
自動比對
讓 hello.iso, hello.iso.md5 在相同資料夾
>>> md5sum -c file.iso.md5
人眼比對
>>> md5sum hello.iso
mac 的話 將 md5sum 改成 md5
其他類似工具
由於 md5sum 的演算法不可靠
在有高度安全性的考量 (檔案遭惡意竄改) 下請使用其他工具
sha256sum
gpg
tar split
用 tar 將多個檔案 合併成一個檔案
將目錄 hello_dir 打包為 hello_dir.tar
>>> tar -cvf hello_dir.tar hello_dir/如果需要壓縮
-z (gzip) -j (bzip2)
Step 1 tar, split 分割
>>> tar -cvf - hello_dir/ | split -b 20m - hello_dir.tar.
Step 2 用 cat 還原
>>> cat hello_dir.tar.* | tar -xvf -OR
>>> find -maxdepth 1 -type f -name 'hello_dir.tar.*' | sort | xargs cat > hello_dir
tar -v
verbosely list files processed
tar 中的 -
-f file Read the archive from or write the archive to the specified file. The filename can be - for standard input or standard output.
split 中的 -
The split utility reads the given file and breaks it up into files of 1000 lines each. If file is a single dash (-) or absent, split reads from the standard input.
$ man tar
$ man split
Tip 用 tar 解壓縮時
tar 可以自動分辨格式 用 tar xvf 就可以 (不用額外指定 z 或 j)