How to Extract and Create tar, tar.gz, and tar.bz2 Archives on Linux

This guide shows you how to work with compressed archives from a Linux shell prompt: extracting .tar, .tar.gz (also written .tgz), .tar.bz2, .tar.xz and .zip files, creating your own archives, listing what is inside one without unpacking it, unpacking into a chosen folder, and pulling out just a single file. It is written for Noiz clients who have shell access over SSH, whether on a VPS or dedicated server with a full shell, or through a shell-user account on shared hosting. The aim is not to hand you commands to memorise, but to explain what each tar option actually means, so you can read any tar command and understand it, and build the one you need with confidence.

A quick note on terminology. A tar archive (often called a tarball) bundles many files and folders into one file. The name comes from "tape archive". On its own, tar does not compress anything; it just packs. The compression is a separate step layered on top, which is why you see the double extension: .tar.gz is a tar archive that has then been squeezed with gzip, .tar.bz2 uses bzip2, and .tar.xz uses xz. A .zip file is a different format entirely that does both jobs at once, and is handled by separate zip and unzip tools rather than by tar.

Last reviewed: 27 July 2026, against GNU tar (1.35 series) and the standard command-line archive tools shipped on Noiz Linux hosting. The tar options described here have been stable across GNU tar releases for many years; where a behaviour depends on the version you are running, this guide says so. It complements, and does not replace, the official GNU documentation linked below.

Official Documentation Reference

  • GNU tar manual: the complete, authoritative reference for every tar option and mode of operation, maintained by the GNU project.
  • tar(1) man page: the concise manual page for tar as installed on current Linux distributions, listing each flag and its meaning.
  • xz(1) man page: reference for the xz compressor used by .tar.xz and .xz files.
  • unzip(1) man page: reference for the Info-ZIP unzip tool used for .zip archives.

Prerequisites

  • Shell access to your Noiz server over SSH. On a VPS or dedicated server you have a full shell; on shared hosting a shell-user (SSH) account must be enabled for your site. If you are unsure whether shell access is available on your plan, open a support ticket to check.
  • A terminal or SSH client on your own computer, and you are logged in at a shell prompt on the server.
  • The name and location of the archive you want to work with. Change into the folder that contains it first, for example with cd, or refer to it by its full path.

Understanding the tar Flags (So You Never Have to Guess)

Almost every tar command you will ever see is built from a small set of single-letter options, usually clustered together after a single dash. Learn these seven and you can read and write any of them. The first three choose what tar does, and you pick exactly one of them:

  • -c, long form --create: create a new archive.
  • -x, long form --extract: extract files out of an archive.
  • -t, long form --list: list the contents of an archive without extracting anything.

The next three tell tar which compression the archive uses. You add one of these only when the archive is compressed:

  • -z, long form --gzip: the archive is gzip-compressed (.tar.gz or .tgz). Think "zip", as in gzip.
  • -j, long form --bzip2: the archive is bzip2-compressed (.tar.bz2).
  • -J, long form --xz: the archive is xz-compressed (.tar.xz). Note this is a capital J; lower-case j is bzip2 and upper-case J is xz, which is an easy one to mix up.

The last two are about the archive file itself and the output:

  • -f, long form --file: the very next thing on the command line is the archive file name. This option is not optional in normal use, and it must be the last letter in a cluster, because tar reads the word after it as the filename. That is why commands are written -xzvf archive.tar.gz and not -xzfv archive.tar.gz: in the second version tar would treat v as the filename.
  • -v, long form --verbose: verbose. Print each file name as it is processed. This is optional and only affects what you see on screen; leave it off for a quiet command, add it when you want to watch progress.

One more option earns its own mention because it is so useful:

  • -C DIR, long form --directory DIR: change into DIR before doing anything. On extraction this decides where files land; on creation it decides which folder tar packs from. The directory must already exist.

So a command like tar -xzvf backup.tar.gz reads, letter by letter, as "extract, from a gzip archive, verbosely, using the file backup.tar.gz". Once you see it that way, the rest of this guide is just rearranging the same handful of letters.

Extract an Archive

Match the compression flag to the file extension. In each example, replace archive with your real file name.

A plain .tar file (no compression)

tar -xvf archive.tar

No compression flag is needed because nothing was compressed.

A .tar.gz or .tgz file (gzip)

tar -xzvf archive.tar.gz

.tgz is simply a shorter spelling of .tar.gz and is extracted exactly the same way.

A .tar.bz2 file (bzip2)

tar -xjvf archive.tar.bz2

A .tar.xz file (xz)

tar -xJvf archive.tar.xz

Remember the capital J for xz.

Do you even need the compression flag?

Modern GNU tar, which is what runs on Noiz Linux servers, inspects the archive when it opens it and recognises the compression automatically on extraction and listing. In practice this means tar -xvf archive.tar.gz also works, without the -z. Two reasons to keep supplying the correct flag anyway: it makes the command self-documenting for anyone reading it later, and it is required on older or non-GNU versions of tar, and whenever you are creating an archive rather than reading one. When in doubt, include it.

A .zip file

tar does not handle the ZIP format; use unzip instead:

unzip archive.zip

This unpacks the archive into the current folder. If unzip is not installed, see the troubleshooting section below.

List What Is Inside Without Extracting

Swap the -x (extract) for -t (list) and keep everything else the same. Nothing is written to disk; tar just prints the contents. This is the safe way to look before you leap, especially with an archive from an unfamiliar source.

tar -tvf archive.tar.gz

