Quick Reference
# Archive and gzip-compress a directory
tar -czvf archive_name.tar.gz /path/to/directory
# Extract into the current directory
tar -xzvf archive.tar.gz
# List contents without extracting
tar -tzvf archive.tar.gz
Use .tar.gz for the conventional tar + gzip format on Linux. All paths below are generic examples. Run creation commands from outside the directory being archived, and choose a fresh output filename: creating a tar archive can overwrite an existing archive.
Jump to: Create · Flags · Extract · List · Destination · Formats · tar.zip · Examples · Cheat sheet.
Create a .tar.gz
tar -czvf myfolder.tar.gz myfolder/
Creates myfolder.tar.gz containing myfolder/ and its contents, including subdirectories and hidden files. The original directory remains in place. Extraction recreates the myfolder/ directory beneath the extraction destination.
With an absolute source path, GNU tar normally strips the leading / and may print “Removing leading / from member names.” For /path/to/directory, stored names begin with path/to/directory/. To keep only the directory name, use -C:
tar -czvf archive_name.tar.gz -C /path/to directory/
Here -C changes the source lookup directory before processing directory/. The output archive is created in the shell’s current directory. See the GNU tar manual for path handling and option details.
Understanding the tar Flags
| Flag | Meaning |
|---|---|
c | Create a new archive. |
z | Use gzip compression or decompression. |
v | Verbose: show files being processed. |
f | Use the following argument as the archive filename. |
x | Extract files. |
t | List archive contents. |
-C | Change directory before processing subsequent files or extracting. |
Choose one operation: c, x, or t. In these combined forms, f is last and the archive filename follows immediately. Case matters: c creates an archive; -C selects a directory.
v is optional. Omit it for large directory trees or scripts to avoid excessive terminal output; errors are still reported.
tar -czf archive.tar.gz directory/
Extract a .tar.gz
tar -xzvf archive.tar.gz
Extracts stored paths beneath the current directory. The archive remains unchanged. Inspect unfamiliar archives first and extract into a new, empty directory as an unprivileged user; extraction can overwrite existing files with matching paths.
List Files Without Extracting
tar -tzvf archive.tar.gz
Shows members and details such as permissions and sizes, without writing their contents to disk. For filenames only:
tar -tzf archive.tar.gz
Extract to a Specific Directory
mkdir -p /destination/path
tar -xzvf archive.tar.gz -C /destination/path
The destination must exist and be writable. Replace the generic path with your chosen directory. -C does not remove stored parent directories: an archive containing myfolder/file.txt produces /destination/path/myfolder/file.txt.
.tar vs .tar.gz vs .zip
| Extension | What it contains | Typical use |
|---|---|---|
.tar | Files and directories bundled into one uncompressed archive. | Archive now; compress separately if needed. |
.tar.gz | A tar archive compressed as a gzip stream. | Conventional tar + gzip format on Linux. |
.zip | A ZIP archive, usually with each file compressed separately. | Exchange files with ZIP-compatible tools. |
.tar.zip | A ZIP archive containing a tar file. | Unusual; use only when explicitly required. |
tar is primarily an archiving tool; gzip is a compression tool. The z option connects them in one command. gzip alone does not bundle a directory tree into one archive. ZIP combines archiving with compression; see the Info-ZIP overview.
directory/files
|
v
tar
|
v
archive.tar
|
v
gzip
|
v
archive.tar.gz
Changing an extension does not convert the format. Naming gzip output .tar.zip would be misleading; gzip and ZIP are different formats.
Creating .tar.zip When Explicitly Required
tar -cvf myfolder.tar myfolder/
zip myfolder.tar.zip myfolder.tar
rm myfolder.tar
Run these steps individually and check success before continuing: first bundle the directory into an uncompressed tar file, then place that file inside a ZIP archive, then remove only the intermediate tar file. The original directory remains. Use fresh archive names; zip can update an existing archive and retain older entries.
For a copyable sequence that tests the ZIP before removing the intermediate file:
tar -cvf myfolder.tar myfolder/ &&
zip myfolder.tar.zip myfolder.tar &&
unzip -t myfolder.tar.zip &&
rm myfolder.tar
The && operators stop the sequence if a command fails. Unpacking requires two steps:
unzip myfolder.tar.zip
tar -xvf myfolder.tar
Normally choose .tar.gz or create .zip directly instead; the extra tar layer is unnecessary unless a consumer explicitly requires it.
Common Examples
Uncompressed tar
# Create
tar -cvf archive.tar directory/
# Extract
tar -xvf archive.tar
Compress an existing tar file
# Creates archive.tar.gz; removes archive.tar on success
gzip archive.tar
# Restores archive.tar; removes archive.tar.gz on success
gunzip archive.tar.gz
To keep the input file, write gzip’s output to a new filename instead:
gzip -c archive.tar > archive.tar.gz
Archive only the contents, or quote a path with spaces
# Store contents under ./ instead of directory/
tar -czf archive.tar.gz -C directory .
# Quote paths that contain spaces
tar -czf archive.tar.gz "my folder/"
These are alternative commands, not a sequence: each creates the output archive. Passing the directory itself includes hidden files; a shell wildcard such as directory/* usually omits hidden entries.
Create a regular ZIP archive
# -r includes subdirectories recursively
zip -r myfolder.zip myfolder/
# List without extracting
unzip -l myfolder.zip
# Extract
unzip myfolder.zip
Check compressed data
gzip -t archive.tar.gz
unzip -t myfolder.zip
A successful integrity test checks compressed data, not backup completeness or application consistency. Avoid archiving actively changing data when a consistent snapshot is required.
Cheat Sheet
| Task | Command |
|---|---|
| Create .tar.gz | tar -czvf archive.tar.gz directory/ |
| Create without verbose output | tar -czf archive.tar.gz directory/ |
| Extract .tar.gz | tar -xzvf archive.tar.gz |
| Extract to an existing directory | tar -xzvf archive.tar.gz -C /destination/path |
| List .tar.gz | tar -tzvf archive.tar.gz |
| Create .tar | tar -cvf archive.tar directory/ |
| Extract .tar | tar -xvf archive.tar |
| Compress an existing tar | gzip archive.tar |
| Create .zip | zip -r archive.zip directory/ |
| List .zip | unzip -l archive.zip |
| Extract .zip | unzip archive.zip |
| Check gzip integrity | gzip -t archive.tar.gz |