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.
- 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;
sudowhen reading/var/www/or restoring into protected paths. - Tools:
taris usually installed by default. - Know where your WordPress files live (common:
/var/www/html).
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 [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 [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 [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 [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 [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 [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 [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 [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 [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 [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 [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 [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
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)
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 [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
| Rule | Why it matters | Example |
|---|---|---|
-f placement | -f must be followed by the archive filename | tar -czvf site.tar.gz /var/www/html |
| Flag order | You can group flags (-czvf) or separate them, but keep -f directly before the filename | tar -c -z -v -f site.tar.gz /var/www/html |
--exclude placement | Excludes are applied while building the archive, so they must appear before the source paths | tar -czf site.tar.gz --exclude='*/cache/*' /var/www/html |
-C on extract | -C chooses the destination directory for extraction | tar -xzf site.tar.gz -C /tmp/restore-test |
Common flags
| Flag | Meaning | Typical use |
|---|---|---|
-c | Create archive | tar -cf site.tar /var/www/html |
-x | Extract archive | tar -xf site.tar -C /tmp/restore-test |
-t | List archive contents | tar -tf site.tar.gz |
-v | Verbose output | Debug what is included/extracted |
-f | Archive filename | Always required for file archives |
-z | Gzip compression | .tar.gz (balanced default) |
-j | Bzip2 compression | .tar.bz2 (rare on VPS backups) |
-J | XZ compression | .tar.xz (smaller, slower) |
--exclude=PATTERN | Skip files/dirs | Exclude caches, logs, nested archives |
-C DIR | Change directory | Control extract destination |
Archive formats compared
| Format | Extension | Compression | Size Impact | CPU Usage | Time Impact | WordPress Use Case |
|---|---|---|---|---|---|---|
.tar | .tar | None | Large | Low | Fast | Local packaging; combine with off-box compression later |
.tar.gz | .tar.gz or .tgz | Gzip | Medium | Medium | Medium | Default portable backup format for many hosts |
.tar.xz | .tar.xz | XZ | Small | High | Slow | Archival when size matters more than time |
.tar.zst | .tar.zst | Zstandard | Small-to-medium | Medium | Fast-to-medium | Great default on VPS when zstd is available |
Core tar commands (examples)
Create a .tar archive (no compression)
tar -cvf site.tar /var/www/html/
Expected Output:
/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 -czvf site.tar.gz /var/www/html/
Expected Output:
/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 -cjvf site.tar.bz2 /var/www/html/
Expected Output:
/var/www/html/
/var/www/html/wp-content/themes/twentytwentyfive/
...
Use Case: Maximum compression for storage/archive.
Extract a .tar.gz archive
tar -xzvf site.tar.gz
Expected Output:
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 -xvf site.tar
Expected Output:
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 -xjvf site.tar.bz2
Expected Output:
var/www/html/
var/www/html/index.php
...
Use Case: Restore space-optimized archive.
List contents of a .tar.gz archive
tar -tzf site.tar.gz
Expected Output:
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 -tvf site.tar
Expected Output:
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 -czvf current-folder.tar.gz .
Expected Output:
./
./wp-content/
./wp-content/themes/
...
Use Case: Avoid absolute paths in backup.
Archive only the themes/ folder
tar -czvf themes.tar.gz wp-content/themes/
Expected Output:
wp-content/themes/twentytwentyfive/
wp-content/themes/twentytwentyfive/functions.php
...
Use Case: Share/export themes only.
Exclude .log files
tar -czvf backup.tar.gz /var/www/html/ --exclude='*.log'
Expected Output: (Log files skipped)
/var/www/html/
(excludes all *.log files)
Use Case: Reduce archive bloat by skipping logs.
Exclude cache/ directories
tar -czvf backup.tar.gz /var/www/html/ --exclude='wp-content/cache'
Expected Output: (Cache directory excluded)
/var/www/html/
(excludes wp-content/cache/)
Use Case: Avoid temporary files in backup.
Partial backup: wp-config.php + uploads/
tar -czvf partial-backup.tar.gz wp-config.php wp-content/uploads/
Expected Output:
wp-config.php
wp-content/uploads/2025/09/image.png
...
Use Case: Quick backup of essential data only.
Extract into a target directory
tar -xzvf backup.tar.gz -C /var/www/html/
Expected Output:
/var/www/html/wp-content/
/var/www/html/wp-config.php
...
Use Case: Controlled restore location.
Use a date-based filename
tar -czf wp-$(date +%F).tar.gz /var/www/html/
Expected Output (file name):
wp-2025-09-24.tar.gz
Use Case: Automated cron-friendly backups.
Archive a logs directory
tar -zcvf logs.tar.gz /var/log/nginx/
Expected Output:
/var/log/nginx/access.log
/var/log/nginx/error.log
...
Use Case: Support ticket / debug packaging.
Create and delete original files
tar --remove-files -czvf site.tar.gz /var/www/html/
Expected Output:
(creates archive)
(deletes /var/www/html/*)
Use Case: One-shot move and archive ( Use with caution).
Archive all contents in the current directory
tar -czvf everything.tar.gz *
Expected Output:
index.php
wp-config.php
wp-content/
...
Use Case: Simple wildcard backup.
Pipe tar output into gzip
tar -cf - /var/www/html/ | gzip > backup.tar.gz
Expected Output:
(no visible output – streaming mode)
Use Case: Custom scripting and pipeline backups.
Extract only .php files
tar -xvf site.tar --wildcards '*.php'
Expected Output:
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
| Scenario | Command | Benefit |
|---|---|---|
| Full site backup | tar -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 archive | tar -tzf | Safe pre-check before extraction |
| Compress logs | tar -zcvf logs.tar.gz /var/log/nginx/ | Compact archiving for diagnostics |
Best practices
- Always compress backups (
.tar.gz) for portability. - Use
-excludefor speed and size reduction. - Include
vonly when you want output feedback (for logs or debugging). - Test backup integrity with
tar -tzf. - Use timestamps in filenames:
wp-backup-$(date +%F).tar.gz. - Schedule using
cronfor regular backups.
Quick lab: backup and restore WordPress
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
| Symptom | Cause | Fix |
|---|---|---|
Permission denied during extract | Target dir is protected | Use sudo or extract elsewhere |
| Archive has unexpected structure | Created from wrong base dir | Always cd into proper root before running tar |
| High CPU during compression | Using bzip2 on low-resources VPS | Use gzip or uncompressed .tar |
| Archive missing folders | Forgot to add -C or incorrect path | Check full absolute vs relative paths |
Cheat sheet
| Command | Purpose |
|---|---|
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.tar | Extract uncompressed .tar |
tar -xzvf file.tar.gz | Extract .tar.gz |
tar -tzf file.tar.gz | List contents of .tar.gz |
tar -xzvf file.tar.gz -C /target/dir | Extract to target directory |
--exclude=pattern | Skip specified files/folders |
Use case scenarios
| Scenario | Command | Outcome |
|---|---|---|
| Full WordPress site backup | tar -czvf wp-full.tar.gz /var/www/html/ | Safe copy for download/migration |
| Theme-only export | tar -czvf theme.tar.gz /var/www/html/wp-content/themes/ | Share or reuse themes |
| Config + uploads only | tar -czvf partial.tar.gz /var/www/html/wp-config.php /var/www/html/wp-content/uploads/ | Lightweight archive for config + media |
| Daily cron job | tar -czf /backups/wp-$(date +%F).tar.gz /var/www/html/ | Automated daily backup |
| Migration unpack | tar -xzvf wp-full.tar.gz -C /var/www/html/ | Restore or deploy site on new VPS |
Mini quiz
- What does
czvfstand for intar -czvf? - Which compression type is slower but results in smaller file sizes?
- How do you list contents of a
.tar.gzfile without extracting? - What’s the purpose of the
-excludeflag intar? - What would happen if you omit
Cwhen 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.
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.
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:
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.