tar — Archive and Extract Files
💬 "tar command"
tar is a tool that bundles multiple files into a single file, and adding -z also applies gzip compression. It is the standard for Linux backup and distribution.
Options are often written together like tar -czvf, and when extracting you change it to -x.
- Concept: c is create (bundle), x is extract (unbundle), and t is list (list contents). These three letters are the core.
- Left letter: operation:
c(create) /x(extract) /t(list contents) - Right letters: options:
z(gzip),v(verbose output),f(specify file name, usually at the end) - Path: When extracting, specify the target directory with
-Cfor safety. - Result: The extension is conventionally .tar.gz or .tgz.
Commonly used options
| Option | Description |
|---|---|
| -c | Creates a new archive |
| -x | Extracts an archive |
| -t | Lists contents without extracting |
| -z | Compresses or decompresses with gzip |
| -C <dir> | Works in the specified directory |
Three essential command combinations
tar -czvf backup.tgz /var/www/html— compresses and backs up the entire web roottar -tzvf backup.tgz | head— checks the contents and paths before extractingtar -xzvf release.tgz -C /opt/app— extracts safely to the specified directory
💬 "Tell me a tip for using it without overlapping with nightly batch jobs"
This is a step-by-step summary of situations commonly encountered on live production servers.
Step 1 · Decide what to include and create the archive
Choose the backup target, then bundle it into a single file.
tar -czvf /backup/www-20261018.tgz /var/www/html
Step 2 · Check the list before extracting
Check the contents first to prevent extracting to the wrong path.
tar -tzvf /backup/www-20261018.tgz | head -20
Step 3 · Extract to the target directory
Don't overwrite the live directory directly; extract to a separate path and verify.
tar -xzvf /backup/www-20261018.tgz -C /tmp/restore
How it's used in practice
1. Distributing release files
Situation — You need to extract release.tgz, which contains build artifacts, to /opt/app on the production server.
tar -xzvf /tmp/release.tgz -C /opt/app
Result — The list of extracted files is printed to stderr and they are placed under /opt/app.
Point — Specify the production directory with -C, and if possible move the previous version to .bak before extracting for safety.
2. Backing up only configuration files
Situation — You want to back up only the nginx/php configuration separately.
tar -czvf /backup/conf-20261018.tgz /etc/nginx /etc/php
Result — The configuration files are bundled into a single tgz for storage. Because absolute paths are included, their locations are preserved when restoring.
Point — Bundling with absolute paths simplifies restoration. For relative-path archives, make sure to check the base path when extracting.
Things to watch out for
- When extracting, there is a risk of tar bombs (path traversal during extraction). For untrusted files, check the list with -t first.
- Files archived with absolute paths will overwrite their original location when extracted without -C, no matter where you extract them.
- Don't extract directly into a live directory; verify in a temporary path.
- Files being written, such as SQLite/DB files, can lose consistency, so use a separate method.
Actual execution screen
This is the result after checking the list first (top) and then checking the actual backup file size (bottom).
!tar command execution result: archive list and backup file size
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/tar.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments