If you’re working with Linux systems, you’ll frequently encounter .tar.gz
files. These are compressed archive files commonly used for packaging software, backups, and configuration bundles. In this guide, we’ll explain what a .tar.gz
file is, how to extract it, and how to create one safely on Linux systems like Debian or Ubuntu.
What is a .tar.gz
File?
A .tar.gz
file is a combination of two formats:
.tar
: Stands for Tape Archive. It combines multiple files and directories into one file, but does not compress them..gz
: Stands for Gzip. It compresses the.tar
file to save space.
This results in a compressed archive: filename.tar.gz
. You may also see .tgz
, which is a shorthand alias for .tar.gz
.
How to Extract a .tar.gz
File
You can extract a .tar.gz
file using the tar
command, which is pre-installed on most Linux distributions.
0 1 2 3 |
# Basic extraction $ tar -xzvf archive.tar.gz |
Explanation:
-x
: Extract-z
: Use gzip to decompress-v
: Verbose, shows files being extracted (optional)-f
: Filename of the archive
Extract to a Specific Directory
If you want to extract files into a particular folder:
0 1 2 |
$ tar -xzvf archive.tar.gz -C /path/to/destination/ |
Extract and Change Top-Level Folder Name
Sometimes, you may want to extract the contents but rename the top-level directory in the archive. One way to do this is:
0 1 2 3 4 5 6 |
# Step 1: Create a target directory $ mkdir new-folder # Step 2: Extract into it $ tar -xzvf archive.tar.gz -C new-folder/ |
Alternatively, to restructure or rename after extraction:
0 1 2 3 |
$ tar -xzvf archive.tar.gz $ mv old-folder-name new-folder-name |
Use --transform
to Rename Folder During Extraction
If you want to rename the top-level folder directly during extraction (e.g., from example.com.v2
to example.com
):
0 1 2 |
$ tar --transform='s/^example\.com\.v2/example.com/' -xzvf archive.tar.gz |
Make sure to match the actual top-level folder name inside the archive. Use
tar -tzf archive.tar.gz
to preview the folder structure before extracting.
⚠️ Permission Denied or Directory Errors?
If you see errors like
Cannot mkdir: Permission denied
orCannot open: No such file or directory
, it usually means:
- You don’t have write permission to the destination directory
- You’re trying to extract into a system-owned path like
/var/www
withoutsudo
Solution:
012 sudo tar -xzvf archive.tar.gz -C /desired/path/Or extract it in your home directory:
012 tar -xzvf archive.tar.gz -C ~/myfolder/
How to Create a .tar.gz
File
To create a .tar.gz
file (compress a folder or multiple files):
0 1 2 |
$ tar -czvf archive.tar.gz folder_or_files |
Explanation:
-c
: Create archive-z
: Compress with gzip-v
: Verbose (optional)-f
: Output filename
Example: Compress a folder
0 1 2 |
$ tar -czvf backup.tar.gz myproject/ |
Example: Compress multiple files
0 1 2 |
$ tar -czvf images.tar.gz photo1.jpg photo2.jpg photo3.jpg |
📦 Note: Compressing already-compressed files like
.jpg
,.mp4
, or.zip
often yields minimal size reduction. Expect 0–5% savings at best.
Rename Parent Directory When Compressing
If you want the archive to extract into a renamed top-level folder:
0 1 2 3 4 |
$ cp -r original-folder temp-folder-name $ tar -czvf renamed-folder.tar.gz temp-folder-name $ rm -rf temp-folder-name |
This way, you control the name users will see when they extract the archive.
Alternative Methods
Step-by-Step: gunzip
+ tar
You can also extract the archive in two steps:
0 1 2 3 |
$ gunzip archive.tar.gz # Results in archive.tar $ tar -xvf archive.tar # Extract the tar archive |
Using 7-Zip
If you’re working in a GUI environment or using 7-Zip from the terminal:
0 1 2 3 |
$ 7z x archive.tar.gz # Step 1: Extracts .tar $ 7z x archive.tar # Step 2: Extracts contents |
Note: Install 7-Zip on Debian/Ubuntu via:
sudo apt install p7zip-full
Conclusion
The .tar.gz
format is widely used in Linux environments because it bundles and compresses files efficiently. Extracting and creating these files is straightforward using the built-in tar
tool, or you can use 7z
for more flexibility.
If you run into permission issues, always check your destination path and consider using sudo
or extracting to a user-owned directory. You can also change the folder structure or top-level folder name during or after extraction and even while compressing.