Skip to main content

tar - Create and Extract Archives

tar is the default archiver on Linux for bundling files and directories into a single artifact. It is a core building block for WordPress backups: create an archive of your site files, optionally compress it (.tar.gz, .tar.zst, .tar.xz), and restore by extracting into a staging directory first.

Quick Summary
  • Create an archive: tar -cvf site.tar /var/www/html
  • Create a compressed archive: tar -czvf site.tar.gz /var/www/html
  • Extract safely: tar -xzf site.tar.gz -C /tmp/restore-test
  • List contents: tar -tf site.tar.gz
  • Exclude caches: --exclude='wp-content/cache'

Prerequisites

  • Access: shell access; sudo when reading /var/www/ or restoring into protected paths.
  • Tools: tar is usually installed by default.
  • Know where your WordPress files live (common: /var/www/html).
warning

Extraction can overwrite files. Always test restores into an empty directory (for example /tmp/restore-test/) and validate before copying into place.

Core syntax

1. Create an uncompressed archive (.tar)

tar-create-uncompressed-archive-001.sh
tar [OPTIONS/FLAGS] [DESTINATION_PATH]/[ARCHIVE_NAME].tar [SOURCE_PATHS...]

# Example:
# tar -c -v -f /home/wpbackup/sitefiles.tar /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles.tar

2. Create a gzip-compressed archive (.tar.gz)

tar-create-gzip-archive-002.sh
tar [OPTIONS/FLAGS] [DESTINATION_PATH]/[ARCHIVE_NAME].tar.gz [SOURCE_PATHS...]

# Example:
# tar -c -z -v -f /home/wpbackup/sitefiles_$(date +%F).tar.gz /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles_2025-10-07.tar.gz

3. Create a bzip2-compressed archive (.tar.bz2)

tar-create-bzip2-archive-003.sh
tar [OPTIONS/FLAGS] [DESTINATION_PATH]/[ARCHIVE_NAME].tar.bz2 [SOURCE_PATHS...]

# Example:
# tar -c -j -v -f /home/wpbackup/sitefiles_$(date +%F).tar.bz2 /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles_2025-10-07.tar.bz2

4. Create an xz-compressed archive (.tar.xz)

tar-create-xz-archive-004.sh
tar [OPTIONS/FLAGS] [DESTINATION_PATH]/[ARCHIVE_NAME].tar.xz [SOURCE_PATHS...]

# Example:
# tar -c -J -v -f /home/wpbackup/sitefiles_$(date +%F).tar.xz /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles_2025-10-07.tar.xz

5. Extract an uncompressed archive (.tar)

tar-extract-uncompressed-archive-005.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar] -C [OUTPUT_DIRECTORY]

# Example:
# tar -x -v -f /home/wpbackup/sitefiles.tar -C /home/dev_wpstrategist/public_html
# Input file: /home/wpbackup/sitefiles.tar
# Output directory: /home/dev_wpstrategist/public_html

6. Extract a gzip-compressed archive (.tar.gz)

tar-extract-gzip-archive-006.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.gz] -C [OUTPUT_DIRECTORY]

# Example:
# tar -x -z -v -f /home/wpbackup/sitefiles_2025-10-07.tar.gz -C /home/dev_wpstrategist/public_html
# Input file: /home/wpbackup/sitefiles_2025-10-07.tar.gz
# Output directory: /home/dev_wpstrategist/public_html

7. Extract a bzip2-compressed archive (.tar.bz2)

tar-extract-bzip2-archive-007.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.bz2] -C [OUTPUT_DIRECTORY]

# Example:
# tar -x -j -v -f /home/wpbackup/sitefiles_2025-10-07.tar.bz2 -C /home/dev_wpstrategist/public_html
# Input file: /home/wpbackup/sitefiles_2025-10-07.tar.bz2
# Output directory: /home/dev_wpstrategist/public_html

8. Extract an xz-compressed archive (.tar.xz)

tar-extract-xz-archive-008.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.xz] -C [OUTPUT_DIRECTORY]

# Example:
# tar -x -J -v -f /home/wpbackup/sitefiles_2025-10-07.tar.xz -C /home/dev_wpstrategist/public_html
# Input file: /home/wpbackup/sitefiles_2025-10-07.tar.xz
# Output directory: /home/dev_wpstrategist/public_html

