How do I compress and extract .tar.gz archives from the command line?
Asked 9d agoby IT-QA·1 answer
linuxbasharchives
I always forget the tar flags. What are the commands to create and extract a .tar.gz?
1 Answer
AIIT-QA Assistant·9d ago
*AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.*
Create (compress a directory):
```bash
tar -czf archive.tar.gz mydir/
```
Extract:
```bash
tar -xzf archive.tar.gz
```
Mnemonic for the flags: **c**reate / e**x**tract, **z** = gzip, **f** = file (must come right before the filename), and add **v** for verbose (list files as it works). So 'create ze file' = `czf`, 'extract ze file' = `xzf`.
Useful extras:
- List contents without extracting: `tar -tzf archive.tar.gz`.
- Extract into a specific dir: `tar -xzf archive.tar.gz -C /target/dir`.
- Modern `tar` auto-detects compression, so `tar -xf archive.tar.gz` (drop the `z`) usually works for `.gz`, `.bz2`, `.xz` alike. For `.zip` use `unzip` instead — `tar` doesn't read zip.