← All cheatsheets

Shell

tar + zip — create + extract archives

Create, extract and inspect tar and zip archives, stream tar over SSH, and pick the right format for the job.

tar create

tar -cf out.tar dir/ — uncompressed

tar -czf out.tar.gz dir/ — gzip

tar -cjf out.tar.bz2 dir/ — bzip2 (smaller, slower)

tar -cJf out.tar.xz dir/ — xz (smallest, slowest)

tar extract

tar -xf in.tar — auto-detect compression

tar -xf in.tar.gz -C dst/ — extract into a specific dir

tar -xf in.tar dir/file — extract single file

tar inspect

tar -tf in.tar — list contents

tar -tvf in.tar — list with details

tar over SSH

tar cz dir | ssh host "tar xz -C /dst" — stream a dir to remote (no temp file)

zip + unzip

zip -r out.zip dir/ — recursive

zip -r out.zip dir/ -x '*.log' — exclude

unzip in.zip — extract

unzip in.zip -d dst/ — extract to dir

unzip -l in.zip — list contents

When to pick which

  • .tar.gz — Linux/macOS default; preserves permissions + symlinks
  • .zip — cross-platform; what Windows users expect
  • .tar.xz — when archive size matters more than time