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

  1. File archiving
  2. Backup creation
  3. File distribution
  4. Data compression
  5. 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

  1. Backup directory:

    tar -czf backup.tar.gz /path/to/dir/
  2. Extract to location:

    tar -xf archive.tar -C /target/
  3. Exclude files:

    tar -czf archive.tar.gz --exclude='*.tmp' dir/

Security Considerations

  1. File permissions
  2. Path traversal
  3. Symbolic links
  4. Compression ratio
  5. Archive validation

Additional Resources

Best Practices

  1. Test archives
  2. Verify contents
  3. Use compression
  4. Document contents
  5. Regular backups

Compression Methods

  1. gzip (fast)
  2. bzip2 (better)
  3. xz (best)
  4. zstd (modern)
  5. lz4 (fastest)

Troubleshooting

  1. Archive errors
  2. Permission issues
  3. Space problems
  4. Corruption
  5. Extraction fails