The tar Commands You Will Use Most
tar -tzf archive.tar.gz
tar -xzf archive.tar.gz
tar -xzf archive.tar.gz -C /opt/app
tar -xf archive.tar.xz
The first command lists what is inside without extracting anything. The second extracts into the current folder, the third into /opt/app, and the fourth extracts an archive compressed with xz. The letters mean: x extract, t list, z gzip, and f the archive file, whose name must follow it. Add v to print each name as tar handles it: tar -xvzf.
List the Contents First
tar -tzf archive.tar.gz | head
tar -tvzf archive.tar.gz
Read the first lines. A well-made archive has one top folder, such as app-2.4/, with everything inside it. If the names start without a folder, extracting in your current directory scatters the files among the ones already there. -tv also shows owners, permissions, and sizes.
Archives from Unknown Sources
An archive can hold files with the same names as files you already have, and tar replaces them without asking. GNU tar removes a leading / from member names and refuses to extract names that contain .., but an archive can still overwrite anything inside the folder you extract into. Use this order for any archive you did not make yourself:
- List it with
tar -tzfand read the names. - Create an empty folder:
mkdir -p ~/extract-check. - Extract there with
-C ~/extract-check, as your normal user, not as root. - Check the files, then copy what you need to its final place.
Extract to Another Directory
sudo mkdir -p /opt/app
sudo tar -xzf app-2.4.tar.gz -C /opt/app
sudo tar -xzf app-2.4.tar.gz -C /opt/app --strip-components=1
-C changes to the given folder before extracting, and the folder must exist. --strip-components=1 drops the top folder from every path, so the contents of app-2.4/ land directly in /opt/app. Run as root, tar restores the owners stored in the archive; add --no-same-owner to make the files belong to root instead, then set the owner you want with chown.
Extract a Single File or Folder
tar -xzf backup.tar.gz var/www/site/config.php
tar -xzf backup.tar.gz var/www/site/uploads
tar -xzf backup.tar.gz --wildcards '*.conf'
Write the path exactly as tar -tzf prints it, usually without a leading slash. The file is created with its full path below the current folder, so the first command writes ./var/www/site/config.php. Patterns need --wildcards and quotes. A compressed archive has no index, so tar decompresses and reads through it to find the member; on a large archive this takes about as long as a full extraction.
tar.bz2, tar.xz, and Other Formats
| Ending | Option | Program |
|---|---|---|
.tar.gz, .tgz | -z | gzip |
.tar.bz2 | -j | bzip2 |
.tar.xz | -J | xz |
.tar.zst | --zstd | zstd |
When it reads from a file, GNU tar detects the compression by itself, so tar -xf works for all of these; the option is needed when tar reads from a pipe, and tar then says which one to add. The matching program must be installed, including zstd for .tar.zst. On a minimal system, tar may stop with a message that it cannot run bzip2; install it with sudo apt install bzip2 on Ubuntu and Debian or sudo dnf install bzip2 on AlmaLinux and Rocky Linux. The xz package is xz-utils on Debian and Ubuntu and xz on AlmaLinux and Rocky Linux.
Create a tar.gz Archive
tar -czf site-backup.tar.gz -C /var/www example.com
tar -czf site-backup.tar.gz --exclude='*.log' -C /var/www example.com
tar -cJf logs.tar.xz /var/log/app
c creates. With -C /var/www, the archive holds example.com/ instead of the full path. Without it, tar prints a note that it removed the leading / from member names, which is normal. Keep copies of important archives off the server; snapshot vs backup explains why.
A .gz File That Is Not a tar Archive
gunzip dump.sql.gz
gunzip -k dump.sql.gz
A name that ends only in .gz is a single compressed file. gunzip replaces it with the uncompressed file; -k keeps the original. For .zip files, see how to unzip files in Ubuntu.
Common Errors
- Not in gzip format: the file is not gzip-compressed, or it is an error page saved by a failed download. Check it with
file archive.tar.gz; how to download a file with curl explains-Lfor redirects. - Unexpected end of file: the archive is incomplete. Download it again and compare the checksum.
- Cannot open, No such file or directory: the archive name or path is wrong; check it with
ls -l.