Drop the -v for a plain list of names, or keep it to also see sizes, permissions and dates, much like ls -l. The same substitution works for every compression type: tar -tjvf archive.tar.bz2, tar -tJvf archive.tar.xz, and so on. For a ZIP file, list its contents with:

unzip -l archive.zip

Extract Into a Specific Folder

By default tar unpacks into whatever folder you are standing in. To send the contents somewhere else, add -C followed by the target folder. The folder must already exist, so create it first if needed:

mkdir -p /var/www/restore
tar -xzvf archive.tar.gz -C /var/www/restore

This is the clean way to unpack an archive without cluttering your current directory, and it is handy when restoring a site backup into a fresh location before moving it into place.

Extract a Single File or Folder

You do not have to unpack an entire archive to retrieve one item. Name the file after the archive, using its exact path as stored inside the archive. That last part matters: the path must match what tar recorded, which is why it pays to list the archive first and copy the path from there.

tar -tzvf archive.tar.gz
tar -xzvf archive.tar.gz wp-content/uploads/2026/logo.png

The first command shows you the exact stored paths; the second pulls out just that one file, recreating any leading folders it needs. To extract a whole sub-folder, name the folder instead of a single file, for example wp-content/uploads/.

Create Your Own Archive

Creating flips -x to -c. Here the order on the command line is: the options, then the name of the archive you want to produce, then the files or folders you want to pack into it. When creating, the compression flag is not optional: it is what tells tar to compress at all, and choosing it also decides which extension you should give the file.

Pack a folder into a gzip archive (.tar.gz)

tar -czvf mysite-backup.tar.gz public_html/

This reads as "create, gzip compressed, verbose, into the file mysite-backup.tar.gz, from the folder public_html/". gzip is the sensible default: it is fast and universally supported.

Other compression choices

tar -cjvf mysite-backup.tar.bz2 public_html/
tar -cJvf mysite-backup.tar.xz public_html/
tar -cvf  mysite-backup.tar public_html/

bzip2 (-j) and xz (-J) compress smaller than gzip but take longer, with xz usually giving the smallest file at the cost of the most time and memory. The last line, with no compression flag, produces an uncompressed .tar, which is quick and useful when the contents are already compressed, for example a folder of images or videos, where further compression would gain almost nothing.

Making a ZIP instead

If you need a .zip, for instance to send something to a Windows user, use the zip tool with -r so it includes sub-folders:

zip -r mysite-backup.zip public_html/

A Note on Single Gzipped Files: .gz and .sql.gz

A very common point of confusion, and one worth clearing up because Noiz database backups often arrive this way. A file ending in a bare .gz with no .tar before it, such as database.sql.gz, is a single file that has been gzipped on its own. It is not a tar archive, so tar is the wrong tool. Uncompress it with gunzip:

gunzip database.sql.gz

That replaces database.sql.gz with the uncompressed database.sql. To keep the compressed copy as well, decompress to a new file instead:

gunzip -k database.sql.gz

Or stream it straight into another command without ever writing the plain file to disk, which is a tidy way to import a dump:

zcat database.sql.gz | mysql -u dbuser -p dbname

In short: two extensions ending in .tar.gz means "many files, use tar"; a single .gz such as .sql.gz means "one file, use gunzip".

Troubleshooting

  • gzip: stdin: not in gzip format, often alongside tar: Child returned status 1 and tar: Error is not recoverable: exiting now: you told tar the file is gzip with -z, but it is not, or the download is incomplete or corrupt. First check what the file really is with file archive.ext, which reads the file's contents rather than trusting the name. If it reports a different compression, use the matching flag; if it looks truncated, download it again.
  • tar: Refusing to read archive contents from terminal (missing -f option?), or the write equivalent when creating: you left out -f, so tar had no archive to read from or write to. Add -f immediately followed by the archive name.
  • tar: archive.tar.gz: Cannot open: No such file or directory: the name or location is wrong. Confirm you are in the right folder and that the file exists with ls -l, watching for typos and the exact extension.
  • tar: /path/to/dir: Cannot open: No such file or directory when using -C: the target folder does not exist. Create it first with mkdir -p /path/to/dir, then run the extraction again.
  • tar: somefile: Not found in archive when extracting one file: the path did not match what is stored inside. List the archive with -t first and copy the path exactly as shown, including any leading folders.
  • tar: Removing leading '/' from member names: this is an informational message, not an error. tar strips the leading slash so the archive unpacks relative to the current folder rather than overwriting absolute system paths. It is a safety feature and nothing is wrong.
  • bzip2: command not found or xz: command not found: the compressor for that format is not installed. On a VPS or dedicated server where you hold root, install it (the packages are typically named bzip2 and xz-utils). On shared hosting, open a support ticket and Noiz will assist.
  • unzip: command not found: the ZIP tools are not present. Install unzip (and zip for creating) on a server you control, or raise a ticket on shared hosting.
  • Cannot open: Permission denied while extracting: you are unpacking into a folder your user cannot write to. Extract into a location you own, such as your home directory or your site's document root, then move the files into place.

If an archive will not open, or a backup does not extract the way you expect, open a support ticket with the Noiz support team. Include the exact command you ran, the full error message, and the output of file yourarchive and ls -l yourarchive so the team can see the real format and size straight away.

  • 0 Users Found This Useful
  • backup, ssh, linux
Was this answer helpful?

Related Articles

How to Find the UUID of a Disk or Partition on Linux

This guide shows you how to find the UUID of a disk or partition on a Linux server, and, just as...