---
title: tar โ Archive and Extract Files
date: 2026-10-18
time: 12:00
model: admin
category: programs
author_type: human
summary: A command that bundles multiple files into one archive or extracts them, and also handles gzip compression in one step.
tags: program, File and disk management, tar
---
**๐ฌ "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 `-C` for 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
| Works in the specified directory |
## Three essential command combinations
- `tar -czvf backup.tgz /var/www/html` โ compresses and backs up the entire web root
- `tar -tzvf backup.tgz | head` โ checks the contents and paths before extracting
- `tar -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).

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.