tar gzip in Linux: Archive & Extract Files

How to Archive, Compress & Extract Files with tar and gzip in Linux
The easiest way to make a compressed archive in Linux is a one-line command: tar -czvf archive.tar.gz folder/. Extraction is done using tar -xzvf archive.tar.gz. That tar gzip combo is the heart of backups, deployment and file transfers on virtually every Linux server.
This article is for everyone who uses any kind of Linux server, VPS or workstation and wants to bundle files for a backup, a deployment or a file transfer. You will learn what tar and gzip do on their own, how gzip and tar are used together in order to create and extract a gzipped tar archive (commonly known as gzip tarball, or tarball), how to extract into a particular directory, how to inspect the content of an archive without unpacking it and how to compare gzip, gunzip and bzip2 when you don't need to bundle files and use tar.
This article is for everyone who uses any kind of Linux server, VPS or workstation and wants to bundle files for a backup, a deployment or a file transfer. You will learn what tar and gzip do on their own, how gzip and tar are used together in order to create and extract a gzipped tar archive (commonly known as gzip tarball, or tarball), how to extract into a particular directory, how to inspect the content of an archive without unpacking it and how to compare gzip, gunzip and bzip2 when you don't need to bundle files and use tar.
Short answer: how to create and extract a tar.gz in two commands
To create a gzip-compressed tar archive:
bash
tar -czvf archive.tar.gz folder/To extract a tar.gz back to files and directories:
bash
tar -xzvf archive.tar.gzc creates a new archive, x extracts from it, z feeds it through gzip, v shows the list of processed files and f sets the archive name. This is how you should go if you are in a hurry. Keep reading to know what each option does, how to extract it to a directory, how gzip works and how to compare it with bzip2 when you need to compress files but not to bundle them with tar.
Difference between tar and gzip
tar is a utility to bundle many files and directories into a single file (called tarball) without shrinking them at all. gzip reduces the size of a single file but doesn't understand how to process directories or several files.
This is why tar and gzip are almost always used in tandem: tar solves the problem "how to pack many files into one" while gzip solves the problem "how to compress a file". When we say gzip tarball or gzipped tar we mean an archive created using tar and then compressed with gzip (using tar -cvf archive.tar folder/ followed by gzip archive.tar) or gzip integrated into tar using -z switch in one line command. The result is a .tar.gz archive, often shortened to .tgz.
Other popular compressors include bzip2 (.tar.bz2, compresses better but runs slower) and xz (.tar.xz, compresses best but runs slowest). Gzip is the default compressor most of the people use because it is fast and available on every machine.
How to create a tar.gz file in Linux
Use tar -czvf archive.tar.gz folder/ to create a gzip-compressed tarball in one line command. All this magic is performed by four switches: -c creates a new archive, -z compresses the archive, -v prints the names of processed files and -f sets the name of the file.
bash
tar -czvf archive.tar.gz folder/Result:
bash
folder/
folder/config.yml
folder/logs/
folder/logs/app.logIf you just need to create an archive without compression, remove the z part:
bash
tar -cvf archive.tar folder/-f has to be immediately followed by an archive name and the archive name should go before the files you want to put there. Incorrect order of parameters is the most popular mistake while trying to perform a tar command for the first time.
A note from experience: [YOUR EXPERIENCE HERE - the skill will not invent this]
The tar and gzip command shown above is the one that will be used in the great majority of backups and file transfers. In case if you archive with a leading / character (for instance, tar -czvf archive.tar.gz /var/www/app), GNU tar removes the leading / from file names and prints a warning ("Removing leading / from member names"). It is normal behavior, meaning that the archive will be extracted relatively to your current directory, not to the original /var/www/app.
How to extract a tar.gz file (tar gunzip) in Linux
Use tar -xzvf archive.tar.gz to extract gzip-compressed tarball in one command. Instead of -c now there is -x that means to extract an archive, -z means to pipe the archive through gzip, -v means to print the list of files being extracted and -f means to set the archive name.
bash
tar -xzvf archive.tar.gzModern GNU tar (the version that is included into almost every current Linux distribution) detects whether the archive is compressed with gzip automatically. That is why tar -xvf archive.tar.gz without -z will work just fine. But there are some good reasons to leave -z in your commands anyway: it will make the intention clear for whoever is reading them and it will avoid unexpected results on older systems or minimal images that ship a stripped-down tar.
Note for people that often make this mistake: the unzip command does not extract .tar.gz files. unzip recognizes only .zip files. If you try to search for the way to extract .tar.gz with unzip into google, what you actually need is tar -xzvf.
How to extract a tar.gz file to a particular directory?
Add -C flag followed by the directory path: tar -xzvf archive.tar.gz -C /path/to/dir. The directory has to exist first, so you need to create it using mkdir -p /path/to/dir before extracting.
bash
mkdir -p /path/to/dir
tar -xzvf archive.tar.gz -C /path/to/dirIt is the answer for "how to untar to a directory" or "how to extract tar.gz to a directory": the key is always -C, no matter how the question is formulated. Without -C flag, tar extracts everything relative to the directory you are in, which is rarely what you want to do if you try to extract something for a deployment or downloaded package to the server.
How to see the contents of a tar.gz file without extracting it?
Use tar -tzvf archive.tar.gz to see all the files stored in the gzip-compressed archive without actually extracting it. Remove -z in case of a plain tar archive.
bash
tar -tzvf archive.tar.gzResult:
bash
drwxr-xr-x user/user 0 2026-08-14 10:02 folder/
-rw-r--r-- user/user 812 2026-08-14 10:02 folder/config.yml
drwxr-xr-x user/user 0 2026-08-14 10:03 folder/logs/
-rw-r--r-- user/user 4021 2026-08-14 10:03 folder/logs/app.logIt is the safest way to inspect the archive which you haven't created yourself before extracting it to the production server. Use pipe and grep to find a particular file without looking through all the files:
bash
tar -tzvf archive.tar.gz | grep configHow to compress files with gzip, gunzip and bzip2?
Use gzip filename in order to compress a single file in-place, replacing it with filename.gz. In order to restore the original file, use gunzip filename.gz, or gzip -d filename.gz.
bash
gzip filename
gunzip filename.gzBy default gzip will delete the original file once the .gz version is written. To preserve both of them, use -k parameter:
bash
gzip -k filenameGzip and gunzip can be applied only to one file at a time. That is exactly why tar exists to bundle files first. In case if you already have a .tar file and want to gzip the tar file without creating it from scratch, just run gzip archive.tar. It will become archive.tar.gz in place.
bash
gzip archive.tarThere is one more popular alternative: bzip2 filename to compress and bunzip2 filename.bz2 or bzip2 -d filename.bz2 to uncompress. It provides smaller files at the price of additional runtime but takes noticeably longer than gzip. To linux tar gz decompress or tar bzip2 decompress directly through tar, rather than to perform these operations in two steps, use corresponding parameter -j or -J.
bash
tar -cjvf archive.tar.bz2 folder/
tar -xjvf archive.tar.bz2As the general rule of thumb: use gzip (-z) if speed is the priority and you don't mind few extra megabytes of space and bzip2 (-j) or xz (-J) when you are archiving something once and storage or transfer size matters more than the time it takes to compress.
Popular mistakes with tar and gzip
Switch -c instead of -x or vice versa. It is easy to mess up two flags that differ only by one letter when you are in a rush. Double-check the command before you hit Enter, especially in a script.
Forget that -C needs an existing target directory. tar -xzvf archive.tar.gz -C /path/to/dir won't work if /path/to/dir doesn't exist. Use mkdir -p to create it beforehand.
Try to use unzip on a .tar.gz file. unzip is designed for .zip archives only. Use tar -xzvf instead for .tar.gz and .tgz.
Think that tar and gzip can encrypt archives somehow. Neither of them does that, so a .tar.gz file is just as readable as the files inside it once someone has it. In order to encrypt your archive, use another tool, for instance gpg.
Pack files using absolute paths and expect that the extraction will happen in the same place. GNU tar removes leading / characters by default and notifies about that. Perform the extraction in the desired directory or use -C to be precise.
FAQ
What is the difference between a .tar file and a .tar.gz file?
A .tar file is a bundle of files and directories which is not compressed at all. A .tar.gz file is a bundle that is additionally compressed with gzip, so it takes up less space but needs decompressing before tar can read the contents.
Can I gzip a tar file after I've created it?
Yes. Use gzip archive.tar and it will be converted to archive.tar.gz in place without recreating it.
How do I extract a single file from a tar.gz archive?
Put the filename after the archive name: tar -xzvf archive.tar.gz path/to/file.txt. List the archive's contents with tar -tzvf archive.tar.gz if you are not sure about the exact path.
Is the archive encrypted by tar or gzip?
No, both only handle bundling and compression. Use a special tool for encryption, like gpg or openssl, if the archive needs to be encrypted, not just compressed.
What is the difference between gzip and bzip2?
Gzip is faster and the default most people use for everyday archives, but bzip2 compresses better and is used for larger archives that are supposed to be stored or transferred once, at the cost of taking longer to run.
Conclusion
tar -czvf to create, tar -xzvf to extract, -C to define the destination directory and -tzvf to preview the content: that covers the vast majority of tar gzip use on a Linux server. Just replace -z with -j in case you need bzip2 or use gzip/gunzip commands for a single file compression when you do not need tar at all.
Try Orbit Servers
Low-latency VPS, Bare Metal Servers and Colocation across the US, EU and APAC, provisioned instantly and built for performance-critical workloads.
Related products
tar and gzip cheat sheet
Table
Task | Command |
|---|---|
Create a tar archive (no compression) |
|
Create a gzip-compressed tar.gz |
|
Extract a plain tar archive |
|
Extract a tar.gz archive |
|
Extract into a specific directory |
|
List contents without extracting |
|
Extract a single file from an archive |
|
Compress a single file with gzip |
|
Compress but keep the original file |
|
Decompress a .gz file |
|
Gzip an existing tar file |
|
Create a bzip2-compressed tar.bz2 |
|
Extract a tar.bz2 archive |
|
Create an xz-compressed tar.xz |
|
Get started with Orbit Servers
Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.
Get startedRelated products
Written by
Julius