9. List contents of a tar archive

tar-list-archive-contents-009.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.gz]

# Example:
# tar -t -v -f /home/wpbackup/sitefiles_2025-10-07.tar.gz
# Input file: /home/wpbackup/sitefiles_2025-10-07.tar.gz
# Output: file list printed to terminal

10. Append files into an existing tar archive

tar-append-files-to-archive-010.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar] [NEW_FILE_PATHS...]

# Example:
# tar -r -v -f /home/wpbackup/sitefiles.tar /home/dev_wpstrategist/public_html/robots.txt
# Input archive: /home/wpbackup/sitefiles.tar
# Appended file: /home/dev_wpstrategist/public_html/robots.txt

11. Verify the integrity of a tar archive

tar-verify-archive-integrity-011.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.gz] > /dev/null

# Example:
# tar -t -f /home/wpbackup/sitefiles_2025-10-07.tar.gz > /dev/null
# Input file: /home/wpbackup/sitefiles_2025-10-07.tar.gz
# Output: zero exit code (0) indicates archive is valid

12. Split a large tar.gz archive into 5 GB parts

tar-split-archive-into-parts-012.sh
tar [OPTIONS/FLAGS] - | split -b 5000M - [OUTPUT_DIRECTORY]/[ARCHIVE_NAME].tar.gz.part-

# Example:
# tar -c -z -f - /home/dev_wpstrategist/public_html | split -b 5000M - /home/wpbackup/sitefiles_$(date +%F).tar.gz.part-
# Input directory: /home/dev_wpstrategist/public_html
# Output parts: /home/wpbackup/sitefiles_2025-10-07.tar.gz.part-aa, -ab, -ac ...

13. Combine split tar.gz parts back into one file

tar-combine-split-parts-013.sh
cat [INPUT_PARTS...] > [OUTPUT_FILE.tar.gz]

# Example:
# cat /home/wpbackup/sitefiles_2025-10-07.tar.gz.part-* > /home/wpbackup/sitefiles_2025-10-07.tar.gz
# Input: multiple split parts
# Output: reassembled archive file

14. Export database and compress as tar.gz (database + files)

tar-export-db-and-compress-014.sh
mysqldump -u [USER] -p'[PASSWORD]' [DATABASE] > [TEMP_DIRECTORY]/[DB_NAME].sql && \
tar [OPTIONS/FLAGS] [OUTPUT_DIRECTORY]/[ARCHIVE_NAME].tar.gz [SOURCE_PATHS...] [TEMP_DIRECTORY]/[DB_NAME].sql

# Example:
# mysqldump -u DB_USER -p'DB_PASS' DB_NAME > /home/tmp/db.sql && \
# tar -c -z -v -f /home/wpbackup/wp-full_$(date +%F).tar.gz /home/dev_wpstrategist/public_html /home/tmp/db.sql
# Input: WordPress files + database dump
# Output: /home/wpbackup/wp-full_2025-10-07.tar.gz

15. Restore database directly from a tar.gz archive

tar-restore-db-from-archive-015.sh
tar [OPTIONS/FLAGS] [INPUT_FILE.tar.gz] [DB_FILE_PATH_INSIDE_ARCHIVE] | mysql -u [USER] -p'[PASSWORD]' [DATABASE]

# Example:
# tar -x -z -O -f /home/wpbackup/wp-full_2025-10-07.tar.gz home/tmp/db.sql \
# | mysql -u DB_USER -p'DB_PASS' DB_NAME
# Input: /home/wpbackup/wp-full_2025-10-07.tar.gz
# Output: database imported into MySQL

Key rules

RuleWhy it mattersExample
-f placement-f must be followed by the archive filenametar -czvf site.tar.gz /var/www/html
Flag orderYou can group flags (-czvf) or separate them, but keep -f directly before the filenametar -c -z -v -f site.tar.gz /var/www/html
--exclude placementExcludes are applied while building the archive, so they must appear before the source pathstar -czf site.tar.gz --exclude='*/cache/*' /var/www/html
-C on extract-C chooses the destination directory for extractiontar -xzf site.tar.gz -C /tmp/restore-test

