A .tar file and a .tar.gz file are not quite the same thing: tar bundles files and directories into an archive, while gzip compresses data. The tar command can do both, which is why a few flags cover most everyday archive tasks.
Here’s a practical workflow: create an archive, check what’s inside, then extract it somewhere safe.
The three operations to remember
The main tar operation flags are:
-
-ccreates an archive. -
-tlists its contents without extracting. -
-xextracts its contents. -
-fspecifies the archive filename.
For example, to create a plain archive, list it, and extract it:
tar -cf project.tar project/
tar -tf project.tar
tar -xf project.tar
The -f flag takes a filename, so put the archive name right after the grouped options. In tar -cf project.tar project/, project.tar is the archive being created and project/ is what goes into it.
Add compression when you need it
A plain .tar combines files but does not compress them. Add a compression flag when you want tar to use a compressor:
# gzip
tar -czf project.tar.gz project/
# bzip2
tar -cjf project.tar.bz2 project/
# xz
tar -cJf project.tar.xz project/
The matching extraction commands are:
tar -xzf project.tar.gz
tar -xjf project.tar.bz2
tar -xJf project.tar.xz
-z selects gzip, -j selects bzip2, and uppercase -J selects xz. The extensions are conventional names for these formats; .tgz is also commonly used for a gzip-compressed tar archive.
Compression results depend on the contents. Files that are already compressed, such as many images and videos, may not get much smaller.
Choose what goes into the archive
You can pass tar individual files or a directory:
tar -czf notes.tar.gz notes.txt report.pdf
tar -czf project.tar.gz project/
Tar stores paths based on how you provide them. If you want shorter, predictable paths inside the archive, use -C to change to a parent directory before adding the files:
tar -czf project.tar.gz -C /home/alex project/
Here, tar changes to /home/alex before reading project/, so the archive contains paths relative to that location. The order matters: -C affects paths that follow it. If you’re assembling an archive from files scattered across a directory tree, find can help you locate the paths you want to include.
Inspect an archive before extracting
Use -t to see the member names without writing files to disk:
tar -tf backup.tar.gz
For a more detailed listing, add -v:
tar -tvf backup.tar.gz
Listing first is especially useful for archives you didn’t create. It shows whether the archive contains a top-level directory, which can explain why files end up nested after extraction. It also gives you the exact member path if you want to extract just one file.
For example, if the listing includes project/config/settings.yml, extract only that file with:
tar -xf project.tar project/config/settings.yml
Use the member name as it appears in the archive, not the path you expect it to have on your machine.
Extract to a dedicated directory
Without -C, tar extracts relative to your current working directory. You can choose another destination with -C, but the directory must already exist:
mkdir -p /tmp/restore
tar -xzf backup.tar.gz -C /tmp/restore
A good habit is to create a separate destination for unfamiliar archives rather than extracting them into a directory full of important files. Extraction can create files and may replace existing files at the destination. Check the archive listing first, then choose a destination you can write to.
Tar archives are different from ZIP files. If you have a .zip archive, use a ZIP-capable tool instead; this guide to extracting ZIP files on Linux covers the unzip command.
A compact reference
# Create an uncompressed archive
tar -cf files.tar file1.txt file2.txt
# Create a gzip-compressed archive
tar -czf backup.tar.gz project/
# List members
tar -tf backup.tar.gz
# Extract gzip-compressed files
tar -xzf backup.tar.gz
# Extract into an existing directory
tar -xzf backup.tar.gz -C /tmp/restore
If tar reports that an archive or input file cannot be opened, check the path and your current directory. If it says a member wasn’t found, list the archive and use the exact displayed path. And if a compression option is rejected, check tar --help or the local manual: GNU tar is common on Linux, but minimal environments and other implementations may support different options.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)