tar (Tape Archiver)
The tar command is used to bundle multiple files and directories into a single archive file, often called a "tarball." It is also capable of compressing these archives using various algorithms like gzip or bzip2, making it the standard tool for backups and software distribution in Linux.
1. Basic Usage
tar [options] [archive_name.tar] [target]
2. Essential Options
| Option | Name | Description |
|---|---|---|
| c | create | Creates a new archive. |
| x | extract | Extracts files from an archive. |
| v | verbose | Lists files processed in the terminal. |
| f | file | Specifies the filename of the archive. |
| z | gzip | Filters the archive through gzip (for .tar.gz). |
| j | bzip2 | Filters the archive through bzip2 (for .tar.bz2). |
3. Practical Examples
① Compressing a directory into a tarball
tar -cvzf backup.tar.gz ./data/
② Extracting a compressed tarball
tar -xvzf backup.tar.gz
③ Listing the contents of an archive without extracting
tar -tvf backup.tar.gz
④ Extracting to a specific directory
tar -xvzf archive.tar.gz -C /opt/my_app/
4. [Tip] Why tar is preferred in Linux
Unlike many other compression formats, tar preserves Linux file permissions, owners, and groups. This makes it the only reliable choice for moving system files or entire user directories between Linux servers.