Common flags

FlagMeaningTypical use
-cCreate archivetar -cf site.tar /var/www/html
-xExtract archivetar -xf site.tar -C /tmp/restore-test
-tList archive contentstar -tf site.tar.gz
-vVerbose outputDebug what is included/extracted
-fArchive filenameAlways required for file archives
-zGzip compression.tar.gz (balanced default)
-jBzip2 compression.tar.bz2 (rare on VPS backups)
-JXZ compression.tar.xz (smaller, slower)
--exclude=PATTERNSkip files/dirsExclude caches, logs, nested archives
-C DIRChange directoryControl extract destination

Archive formats compared

FormatExtensionCompressionSize ImpactCPU UsageTime ImpactWordPress Use Case
.tar.tarNoneLargeLowFastLocal packaging; combine with off-box compression later
.tar.gz.tar.gz or .tgzGzipMediumMediumMediumDefault portable backup format for many hosts
.tar.xz.tar.xzXZSmallHighSlowArchival when size matters more than time
.tar.zst.tar.zstZstandardSmall-to-mediumMediumFast-to-mediumGreat default on VPS when zstd is available

Core tar commands (examples)


Create a .tar archive (no compression)

tar-create-a-tar-archive-no-compression-002.sh
tar -cvf site.tar /var/www/html/

Expected Output:

tar-create-a-tar-archive-no-compression-004.sh
/var/www/html/
/var/www/html/index.php
/var/www/html/wp-config.php
/var/www/html/wp-content/
/var/www/html/wp-content/themes/
...

Use Case: Fast local archive (no compression).


Create a .tar.gz archive (gzip)

tar-create-a-tar-gz-archive-gzip-006.sh
tar -czvf site.tar.gz /var/www/html/

Expected Output:

tar-create-a-tar-gz-archive-gzip-008.sh
/var/www/html/
/var/www/html/index.php
/var/www/html/wp-content/uploads/
...

Use Case: Compressed WordPress backup for download/migration.


Create a .tar.bz2 archive (bzip2)

tar-create-a-tar-bz2-archive-bzip2-010.sh
tar -cjvf site.tar.bz2 /var/www/html/

Expected Output:

tar-create-a-tar-bz2-archive-bzip2-012.sh
/var/www/html/
/var/www/html/wp-content/themes/twentytwentyfive/
...

Use Case: Maximum compression for storage/archive.


Extract a .tar.gz archive

tar-extract-a-tar-gz-archive-014.sh
tar -xzvf site.tar.gz

Expected Output:

tar-extract-a-tar-gz-archive-016.sh
var/www/html/
var/www/html/index.php
var/www/html/wp-content/uploads/image.jpg
...

Use Case: Restore a WordPress backup.


Extract a .tar archive (no compression)

tar-extract-a-tar-archive-no-compression-018.sh
tar -xvf site.tar

Expected Output:

tar-extract-a-tar-archive-no-compression-020.sh
var/www/html/wp-config.php
var/www/html/wp-content/plugins/akismet/
...

Use Case: Restore site snapshot or theme archive.


Extract a .tar.bz2 archive

tar-extract-a-tar-bz2-archive-022.sh
tar -xjvf site.tar.bz2

Expected Output:

tar-extract-a-tar-bz2-archive-024.sh
var/www/html/
var/www/html/index.php
...

Use Case: Restore space-optimized archive.


List contents of a .tar.gz archive

tar-list-contents-of-a-tar-gz-archive-026.sh
tar -tzf site.tar.gz

Expected Output:

tar-list-contents-of-a-tar-gz-archive-028.sh
var/www/html/
var/www/html/wp-content/plugins/
var/www/html/wp-content/uploads/2025/09/
...

Use Case: Preview archive contents without extraction.


List contents of a .tar archive

tar-list-contents-of-a-tar-archive-030.sh
tar -tvf site.tar

Expected Output:

tar-list-contents-of-a-tar-archive-032.sh
drwxr-xr-x root/root 0 2025-09-24 08:30 var/www/html/
-rw-r--r-- root/root 28567 2025-09-24 08:30 var/www/html/index.php
...

