tar
Overview
The tar
(Tape Archive) command creates, extracts, and manipulates archive files. It can combine multiple files into a single archive and optionally compress it.
Syntax
tar [options] [archive] [files...]
Common Options
Option | Description |
---|---|
-c |
Create archive |
-x |
Extract archive |
-t |
List contents |
-f |
Specify file |
-v |
Verbose output |
-z |
Use gzip |
-j |
Use bzip2 |
-J |
Use xz |
-p |
Preserve permissions |
-r |
Append files |
--delete |
Delete from archive |
--exclude |
Exclude pattern |
Archive Types
Extension | Description |
---|---|
.tar |
Uncompressed |
.tar.gz |
Gzip compressed |
.tgz |
Gzip compressed |
.tar.bz2 |
Bzip2 compressed |
.tbz2 |
Bzip2 compressed |
.tar.xz |
XZ compressed |
.txz |
XZ compressed |
Key Use Cases
- File archiving
- Backup creation
- File distribution
- Data compression
- System backup
Examples with Explanations
Example 1: Create Archive
tar -czf archive.tar.gz files/
Create gzipped archive
Example 2: Extract Archive
tar -xf archive.tar
Extract archive
Example 3: List Contents
tar -tvf archive.tar
List archive contents
Common Usage Patterns
Backup directory:
tar -czf backup.tar.gz /path/to/dir/
Extract to location:
tar -xf archive.tar -C /target/
Exclude files:
tar -czf archive.tar.gz --exclude='*.tmp' dir/
Security Considerations
- File permissions
- Path traversal
- Symbolic links
- Compression ratio
- Archive validation
Additional Resources
Best Practices
- Test archives
- Verify contents
- Use compression
- Document contents
- Regular backups
Compression Methods
- gzip (fast)
- bzip2 (better)
- xz (best)
- zstd (modern)
- lz4 (fastest)
Troubleshooting
- Archive errors
- Permission issues
- Space problems
- Corruption
- Extraction fails