Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> สมาร์ทโฟน >> Linux

วิธีสร้างและแตกไฟล์ ZIP, TAR, TAR.GZ, TAR.BZ2 ใน Linux

คู่มือครบครันสำหรับการบีบอัดและแตกไฟล์ใน Linux: ZIP, TAR, TAR.GZ, TAR.BZ2, TAR.XZ

เปรียบเทียบ Format

คำสั่งสรุป (Cheat Sheet)

ZIP


# สร้าง (Recursive)
zip -r archive.zip /path/to/dir

# แตก
unzip archive.zip

# แตกไปโฟลเดอร์เฉพาะ
unzip archive.zip -d /destination

# ดูรายการ
unzip -l archive.zip

TAR (No Compression)


# สร้าง
tar -cvf archive.tar /path/to/dir

# แตก
tar -xvf archive.tar

# แตกไปโฟลเดอร์เฉพาะ
tar -xvf archive.tar -C /destination

TAR.GZ (Gzip) - แนะนำใช้งานทั่วไป


# สร้าง
tar -czvf archive.tar.gz /path/to/dir

# แตก
tar -xzvf archive.tar.gz

# แตกไปโฟลเดอร์เฉพาะ
tar -xzvf archive.tar.gz -C /destination

TAR.BZ2 (Bzip2)


# สร้าง
tar -cjvf archive.tar.bz2 /path/to/dir

# แตก
tar -xjvf archive.tar.bz2

TAR.XZ (XZ/LZMA) - บีบอัดได้ดีที่สุด


# สร้าง
tar -cJvf archive.tar.xz /path/to/dir

# แตก
tar -xJvf archive.tar.xz

อธิบาย Flags

FormatCompressionSpeedCompatibilityUse Case
ZIPMediumFastทุก OS (Built-in)Share with Windows/Mac
TARNone (Archive only)Very FastLinux/UnixBackup ชั่วคราว, Pipe
TAR.GZGoodFastLinux/Unix, Windows 10+General Purpose (แนะนำ)
TAR.BZ2BetterSlowerLinux/UnixArchive จำเป็นต้องเล็กที่สุด
TAR.XZBestSlowestLinux/Unix (Modern)Distribution, Long-term Archive

Progress Bar (ต้องมี pv)


# สร้าง
tar -cf - /path/to/dir | pv | gzip > archive.tar.gz

# แตก
pv archive.tar.gz | tar -xzf -

Parallel Compression (Multi-core)


# pigz (Parallel gzip)
tar -cf - /path | pigz > archive.tar.gz

# pbzip2 (Parallel bzip2)
tar -cf - /path | pbzip2 > archive.tar.bz2

# pxz (Parallel xz)
tar -cf - /path | pxz > archive.tar.xz

Best Practices

  • ใช้ tar.gz สำหรับ Backup ทั่วไป (Balance Speed/Size)
  • ใช้ tar.xz สำหรับ Archive ระยะยาว/แจกจ่าย
  • ใช้ zip เมื่อส่งให้ Windows User
  • Always verify: tar -tzf archive.tar.gz หรือ unzip -l archive.zip
  • ใช้ -p รักษา Permission เมื่อ Backup System Files

การเลือก Format ขึ้นอยู่กับ: ความเร็ว, อัตราการบีบอัด, และ Compatibility กับปลายทาง

Flagคำอธิบาย
cCreate
xExtract
vVerbose (แสดงไฟล์)
fFile (ระบุชื่อไฟล์ archive)
zGzip
jBzip2
JXZ
CChange Directory (Extract to)
tList (List contents)
pPreserve Permissions