tar combines multiple files and directories into a single archive file. The name stands for “tape archive,” a holdover from when Unix systems backed up to magnetic tape. The tool survived because the concept is universally useful: bundle a directory tree into one file that you can transfer, store, or compress.
A
.tar
file is an uncompressed archive. It packages files together but does not reduce their size. A
.tar.gz
(or
.tgz
) file is a tar archive compressed with gzip, which is the most common format you encounter on Linux. This guide covers creating, extracting, and inspecting tar archives with practical examples for every common scenario.
What tar files are#
A tar archive is a single file that contains other files and directories, along with their metadata (permissions, ownership, timestamps). Think of it as a container.
The common extensions:
| Extension | What it is |
|---|---|
.tar
| Uncompressed archive. Files are bundled but not compressed. |
.tar.gz
or
.tgz
| Archive compressed with gzip. The most common format on Linux. |
.tar.bz2
| Archive compressed with bzip2. Smaller than gzip but slower to create. |
.tar.xz
| Archive compressed with xz. Smallest file size but slowest compression. |
.tar.zst
| Archive compressed with zstd. Good balance of speed and compression ratio. |
The compression is applied on top of the archive. tar creates the bundle, then a compression tool (gzip, bzip2, xz) compresses the bundle. Modern versions of tar handle both steps in a single command.
Creating archives#
Archive a directory
tar -cf archive.tar /path/to/directory
This creates
archive.tar
containing everything in
/path/to/directory
. The flags:
-
-c— create a new archive -
-f archive.tar— write to the filearchive.tar
The
-f
flag must be followed by the filename. It is always the last flag before the filename, or the command breaks.
Archive a directory with gzip compression
tar -czf archive.tar.gz /path/to/directory
The
-z
flag adds gzip compression. This is the command you will use most often. On a typical WordPress installation, gzip reduces the archive size by 60-80% compared to an uncompressed tar.
Archive a directory with bzip2 compression
tar -cjf archive.tar.bz2 /path/to/directory
The
-j
flag uses bzip2. Produces smaller files than gzip but takes noticeably longer, especially on large directories.
Archive a directory with xz compression
tar -cJf archive.tar.xz /path/to/directory
The
-J
flag (capital J) uses xz. Produces the smallest files but is the slowest. Use this when archive size matters more than creation speed, like when transferring large backups over a slow connection.
Archive multiple files and directories
tar -czf archive.tar.gz file1.txt file2.txt /path/to/dir1 /path/to/dir2
You can list any combination of files and directories as arguments.
Archive with verbose output
tar -czvf archive.tar.gz /path/to/directory
The
-v
flag prints each file as it is added to the archive. Useful for monitoring progress on large directories. On very large archives, the verbose output itself can slow things down since every filename is printed to the terminal.
Extracting archives#
Extract a .tar.gz file
tar -xzf archive.tar.gz
This extracts the contents into the current directory. The flags:
-
-x— extract -
-z— decompress gzip -
-f archive.tar.gz— read from this file
Extract an uncompressed .tar file
tar -xf archive.tar
Extract a .tar.bz2 file
tar -xjf archive.tar.bz2
Extract a .tar.xz file
tar -xJf archive.tar.xz
Let tar detect the compression automatically
Modern versions of tar (GNU tar, which is standard on Linux) can detect the compression format automatically:
tar -xf archive.tar.gz
tar -xf archive.tar.bz2
tar -xf archive.tar.xz
Without specifying
-z
,
-j
, or
-J
, tar examines the file header and decompresses accordingly. This works for extraction. For creation, you still need to specify the compression flag because tar needs to know which format you want.
Extract to a specific directory
tar -xzf archive.tar.gz -C /path/to/destination
The
-C
flag changes to the specified directory before extracting. The directory must already exist. If it does not:
mkdir -p /path/to/destination && tar -xzf archive.tar.gz -C /path/to/destination
Extract a single file from an archive
tar -xzf archive.tar.gz path/to/specific/file.txt
The path must match exactly as it appears in the archive. Use
tar -tzf
(covered below) to find the exact path first.
Extract with verbose output
tar -xzvf archive.tar.gz
Prints each file as it is extracted.
Listing archive contents#
List all files in an archive
tar -tzf archive.tar.gz
The
-t
flag lists contents without extracting. Combined with
-z
for gzip and
-f
for the filename. This prints every file and directory path in the archive.
List with details
tar -tzvf archive.tar.gz
Adding
-v
shows permissions, ownership, size, and timestamp for each entry, similar to
ls -l
output:
drwxr-xr-x user/group 0 2026-03-19 14:00 wordpress/
-rw-r--r-- user/group 405 2026-03-19 14:00 wordpress/index.php
-rw-r--r-- user/group 3065 2026-03-19 14:00 wordpress/wp-config.php
Search for a file in an archive
tar -tzf archive.tar.gz | grep wp-config
Pipe the file listing through grep to find specific files without extracting the entire archive.
Count files in an archive
tar -tzf archive.tar.gz | wc -l
Common flag reference#
| Flag | Meaning |
|---|---|
-c
| Create a new archive |
-x
| Extract files from an archive |
-t
| List the contents of an archive |
-f
| Specify the archive filename (must come last among flags) |
-z
| Compress/decompress with gzip (.tar.gz) |
-j
| Compress/decompress with bzip2 (.tar.bz2) |
-J
| Compress/decompress with xz (.tar.xz) |
-v
| Verbose output (list files being processed) |
-C
| Change to directory before operation |
-p
| Preserve file permissions on extraction |
--exclude
| Exclude files matching a pattern |
--strip-components
| Remove leading directory components on extraction |
Excluding files#
Exclude by pattern
tar -czf archive.tar.gz --exclude='*.log' /path/to/directory
Skips all files ending in
.log
.
Exclude multiple patterns
tar -czf archive.tar.gz \
--exclude='.git' \
--exclude='node_modules' \
--exclude='*.log' \
--exclude='wp-content/cache' \
/var/www/html
Exclude from a file
For complex exclusion lists:
tar -czf archive.tar.gz --exclude-from=exclude-list.txt /var/www/html
Where
exclude-list.txt
contains one pattern per line:
.git
node_modules
*.log
*.tmp
wp-content/cache
wp-content/uploads/backups
Working with directories#
Archive a folder without the full path
When you run:
tar -czf backup.tar.gz /var/www/html
The archive contains the full path structure:
var/www/html/index.php
,
var/www/html/wp-config.php
, etc. When extracted, it creates the
var/www/html/
directory tree.
To archive only the contents without the leading path:
tar -czf backup.tar.gz -C /var/www/html .
The
-C /var/www/html
changes to that directory first. The
.
means “everything in the current directory.” Now the archive contains
./index.php
,
./wp-config.php
— no leading path. When extracted, files go directly into the current directory.
This is the approach you want when creating backups that will be extracted into a different directory structure.
Strip leading directory components on extraction
If an archive was created with a leading directory and you want to remove it during extraction:
tar -xzf wordpress-6.5.tar.gz --strip-components=1 -C /var/www/html
The WordPress download, for example, extracts to a
wordpress/
directory by default. The
--strip-components=1
removes that first directory level, placing files directly in
/var/www/html
instead of
/var/www/html/wordpress/
.
Practical examples#
Back up a WordPress site
cd /var/www/html
tar -czf ~/wordpress-backup-$(date +%Y%m%d).tar.gz \
--exclude='wp-content/cache' \
--exclude='wp-content/uploads/backups' \
--exclude='wp-content/debug.log' \
.
Creates a date-stamped compressed archive of the entire WordPress installation, excluding cache, old backup files, and the debug log. The
-C
approach (using
.
after
cd
) keeps the archive paths clean.
For full WordPress migration workflows including database export and DNS switching, see How to migrate WordPress to another hosting provider.
Back up only the database dump
mysqldump -u db_user -p db_name > ~/database.sql
tar -czf ~/database-backup-$(date +%Y%m%d).tar.gz -C ~ database.sql
rm ~/database.sql
Exports the database, compresses it into an archive, and removes the uncompressed SQL file. A 500 MB SQL dump typically compresses to 50-80 MB with gzip.
Transfer a site between servers
On the source server, create the archive:
cd /var/www/html
tar -czf ~/site-transfer.tar.gz .
Transfer it to the destination server using SCP:
scp ~/site-transfer.tar.gz user@new-server:/tmp/
On the destination server, extract:
mkdir -p /var/www/html
tar -xzf /tmp/site-transfer.tar.gz -C /var/www/html
For ongoing file synchronization rather than one-time transfers, rsync is more efficient because it only transfers changed files on subsequent runs.
Download and install software from a tarball
Many Linux tools are distributed as
.tar.gz
files. The general pattern:
wget https://example.com/software-1.0.tar.gz
tar -xzf software-1.0.tar.gz
cd software-1.0
./configure
make
sudo make install
The
--strip-components
flag is useful when you want to extract directly into a target directory without the version-named parent folder:
tar -xzf software-1.0.tar.gz --strip-components=1 -C /opt/software
Create an incremental backup
tar supports incremental backups using a snapshot file:
# First backup (full)
tar -czf full-backup.tar.gz --listed-incremental=snapshot.snar /var/www/html
# Subsequent backups (incremental - only changed files)
tar -czf incremental-$(date +%Y%m%d).tar.gz --listed-incremental=snapshot.snar /var/www/html
The
--listed-incremental
flag tells tar to track which files have changed since the last backup. The first run creates a full backup and initializes the snapshot file. Subsequent runs only archive files that changed since the last run. The snapshot file (
snapshot.snar
) must be preserved between runs.
To restore, extract the full backup first, then each incremental backup in order.
Archive and transfer in one step
You can pipe tar output directly to SSH to transfer without creating a local archive file:
tar -czf - /var/www/html | ssh user@remote-server "tar -xzf - -C /var/www/html"
The
-f -
tells tar to write to stdout (on the source) or read from stdin (on the destination). The data streams directly from one server to the other without creating an intermediate file on either end. This is useful when disk space is limited.
Comparing tar with other tools#
tar vs zip
ZIP files are more common on Windows. tar archives are standard on Linux. The practical differences:
- ZIP compresses each file individually. tar compresses the entire archive as a single stream, which typically achieves better compression ratios.
- ZIP allows extracting individual files without decompressing the entire archive. With tar.gz, the entire archive must be decompressed to access any file.
- ZIP preserves basic permissions. tar preserves full Unix metadata including ownership, timestamps, and symbolic links.
For transferring files to or from Windows machines, ZIP is more convenient. For Linux server backups and transfers, tar.gz is standard.
tar vs rsync
tar creates a snapshot archive at a point in time. rsync synchronizes directories incrementally, transferring only changed files. They serve different purposes:
- Use tar when you need a single archive file for storage, download, or distribution
- Use rsync when you need to keep two directories in sync over time
A common pattern combines both: rsync for daily incremental syncs, tar for periodic full backup archives stored offsite.
Troubleshooting#
“tar: Removing leading ‘/’ from member names”
This warning appears when you archive files with absolute paths:
tar -czf backup.tar.gz /var/www/html
tar strips the leading
/
as a safety measure. Without this, extracting the archive would overwrite files at the absolute path, which is dangerous. The archive stores paths as
var/www/html/
instead of
/var/www/html/
. This is normal and expected behavior, not an error.
“tar: Cannot stat: No such file or directory”
One of the files or directories you specified does not exist. Check the paths. If files are being deleted while tar runs (common with log files or cache directories), use
--ignore-failed-read
to continue instead of aborting:
tar -czf backup.tar.gz --ignore-failed-read /var/www/html
“gzip: stdin: not in gzip format”
You are trying to extract with
-z
(gzip) but the file is not gzip-compressed. It might be a plain tar, bzip2, or xz archive. Check the file type:
file archive.tar.gz
This prints the actual file type. Use the correct flag (
-j
for bzip2,
-J
for xz) or drop the compression flag entirely and let tar auto-detect.
“tar: Error is not recoverable: exiting now”
Usually a permissions issue. tar cannot read the files it is trying to archive, or it cannot write to the destination. Check that you have read access to the source files and write access to the directory where the archive is being created. For system directories, you may need
sudo
:
sudo tar -czf backup.tar.gz /etc/nginx
Archive is much larger than expected
If a tar.gz file is nearly the same size as the uncompressed data, the files are likely already compressed (JPEG images, PNG files, ZIP archives, video files). Gzip cannot compress data that is already compressed. For directories full of media files, the tar.gz will be roughly the same size as the tar.
tar on Hostney#
On Hostney, tar is available via SSH in your account container. You can use it to create backups, package files for download, or extract uploaded archives.
Connect via SSH using the credentials from the Terminal Access section in the control panel, then run tar commands against your site files. For transferring the resulting archives to your local machine, use SFTP or SCP.