tar Cheatsheet - tar Archive & Compression Reference

All essential tar commands organized by use case, with 44+ entries you can copy and run directly. Find the right command fast when you need it.

SysOps·44 commands·Last updated 2026-07-21
Back to SysOps

Archive & Compress 7

tar -cvf archive.tar dir/
打package为 .tar(不Compress),-c Create -v Display -f File名
tar -czvf archive.tar.gz dir/
打package并用 gzip Compress(.tar.gz / .tgz),最常用
tar -cjvf archive.tar.bz2 dir/
打package并用 bzip2 Compress,比 gzip Compress率高但更慢
tar -cJvf archive.tar.xz dir/
打package并用 xz Compress,Compress率最高但最慢
tar --zstd -cvf archive.tar.zst dir/
用 zstd Compress,速度远快于 gzip,新Systemrecommended
tar -czvf archive.tar.gz --exclude="*.log" dir/
打package时排除 .log File
tar -czvf archive.tar.gz --exclude="node_modules" --exclude=".git" project/
排除多个Directory

Extract 8

tar -xvf archive.tar
解package .tar,-x 提取
tar -xzvf archive.tar.gz
Extract .tar.gz / .tgz
tar -xjvf archive.tar.bz2
Extract .tar.bz2 / .tbz2
tar -xJvf archive.tar.xz
Extract .tar.xz / .txz
tar --zstd -xvf archive.tar.zst
Extract .tar.zst
tar -xzvf archive.tar.gz -C /opt/
Extract到指定Directory(-C)
tar -xzvf archive.tar.gz file.txt
只Extractpackage内指定File
tar -xzvf archive.tar.gz --strip-components=1
Extract时去掉第一层Directory结构

List & Test 5

tar -tf archive.tar.gz
ListCompresspackage内容(不Extract),-t list
tar -tzvf archive.tar.gz
verboseList含Filesize和Permission
tar -tf archive.tar.gz | grep "config"
FindCompresspackage中的指定File
gzip -t archive.tar.gz
Test gzip 完整性,无Output=正常
tar -dvf archive.tar
对比Compresspackage内File与DiskFile差异

Compression Formats 6

-z (gzip)
速度最快,兼容性最好,.tar.gz / .tgz
-j (bzip2)
Compress率高于 gzip,速度较慢,.tar.bz2
-J (xz)
Compress率最高,速度最慢,.tar.xz
--zstd (zstd)
速度最快且Compress率好,新Systemrecommended,.tar.zst
-a
根据File后缀自动选择Compress格式
--use-compress-program=pigz
用 pigz 多线程 gzip 加速Compress

Other Compressors 8

zip -r archive.zip dir/
zip CompressDirectory
unzip archive.zip -d /opt/
Extract zip 到指定Directory
unzip -l archive.zip
Show zip 内容(不Extract)
gzip file.txt
Compress单个File为 file.txt.gz(原File消失)
gzip -k file.txt
Compress并保留原File(-k keep)
gunzip file.txt.gz
Extract .gz 单File
zcat file.txt.gz
不Extract直接Show .gz File内容
7z x archive.7z
Extract 7z 格式,需Install p7zip

Advanced 6

tar -rvf archive.tar newfile.txt
向已有 .tar packageAppendFile(不supportsCompresspackage)
tar -uvf archive.tar dir/
仅Update比package内新的File
tar -czf - dir/ | ssh host "tar -xzf - -C /dest"
通过 SSH pipe传输并Extract
tar -czf - dir/ | split -b 100M - archive.tar.gz.
分卷Compress为 100MB 的File
cat archive.tar.gz.* | tar -xzvf -
merge分卷并Extract
tar -g snapshot.snar -czf backup.tar.gz dir/
增量backup,记录snapshotFile

Common Examples 4

tar -czvf backup-$(date +%F).tar.gz /home/user/
带日期的Directorybackup
tar -czvf app.tar.gz --exclude="*.log" --exclude="tmp/" app/
打package项目排除日志和临时File
tar -xzvf package.tar.gz --strip-components=1 -C /opt/app
Extract到指定Directory并去掉顶层Directory
tar -czf - /data | pigz > backup.tar.gz
多线程Compress大Directory加速

💡 Tips

  • 记忆口诀:c=create(打package),x=extract(Extract),t=list(Show),z=gzip,j=bzip2,J=xz。
  • Extract前先用 tar -tf Show内容,confirm没有 ../ 路径穿越(zip slip 安全风险)。
  • 大FileCompress用 zstd --long=31 利用长距离Match,比 gzip 快 5-10 倍且Compress率更好。
  • tar AppendFile只supports未Compress的 .tar,对 .tar.gz 需先Extract再重新打package。
  • pigz 是 gzip 的多线程版本,Compress大File时用 tar -cf - dir | pigz > out.tar.gz 大幅加速。
  • -C 指定ExtractDirectory要在File名之后,--strip-components 去掉前 N 层Directory避免Extract出嵌套。

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it