tar.gz

A gzip-compressed tarball (.tar.gz, also written .tgz). It’s plain tar with -z added, so every flag on that page still applies. The same trick works for other compressors: swap -z for -j (bzip2, .tar.bz2), -J (xz, .tar.xz), or --zstd (.tar.zst).

Compress all files in current folder to archive.tar.gz

tar -czvf archive.tar.gz *

Compress all files in current folder, including subdirectories and dotfiles

tar -czvf archive.tar.gz .

Compress a target folder to archive.tar.gz

tar -czvf archive.tar.gz /path/to/folder

Paths are stored as given. To store them relative to the folder instead, so it unpacks cleanly anywhere, change directory inside the command:

tar -czvf archive.tar.gz -C /path/to/folder .

Leave something out

tar -czvf archive.tar.gz --exclude='*.log' --exclude='.git' /path/to/folder

Show files in the archive without extracting

tar -tzvf archive.tar.gz

To search inside the files without unpacking, see Search Inside Archives.

Extract all files to a specific location

tar -xzvf archive.tar.gz -C /folder

The folder must already exist (mkdir -p /folder).

Extract a specific file

tar -xzvf archive.tar.gz archivedFile.txt

Use the exact path shown by tar -tzvf.

Extract a specific file to a new folder

mkdir -p ./aNewFolder
tar -xzvf archive.tar.gz archivedFile.txt -C ./aNewFolder

Extract without remembering the compression type

tar -xaf archive.tar.xz

-a picks the decompressor from the file extension on create and extract. GNU tar only; newer versions also auto-detect on plain -xf.

Compress with a password

tar has no native encryption. Pipe through gpg for symmetric (passphrase) encryption:

tar -czvf - /path/to/folder | gpg -c -o archive.tar.gz.gpg

Decrypt and extract in one go:

gpg -d archive.tar.gz.gpg | tar -xzvf - -C /folder

See also