tar vs. gzip: how compression and archiving work together on linux
understanding tar and gzip on linux
when working with files on linux, you will often see names such as project.tar, project.gz, and project.tar.gz. these formats are common in devops, software deployment, backups, source-code distribution, and server administration.
although people often say they are “zipping” files, tar and gzip do different jobs:
- tar collects multiple files and directories into one archive file.
- gzip compresses a file to reduce its size.
used together, tar and gzip make it easy to package an entire project directory into one smaller file that can be copied, uploaded, backed up, or deployed.
what is tar?
tar stands for tape archive. it was originally designed to write collections of files to magnetic tape, but today it is widely used for creating archive files on linux and unix-like systems.
a tar archive combines many files and directories into a single file while preserving important filesystem information, including:
- directory structure
- file names
- permissions
- ownership information
- timestamps
- symbolic links
by itself, tar does not usually compress files. a tar archive may be convenient because it contains many files in one package, but its size can be close to the total size of the original files.
creating a tar archive
suppose you have a directory named my-app. you can create an archive called my-app.tar with this command:
tar -cvf my-app.tar my-app/
the options mean:
c— create a new archivev— verbose mode; display files as tar processes themf— use the archive file name that follows
after running the command, you will have one file:
my-app.tar
this archive contains the entire my-app directory, but it is not compressed yet.
viewing files inside a tar archive
you can inspect an archive without extracting it:
tar -tvf my-app.tar
here, t means list. this is useful when checking a backup or investigating the contents of a package before extracting it.
extracting a tar archive
to extract the archive in the current directory, use:
tar -xvf my-app.tar
the x option means extract.
to extract it into a specific destination directory, use -c:
tar -xvf my-app.tar -c /tmp/restore
make sure the destination directory already exists:
mkdir -p /tmp/restore
tar -xvf my-app.tar -c /tmp/restore
what is gzip?
gzip is a compression utility. its purpose is to make a file smaller by encoding repeated patterns more efficiently. it is commonly available on linux distributions and is a standard tool for developers, system administrators, and full stack teams.
unlike tar, gzip is designed to compress one file at a time. for example:
gzip server.log
this command replaces server.log with a compressed file named:
server.log.gz
to decompress the file, use:
gunzip server.log.gz
you can also use gzip with the -d option:
gzip -d server.log.gz
keeping the original file with gzip
by default, gzip removes the original file after compression. if you want to keep it, add the -k option:
gzip -k server.log
after this command, both files remain available:
server.logserver.log.gz
why tar and gzip are often used together
gzip compresses a single file, while tar can bundle a directory tree and many files into one file. therefore, the common workflow is:
- use tar to archive files and directories.
- use gzip to compress the resulting tar archive.
the result is usually a file ending in .tar.gz or .tgz.
for example:
my-app.tar.gz
this file is both:
- an archive containing multiple files and folders
- a compressed file that uses less storage and transfer bandwidth
this combination is especially useful for sending source code, creating database backups, publishing open-source releases, and moving files between servers in a devops workflow.
create a .tar.gz archive
you can create a compressed tar archive in one command:
tar -czvf my-app.tar.gz my-app/
the additional option is:
z— compress the archive with gzip
the complete option breakdown is:
c— create an archivez— use gzip compressionv— show processed filesf— specify the output file name
if you do not need to see every file being processed, remove v:
tar -czf my-app.tar.gz my-app/
this shorter version is commonly used in automation scripts and ci/cd pipelines.
example: backing up a web application
imagine your application lives in /var/www/storefront. you can create a compressed backup like this:
tar -czf storefront-backup.tar.gz /var/www/storefront
however, this command stores the full path inside the archive. a cleaner approach is to change to the parent directory first:
cd /var/www
tar -czf storefront-backup.tar.gz storefront/
now the archive contains the relative directory path storefront/, which is usually easier and safer to extract later.
extract a .tar.gz archive
to extract a gzip-compressed tar archive, use:
tar -xzvf my-app.tar.gz
the options mean:
x— extract filesz— decompress with gzipv— display file namesf— use the specified archive file
to extract into another directory:
mkdir -p ~/projects
tar -xzvf my-app.tar.gz -c ~/projects
this is helpful when downloading source code packages, such as open-source projects from a release page.
list files without extracting
before extracting an unknown archive, inspect its contents:
tar -tzf my-app.tar.gz
this is a good security habit. it lets you confirm the archive structure and avoid accidentally extracting unexpected files into an important directory.
tar vs. gzip: key differences
| feature | tar | gzip |
|---|---|---|
| main purpose | archive files and directories | compress a file |
| can handle directories directly? | yes | no |
| combines multiple files? | yes | no |
| reduces file size? | usually no | yes |
| common extension | .tar |
.gz |
| common combined extension | .tar.gz or .tgz |
common file extensions explained
file extensions provide a useful clue about how a file was packaged.
.tar— archive only; no gzip compression.gz— one gzip-compressed file.tar.gz— tar archive compressed with gzip.tgz— shorter name for.tar.gz.tar.bz2— tar archive compressed with bzip2.tar.xz— tar archive compressed with xz
do not rely only on the extension, especially when working with files from unknown sources. you can identify a file type with the file command:
file my-app.tar.gz
useful tar command cheat sheet
create archives
# create an uncompressed tar archive
tar -cvf archive.tar directory/
# create a gzip-compressed tar archive
tar -czvf archive.tar.gz directory/
# create an archive without verbose output
tar -czf archive.tar.gz directory/
extract archives
# extract a .tar file
tar -xvf archive.tar
# extract a .tar.gz file
tar -xzvf archive.tar.gz
# extract into a target directory
tar -xzvf archive.tar.gz -c /path/to/destination
inspect archives
# list files in a tar archive
tar -tvf archive.tar
# list files in a gzip-compressed tar archive
tar -tzvf archive.tar.gz
extract one specific file
if you only need one file from a large archive, specify its path:
tar -xzvf archive.tar.gz path/inside/archive/config.json
first list the archive contents if you are unsure of the exact path:
tar -tzf archive.tar.gz
using tar and gzip in devops workflows
tar and gzip remain important tools in modern devops environments. even when teams use docker, cloud storage, kubernetes, and automated deployment platforms, archives are still useful for moving and storing files efficiently.
application release packages
a build server can package a compiled application before deployment:
tar -czf release-1.4.0.tar.gz dist/ config/ scripts/
this creates one portable deployment artifact containing the application build, configuration files, and deployment scripts.
log archival
logs can grow quickly on busy linux servers. compressing old logs saves disk space:
gzip -k /var/log/nginx/access.log
for several related log files, tar can archive them together:
tar -czf nginx-logs-2025-01.tar.gz /var/log/nginx/
database backup packaging
a database dump is often a single text file, so gzip alone may be enough:
pg_dump my_database | gzip > my_database.sql.gz
to restore it later, decompress the data and send it to postgresql:
gunzip -c my_database.sql.gz | psql my_database
the -c option writes decompressed output to standard output instead of creating a separate .sql file first.
important security and safety tips
archives are convenient, but you should handle them carefully, particularly when they come from the internet or another user.
- inspect before extracting: use
tar -tzf archive.tar.gzto see its contents. - extract into a temporary directory: avoid unpacking unknown archives directly into system directories.
- be careful with permissions: archives can preserve executable permissions and ownership metadata.
- avoid using sudo unnecessarily: extract as a normal user unless administrative access is truly required.
- verify downloads: when available, compare checksums such as sha-256 values.
to calculate a sha-256 checksum on linux:
sha256sum my-app.tar.gz
if the published checksum matches your result, you have stronger evidence that the downloaded file has not been corrupted or modified.
common beginner mistakes
trying to gzip an entire directory directly
this command does not work as expected because gzip does not archive directories:
gzip my-app/
instead, use tar and gzip together:
tar -czf my-app.tar.gz my-app/
forgetting the f option
when using f, the next value must be the archive name:
tar -czf backup.tar.gz project/
if the file name is placed incorrectly, tar may interpret it as a directory or file to archive.
extracting into the wrong directory
archives extract into the current directory unless you specify another location with -c. check your current directory first:
pwd
tar -xzvf archive.tar.gz
or explicitly choose a destination:
tar -xzvf archive.tar.gz -c ~/downloads/extracted
expecting compression to shrink every file significantly
text files, source code, json, csv, and log files often compress well. files that are already compressed, such as jpeg images, mp4 videos, zip archives, and many pdf files, may not become much smaller.
for example, compressing a directory full of images may produce only a small size reduction:
tar -czf photos.tar.gz photos/
even so, tar is still useful because it packages all files into one archive.
alternative compression methods
gzip is popular because it is fast, widely supported, and simple. linux also supports other compression tools that can work with tar.
- bzip2 — often produces smaller archives than gzip, but can be slower.
- xz — can achieve very high compression, but may use more cpu time.
- zstd — modern compression with a strong balance between speed and compression ratio.
examples:
# create a bzip2-compressed tar archive
tar -cjf archive.tar.bz2 directory/
# create an xz-compressed tar archive
tar -cjf archive.tar.xz directory/
# create a zstd-compressed tar archive
tar --zstd -cf archive.tar.zst directory/
for beginners, .tar.gz is an excellent default choice because it works almost everywhere and is easy to recognize.
summary
the easiest way to remember the difference is:
- tar bundles files together.
- gzip makes a file smaller.
- tar.gz does both.
for everyday linux coding, backups, full stack deployments, and devops automation, the command below is one of the most useful to know:
tar -czf archive.tar.gz directory/
and to extract it:
tar -xzf archive.tar.gz
once you understand these commands, you can confidently work with source-code downloads, server backups, deployment artifacts, and linux package archives.
Comments
Share your thoughts and join the conversation
Loading comments...
Please log in to share your thoughts and engage with the community.