In Ubuntu systems, file packaging and decompression are common daily operations, such as backing up files or transferring data. The tar command is the core tool for these tasks—it can “package” multiple files or directories into a single file (called a tar package), and can further reduce file size when combined with compression tools like gzip, bzip2, or xz. This article explains the basic usage of the tar command in the simplest way, making it suitable for beginners new to Linux.
1. Core Function of the tar Command¶
The core function of tar is “packaging”, which merges multiple files/directories into a single uncompressed file. If you need to reduce file size, you can compress the tar package using gzip/bzip2/xz, generating formats like .tar.gz/.tar.bz2/.tar.xz.
2. Basic Options and Syntax¶
The basic syntax of the tar command is:
tar [options] [tar_package_name] [files/directories]
Here, [options] are key parameters controlling the operation, [tar_package_name] is the name of the generated package, and [files/directories] are the target files or directories to process.
3. Detailed Explanation of Common Options (with Examples)¶
The following are the most commonly used tar options for beginners, each paired with a simple example:
1. Basic Operation Options¶
| Option | Meaning | Scenario Example |
|---|---|---|
-c |
Create a new tar package (packaging) | Generate an uncompressed tar package |
-x |
Extract files from a tar package | Unpack/extract a tar package |
-t |
List files inside the tar package | Preview the content of the tar package |
-v |
Show detailed process (output files being processed) | Make operations clearer |
-f |
Specify the name of the tar package | Must follow the package name (e.g., -f my.tar) |
2. Compression Format Options (to be used with -c/-x)¶
| Option | Compression Tool | Generated File Format | Features |
|---|---|---|---|
-z |
gzip | .tar.gz |
Fast compression, suitable for small/medium files |
-j |
bzip2 | .tar.bz2 |
Moderate compression ratio, balanced speed |
-J |
xz | .tar.xz |
Highest compression ratio, suitable for large files |
3. Extraction Directory Option¶
| Option | Meaning | Scenario Example |
|---|---|---|
-C |
Specify the target directory for extraction | Avoid extracting files directly to the current directory |
4. Common Operation Examples¶
1. Packing Files (Generate an Uncompressed tar Package)¶
Requirement: Package all .txt files in the current directory into files.tar
tar -cvf files.tar *.txt
-c: Create a new package-v: Show detailed process (e.g., “Packing file1.txt”)-f files.tar: Specify the output package name asfiles.tar
After execution, files.tar will be generated in the current directory, with a size equal to the sum of all .txt files (uncompressed).
2. Compressed Packaging (Generate .tar.gz File)¶
Requirement: Package .txt files and compress them into files.tar.gz (using gzip)
tar -czvf files.tar.gz *.txt
-z: Call gzip for compression- Other parameters:
-c(create),-v(show process),-f files.tar.gz(specify package name)
The generated files.tar.gz will be significantly smaller than the uncompressed files.tar, making it suitable for transmission.
3. Extracting .tar.gz Files¶
Requirement: Extract files.tar.gz to the current directory
tar -xzvf files.tar.gz
-x: Extract-z: Use gzip for decompression-v: Show extraction process
To specify an extraction directory (e.g., /tmp), add the -C option:
tar -xzvf files.tar.gz -C /tmp
4. Viewing Tar Package Content¶
Requirement: Preview the files inside files.tar.gz
tar -tvf files.tar.gz
-t: View the content of the tar package (without extracting)- The command will list the names, permissions, etc., of all files.
5. Compressing to .tar.bz2 or .tar.xz¶
- bzip2 compression (generates
.tar.bz2):
tar -cjvf files.tar.bz2 *.txt
Use -j for decompression:
tar -xjvf files.tar.bz2
- xz compression (generates
.tar.xz, highest compression ratio):
tar -cJvf files.tar.xz *.txt
Use -J for decompression:
tar -xJvf files.tar.xz
5. Notes¶
-
Compression Format Must Match Options:
-.tar.gzuses-z; decompress with-xzvf
-.tar.bz2uses-j; decompress with-xjvf
-.tar.xzuses-J; decompress with-xJvf -
-fOption Must Follow Package Name:
Incorrect:tar -cvf my.tar -v(-vis redundant)
Correct:tar -cvf my.tar file1.txt -
Directory Structure Preservation:
When packaging a directory (e.g.,tar -cvf docs.tar docs/), the original directory structure will be retained after extraction.
6. Quick Start Summary¶
- Packing + Compression (Generate .gz):
tar -czvf package_name.tar.gz target_files - Decompress .gz:
tar -xzvf package_name.tar.gz - View Tar Package Content:
tar -tvf package_name.tar.gz
Mastering these basic operations will suffice for most file packaging and decompression scenarios. For advanced features (e.g., excluding specific files), further explore tar’s advanced options, but these basics are sufficient for beginners.