1. Core Purpose & Concepts
- Core Purpose: Learn the standard backup process in Linux: first, archive multiple files/directories into a single file package, and then compress this package to save disk space.
- Core Terminology:
- Archiving: The process of bundling multiple files and directories into a single file. This process does not involve compression; it is merely for easier management. It’s like putting a bunch of apples into a shopping bag.
- Compression: The process of reducing file size using specific algorithms.
- Tarball (
<span>.tar</span>): An archive file created using the<span>tar</span>command. It is the most common archive format in the Linux world. <span>tar</span>command: The core tool for creating (<span>c</span>), viewing (<span>t</span>), and extracting (<span>x</span>) Tarballs.- Gzip (
<span>.gz</span>): One of the most common compression algorithms. Its advantages are speed and moderate compression ratio. - Bzip2 (
<span>.bz2</span>): Another common compression algorithm. It is usually slower than Gzip, but can provide higher compression ratios, especially when dealing with large files. - Zip (
<span>.zip</span>): A format that combines archiving and compression, mainly used for exchanging files with other operating systems like Windows/macOS, as it has better cross-platform compatibility.
2. Key Command & Options
<span>tar</span> Command Core Options
Remembering the <span>tar</span> command is key to understanding the meaning of its core options.
| Option | Mnemonic | Function |
|---|---|---|
<span>-c</span> |
Create | Create a new archive file. |
<span>-x</span> |
Extract | Extract content from the archive file. |
<span>-t</span> |
Test / Table | List the contents of the archive file without extracting. |
<span>-v</span> |
Verbose | Display detailed operation information, letting you know what the command is doing. |
<span>-f</span> |
File | Specify that the following parameter is the archive filename. This option is almost always required. |
<span>-z</span> |
gZip | Compress or decompress using the Gzip algorithm when creating or extracting. |
<span>-j</span> |
bjip2 | Compress or decompress using the Bzip2 algorithm when creating or extracting. |
Workflow: Step-by-Step vs. One-Step
Process 1: Step-by-Step (Archive first, then compress)
Bash
# 1. Create Tarball (archive)tar -cvf archive.tar file1.txt folder1/
# 2. Compress using Gzipgzip archive.tar # This will generate archive.tar.gz and delete the original archive.tar
# 3. Decompressgunzip archive.tar.gz # This will restore archive.tar
# 4. Extract from Tarballtar -xvf archive.tar
Process 2: One-Step (Use <span>tar</span> to directly archive and compress)
This is the most common and efficient method.
| Operation | Gzip (<span>.tar.gz</span>) |
Bzip2 (<span>.tar.bz2</span>) |
|---|---|---|
| Create | <span>tar -czvf archive.tar.gz file1 folder1</span> |
<span>tar -cjvf archive.tar.bz2 file1 folder1</span> |
| Extract | <span>tar -xzvf archive.tar.gz</span> |
<span>tar -xjvf archive.tar.bz2</span> |
| View | <span>tar -tzvf archive.tar.gz</span> |
<span>tar -tjvf archive.tar.bz2</span> |
Process 3: Using <span>zip</span> (for cross-platform)
Bash
# Create zip archivezip my_archive.zip file1.txt folder1/
# Extract zip archiveunzip my_archive.zip
3. Practical Use Cases
- Creating Project Backups:
<span>tar -czvf project_backup_$(date +%F).tar.gz ./my_project/</span>is a classic command that packages the<span>my_project</span>directory and creates a filename with the current date, making it ideal for daily backups. - Software Source Distribution: In the open-source world, software source code is almost always provided for download in the form of
<span>.tar.gz</span>or<span>.tar.bz2</span>. - Transmitting Large Files: When you need to send a directory containing hundreds or thousands of small files over the network, packaging it into a compressed file for transmission is much faster than sending each file individually.
- Collaborating with Non-Linux Users: When you need to send a bunch of files to colleagues using Windows or macOS, creating a
<span>.zip</span>file is the best way to ensure they can easily extract it.
4. Common Pitfalls
- Confusing Archiving and Compression: The biggest misconception among beginners is thinking that the
<span>tar -cvf archive.tar</span>command compresses files. In fact, it only does “packaging,” and the overall size of the files may even slightly increase. True compression requires adding the<span>z</span>or<span>j</span>options. - Forgetting the
<span>f</span>Option:<span>tar</span>’s<span>f</span>option is used to specify the filename, and almost all operations require it. If forgotten,<span>tar</span>may attempt to operate on standard input/output, leading to unexpected behavior. - Filename and Compression Option Mismatch: When using the one-step compression method, the
<span>tar</span>command does not automatically add the correct file extension. You must manually write the filename correctly, for example,<span>tar -czvf my_files.tar.gz</span>. If you write it as<span>tar -czvf my_files.tar</span>, you will get a Gzip-compressed file with a misleading name. - Extracting to Current Directory: By default,
<span>tar -x</span>will extract files to the current working directory. If the file structure inside the archive is messy, this can clutter your current directory. - Safety Practice: It is best to first create a new directory,
<span>cd</span>into it, and then execute the extraction command. - Confusing
<span>c</span>and<span>x</span>: Incorrectly using the create (<span>c</span>) and extract (<span>x</span>) options in reverse may lead you to accidentally overwrite your precious backup compressed file with an empty file. Be careful.