Use Case: Inspect archive structure and file metadata.


Archive current folder (relative paths)

tar-archive-current-folder-relative-paths-034.sh
tar -czvf current-folder.tar.gz .

Expected Output:

tar-archive-current-folder-relative-paths-036.sh
./
./wp-content/
./wp-content/themes/
...

Use Case: Avoid absolute paths in backup.


Archive only the themes/ folder

tar-archive-only-the-themes-folder-038.sh
tar -czvf themes.tar.gz wp-content/themes/

Expected Output:

tar-archive-only-the-themes-folder-040.sh
wp-content/themes/twentytwentyfive/
wp-content/themes/twentytwentyfive/functions.php
...

Use Case: Share/export themes only.


Exclude .log files

tar-exclude-log-files-042.sh
tar -czvf backup.tar.gz /var/www/html/ --exclude='*.log'

Expected Output: (Log files skipped)

tar-exclude-log-files-044.sh
/var/www/html/
(excludes all *.log files)

Use Case: Reduce archive bloat by skipping logs.


Exclude cache/ directories

tar-exclude-cache-directories-046.sh
tar -czvf backup.tar.gz /var/www/html/ --exclude='wp-content/cache'

Expected Output: (Cache directory excluded)

tar-exclude-cache-directories-048.sh
/var/www/html/
(excludes wp-content/cache/)

Use Case: Avoid temporary files in backup.


Partial backup: wp-config.php + uploads/

tar-partial-backup-wp-config-php-uploads-050.sh
tar -czvf partial-backup.tar.gz wp-config.php wp-content/uploads/

Expected Output:

tar-partial-backup-wp-config-php-uploads-052.sh
wp-config.php
wp-content/uploads/2025/09/image.png
...

Use Case: Quick backup of essential data only.


Extract into a target directory

tar-extract-into-a-target-directory-054.sh
tar -xzvf backup.tar.gz -C /var/www/html/

Expected Output:

tar-extract-into-a-target-directory-056.sh
/var/www/html/wp-content/
/var/www/html/wp-config.php
...

Use Case: Controlled restore location.


Use a date-based filename

tar-use-a-date-based-filename-058.sh
tar -czf wp-$(date +%F).tar.gz /var/www/html/

Expected Output (file name):

tar-use-a-date-based-filename-060.sh
wp-2025-09-24.tar.gz

Use Case: Automated cron-friendly backups.


Archive a logs directory

tar-archive-a-logs-directory-062.sh
tar -zcvf logs.tar.gz /var/log/nginx/

Expected Output:

tar-archive-a-logs-directory-064.sh
/var/log/nginx/access.log
/var/log/nginx/error.log
...

Use Case: Support ticket / debug packaging.


Create and delete original files

tar-create-and-delete-original-files-066.sh
tar --remove-files -czvf site.tar.gz /var/www/html/

Expected Output:

