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/folderPaths 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/folderShow files in the archive without extracting
tar -tzvf archive.tar.gzTo search inside the files without unpacking, see Search Inside Archives.
Extract all files to a specific location
tar -xzvf archive.tar.gz -C /folderThe folder must already exist (mkdir -p /folder).
Extract a specific file
tar -xzvf archive.tar.gz archivedFile.txtUse 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 ./aNewFolderExtract 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.gpgDecrypt and extract in one go:
gpg -d archive.tar.gz.gpg | tar -xzvf - -C /folderSee also
- tar — uncompressed tar, and the base flags.
- Compress and Extract Files — the task-level hub across tar, zip and 7zip.