Introduction to the tar Command
The tar command is a classic tool for file archiving and packaging in Linux and Unix systems. Its name originates from "Tape ARchive." It can bundle multiple files or directories into a single archive file (commonly called a tarball) and can be combined with compression tools (like gzip, bzip2) to further reduce file size.
Basic Command Format
The basic syntax of the tar command is:
tar [options...] [archive_name] [files_or_directories...]
Options are typically a series of letters, where one primary operation command (e.g., -c, -x, -t) must be selected, and only one.
Common Operations and Parameters
Primary Operation Commands (choose one)
-cor--create: Create a new archive.-xor--extract: Extract files from an archive.-tor--list: List the contents of an archive.-ror--append: Append files to the end of an archive.-uor--update: Append only files newer than those in the archive.
Common Auxiliary Parameters
-for--file=ARCHIVE: Specify the archive filename. This parameter is usually required and should be placed last in the argument list.-vor--verbose: Verbosely list processed files.-zor--gzip: Filter the archive through gzip (for .tar.gz or .tgz files).-jor--bzip2: Filter the archive through bzip2 (for .tar.bz2 files).-Jor--xz: Filter the archive through xz (for .tar.xz files, modern and common).-Cor--directory=DIR: Change to the specified directory before performing operations.--exclude=PATTERN: Exclude files or directories matching the pattern.
Practical Examples
1. Creating Archives and Compression
Archive only (no compression): Package the /usr/local/test directory into test.tar.
tar -cvf /usr/local/auto_bak/test.tar /usr/local/test
Archive and compress with gzip: Create a .tar.gz file.
tar -zcvf /usr/local/auto_bak/test.tar.gz /usr/local/test
Archive and compress with bzip2: Create a .tar.bz2 file.
tar -jcvf /usr/local/auto_bak/test.tar.bz2 /usr/local/test
Archive while excluding specific files/directories:
tar -zcvf backup.tar.gz /path/to/dir --exclude=*.log --exclude=./cache
2. Viewing Archive Contents
tar -tf archive.tar.gz
3. Extracting Archives
Extract to the current directory:
tar -zxvf test.tar.gz
Extract to a specified directory (using the -C parameter):
tar -zxvf test.tar.gz -C /home
Note: The command tar -zxvf test.tar.gz /home is incorrect; /home would be interpreted as a member to extract from the archive, not the target directory. The correct usage is -C /home.
4. Other Compression and Extraction Examples
Compression:
tar -czf photos.tar.gz *.jpg # Create .tar.gz
tar -cjf photos.tar.bz2 *.jpg # Create .tar.bz2
tar -cJf photos.tar.xz *.jpg # Create .tar.xz (recommended, high ratio)
zip -r photos.zip *.jpg # Create .zip file
Extraction:
tar -xvf file.tar # Extract .tar
tar -xzvf file.tar.gz # Extract .tar.gz
tar -xjvf file.tar.bz2 # Extract .tar.bz2
tar -xJvf file.tar.xz # Extract .tar.xz
unzip file.zip # Extract .zip
unrar x file.rar # Extract .rar (requires unrar)
Quick Reference for Common Archive Formats
| File Extension | Extraction Command | Description |
|---|---|---|
*.tar |
tar -xvf |
Extract tar archive |
*.tar.gz or *.tgz |
tar -xzvf |
Extract gzip-compressed tar |
*.tar.bz2 |
tar -xjvf |
Extract bzip2-compressed tar |
*.tar.xz |
tar -xJvf |
Extract xz-compressed tar |
*.zip |
unzip |
Extract ZIP archive |
*.rar |
unrar x |
Extract RAR archive |
*.gz (compressed only, not tar) |
gzip -d or gunzip |
Decompress gzip file |
*.bz2 (compressed only, not tar) |
bzip2 -d or bunzip2 |
Decompress bzip2 file |
Best Practices and Notes
- Absolute vs. Relative Paths: Use relative paths (e.g.,
./dir) when creating archives to avoid overwriting critical system directories upon extraction. The-Pparameter preserves absolute paths. - Parameter Order: The primary command (e.g.,
-c) typically comes first; the-fparameter and its filename should be last. - Modern Compression Choice: For scenarios requiring higher compression ratios, the
.tar.xzformat (using-J) is recommended. It offers better compression than.tar.gzand.tar.bz2, though compression takes slightly longer. - Verify Archives: After creating important archives, verify the contents with
tar -tf archive.tar.gz.
Mastering the tar command is a fundamental skill for Linux system administration. By combining different parameters, you can efficiently handle file backup, migration, and distribution tasks.