# zstd — Fast Compression with Zstandard

> Practical guide to zstd (Zstandard) — fast compression with an excellent speed-to-ratio balance, multi-threading and tar integration.

Source: https://www.jpkc.com/db/en/cheatsheets/archives/zstd/

<!-- PROSE:intro -->
zstd (Zstandard) is a modern compression tool from Meta that strikes a remarkable balance between speed and compression ratio – at the default level 3 it compresses in near real time, while higher levels reach ratios on par with xz. On top of that come multi-threading, seamless tar integration and dictionary compression for many small files. This guide walks you through the commands you reach for daily, from quick compression to a streamed database backup.
<!-- PROSE:intro:end -->

## Compress

`zstd <file>` — Compress a file (creates file.zst, keeps original).

```bash
zstd data.log
```

`zstd -<level> <file>` — Compress with a specific level (1=fastest, 19=best, default 3).

```bash
zstd -19 data.log
```

`zstd --ultra -<level> <file>` — Use ultra compression levels (20-22, very slow but smallest).

```bash
zstd --ultra -22 data.log
```

`zstd -o <output> <file>` — Compress to a specific output filename.

```bash
zstd -o backup.zst data.log
```

`zstd --rm <file>` — Compress and remove the original file.

```bash
zstd --rm data.log
```

`zstd -T<n> <file>` — Multi-threaded compression (0=auto-detect cores).

```bash
zstd -T0 -19 largefile.dat
```

## Decompress

`zstd -d <file>.zst` — Decompress a file.

```bash
zstd -d data.log.zst
```

`unzstd <file>.zst` — Decompress (alias for zstd -d).

```bash
unzstd data.log.zst
```

`zstd -d -o <output> <file>.zst` — Decompress to a specific filename.

```bash
zstd -d -o restored.log data.log.zst
```

`zstdcat <file>.zst` — Decompress to stdout (like zcat for gzip).

```bash
zstdcat data.log.zst | grep 'error'
```

`zstd -d --rm <file>.zst` — Decompress and remove the compressed file.

```bash
zstd -d --rm data.log.zst
```

## Piping & Streaming

`<command> | zstd > <output>.zst` — Compress piped data.

```bash
mysqldump mydb | zstd > backup.sql.zst
```

`zstd -d < <input>.zst | <command>` — Decompress and pipe to a command.

```bash
zstd -d < backup.sql.zst | mysql mydb
```

`tar cf - <dir> | zstd > <archive>.tar.zst` — Create a tar.zst archive (tar + zstd).

```bash
tar cf - project/ | zstd -T0 > project.tar.zst
```

`zstd -d < <archive>.tar.zst | tar xf -` — Extract a tar.zst archive.

```bash
zstd -d < project.tar.zst | tar xf -
```

`tar --zstd -cf <archive>.tar.zst <dir>` — Create tar.zst using tar's built-in zstd support (GNU tar).

```bash
tar --zstd -cf project.tar.zst project/
```

`tar --zstd -xf <archive>.tar.zst` — Extract tar.zst using tar's built-in support.

```bash
tar --zstd -xf project.tar.zst
```

## Multiple Files

`zstd <file1> <file2> <file3>` — Compress multiple files individually.

```bash
zstd *.log
```

`zstd -d *.zst` — Decompress multiple files.

```bash
zstd -d *.zst
```

`zstd -r <dir>` — Recursively compress all files in a directory.

```bash
zstd -r /var/log/old/
```

## Info & Testing

`zstd -l <file>.zst` — Show compression info (ratio, sizes).

```bash
zstd -l data.log.zst
```

`zstd -t <file>.zst` — Test integrity of a compressed file.

```bash
zstd -t backup.sql.zst
```

`zstd -b <file>` — Benchmark compression/decompression speed.

```bash
zstd -b data.log
```

`zstd -b -e<level> <file>` — Benchmark across compression levels.

```bash
zstd -b1 -e19 data.log
```

`zstd -V` — Show the zstd version.

```bash
zstd -V
```

<!-- PROSE:outro -->
## Conclusion

zstd is the obvious choice today when you want speed and compression ratio at the same time: the default level 3 is blazing fast, `-T0` scales across all cores, and `tar --zstd` makes archiving convenient. Keep two things in mind: `--rm` deletes the original irreversibly – only use it once the compressed file has safely arrived. And the ultra levels `--ultra -20` through `-22` need significantly more memory not just when compressing but also when decompressing – so use them with care on tightly provisioned target systems.

## Further Reading

- [zstd – GitHub project](https://github.com/facebook/zstd) – source code, releases and documentation
- [Zstandard – official project site](https://facebook.github.io/zstd/) – format specification, benchmarks and manual
<!-- PROSE:outro:end -->

## Related Commands

- [7z](https://www.jpkc.com/db/en/cheatsheets/archives/7z/) – high-ratio archive format with strong LZMA compression
- [gzip](https://www.jpkc.com/db/en/cheatsheets/archives/gzip/) – classic, widely supported compressor (DEFLATE)
- [tar](https://www.jpkc.com/db/en/cheatsheets/archives/tar/) – bundles files into an archive, often paired with zstd