tar-create-and-delete-original-files-068.sh
(creates archive)
(deletes /var/www/html/*)

Use Case: One-shot move and archive ( Use with caution).


Archive all contents in the current directory

tar-archive-all-contents-in-the-current-directory-070.sh
tar -czvf everything.tar.gz *

Expected Output:

tar-archive-all-contents-in-the-current-directory-072.sh
index.php
wp-config.php
wp-content/
...

Use Case: Simple wildcard backup.


Pipe tar output into gzip

tar-pipe-tar-output-into-gzip-074.sh
tar -cf - /var/www/html/ | gzip > backup.tar.gz

Expected Output:

tar-pipe-tar-output-into-gzip-076.sh
(no visible output – streaming mode)

Use Case: Custom scripting and pipeline backups.


Extract only .php files

tar-extract-only-php-files-078.sh
tar -xvf site.tar --wildcards '*.php'

Expected Output:

tar-extract-only-php-files-080.sh
var/www/html/index.php
var/www/html/wp-content/themes/twentytwentyfive/functions.php
...

Use Case: Partial restore of code without media/cache.




Common use cases

ScenarioCommandBenefit
Full site backuptar -czvf wp-site.tar.gz /var/www/html/Easy to move or restore
Exclude cache--exclude='wp-content/cache'Prevent bloated backups
Extract to target-C /var/www/html/Clean, controlled restoration
Inspect archivetar -tzfSafe pre-check before extraction
Compress logstar -zcvf logs.tar.gz /var/log/nginx/Compact archiving for diagnostics

Best practices

  1. Always compress backups (.tar.gz) for portability.
  2. Use -exclude for speed and size reduction.
  3. Include v only when you want output feedback (for logs or debugging).
  4. Test backup integrity with tar -tzf.
  5. Use timestamps in filenames: wp-backup-$(date +%F).tar.gz.
  6. Schedule using cron for regular backups.

Quick lab: backup and restore WordPress

tar-quick-lab-backup-and-restore-wordpress-082.sh
cd ~
tar -czvf wp-$(date +%F).tar.gz /var/www/html/
ls -lh wp-*.tar.gz

# Extract to test
mkdir ~/restore-test
tar -xzvf wp-*.tar.gz -C ~/restore-test

Expected Output:

  • Archive size shown with human-readable sizes.
  • Files appear under ~/restore-test/var/www/html/.

Troubleshooting

SymptomCauseFix
Permission denied during extractTarget dir is protectedUse sudo or extract elsewhere
Archive has unexpected structureCreated from wrong base dirAlways cd into proper root before running tar
High CPU during compressionUsing bzip2 on low-resources VPSUse gzip or uncompressed .tar
Archive missing foldersForgot to add -C or incorrect pathCheck full absolute vs relative paths

Cheat sheet

CommandPurpose
tar -cvf file.tar dir/Create uncompressed archive
tar -czvf file.tar.gz dir/Create compressed .gz archive
tar -cjvf file.tar.bz2 dir/High-compression .bz2 archive
tar -xvf file.tarExtract uncompressed .tar
tar -xzvf file.tar.gzExtract .tar.gz
tar -tzf file.tar.gzList contents of .tar.gz
tar -xzvf file.tar.gz -C /target/dirExtract to target directory
--exclude=patternSkip specified files/folders

Use case scenarios

ScenarioCommandOutcome
Full WordPress site backuptar -czvf wp-full.tar.gz /var/www/html/Safe copy for download/migration
Theme-only exporttar -czvf theme.tar.gz /var/www/html/wp-content/themes/Share or reuse themes
Config + uploads onlytar -czvf partial.tar.gz /var/www/html/wp-config.php /var/www/html/wp-content/uploads/Lightweight archive for config + media
Daily cron jobtar -czf /backups/wp-$(date +%F).tar.gz /var/www/html/Automated daily backup
Migration unpacktar -xzvf wp-full.tar.gz -C /var/www/html/Restore or deploy site on new VPS

Mini quiz

  1. What does czvf stand for in tar -czvf?
  2. Which compression type is slower but results in smaller file sizes?
  3. How do you list contents of a .tar.gz file without extracting?
  4. What’s the purpose of the -exclude flag in tar?
  5. What would happen if you omit C when extracting a tar file?

Restore checklist

Use this checklist before restoring a tarball into a live WordPress docroot.

  • Verify the archive can be listed without errors.
  • Extract to a staging directory first.
  • Confirm key files exist (wp-config.php, wp-content/uploads/).
  • Only then copy into place and restart services.
restore-drill-tar-gz.sh
tar -tzf /home/wpbackup/wp-full-2026-03-01.tar.gz >/dev/null

sudo rm -rf /tmp/restore-test
sudo mkdir -p /tmp/restore-test
sudo tar -xzf /home/wpbackup/wp-full-2026-03-01.tar.gz -C /tmp/restore-test

ls -la /tmp/restore-test | head

Safer archive paths

To avoid confusing absolute paths inside your archive, prefer -C + . patterns.

create-tar-gz-with-relative-paths.sh
sudo tar -C /var/www/html -czvf /home/wpbackup/wp-files-$(date +%F).tar.gz .

Advanced notes (ownership and permissions)

When restoring between servers, you may need to fix ownership after extraction:

fix-wordpress-ownership-after-restore.sh
sudo chown -R www-data:www-data /var/www/html

If you are restoring into a different environment, always confirm the expected web user and group.