TAR File Archiving Command Examples

We go over the tar for file / directory storage and compression.

TAR File Archiving Command Examples
  • Use these command examples when you are trying to recall the different options available for backup and storage.

Here are twenty practical examples of using the tar command, covering scenarios with and without compression, as well as automatic compression detection where available (primarily in GNU tar via -a / --auto-compress or direct filters). Each example includes the full command followed by a structured parameter breakdown.

# 1. Create an uncompressed tar archive
tar -cvf documents.tar /home/chris/documents

Parameter breakdown

  • -c : Create a new archive.
  • -v : Verbose output (list files being processed).
  • -f : Specify the archive file name (documents.tar).
    No compression is applied.
# 2. Extract an uncompressed tar archive
tar -xvf backup.tar

Parameter breakdown

  • -x : Extract files from the archive.
  • -v : Verbose output.
  • -f : Specify the archive file name.
    No decompression required.
# 3. Create a gzip-compressed archive (explicit)
tar -czvf reports.tar.gz /var/www/reports

Parameter breakdown

  • -c : Create.
  • -z : Compress with gzip.
  • -v : Verbose.
  • -f : Archive file name.
# 4. Extract a gzip-compressed archive (explicit)
tar -xzvf logs-2026.tar.gz

Parameter breakdown

  • -x : Extract.
  • -z : Decompress with gzip.
  • -v : Verbose.
  • -f : Archive file name.
# 5. Create archive with automatic compression via suffix (-a)
tar -cavf config.tar.gz /etc/nginx

Parameter breakdown

  • -c : Create.
  • -a : Auto-compress (uses gzip due to .tar.gz suffix).
  • -v : Verbose.
  • -f : Archive file name.
# 6. Create xz-compressed archive with auto-detection
tar -cavf database.tar.xz /var/lib/mysql

Parameter breakdown

  • -c : Create.
  • -a : Auto-compress (selects xz due to .tar.xz suffix).
  • -v : Verbose.
  • -f : Archive file name.
# 7. Extract archive with auto-detected compression
tar -xavf backup-2026-02.tar.zst

Parameter breakdown

  • -x : Extract.
  • -a : Auto-detect and decompress based on suffix (.zst → zstd).
  • -v : Verbose.
  • -f : Archive file name.
# 8. Create zstd-compressed archive (direct filter, GNU tar ≥1.34)
tar --zstd -cvf projects.tar.zst ~/projects

Parameter breakdown

  • --zstd : Compress with zstandard.
  • -c : Create.
  • -v : Verbose.
  • -f : Archive file name.
# 9. Extract zstd archive using explicit filter
tar --use-compress-program=zstd -xvf large-data.tar.zst

Parameter breakdown

  • --use-compress-program=zstd : Use zstd for decompression.
  • -x : Extract.
  • -v : Verbose.
  • -f : Archive file name.
# 10. Create bzip2-compressed archive (explicit)
tar -cjvf photos.tar.bz2 ~/Pictures

Parameter breakdown

  • -c : Create.
  • -j : Compress with bzip2.
  • -v : Verbose.
  • -f : Archive file name.
# 11. List contents of any compressed archive (auto-detect)
tar -tvf archive.tar.xz

Parameter breakdown

  • -t : List table of contents.
  • -v : Verbose (detailed listing).
  • -f : Archive file name.
    Compression type is auto-detected.
# 12. Extract single file from gzip archive
tar -xzvf web_backup.tar.gz var/www/index.html

Parameter breakdown

  • -x : Extract.
  • -z : Gzip decompression.
  • -v : Verbose.
  • -f : Archive file name.
    Followed by path(s) to extract selectively.
# 13. Create archive excluding patterns
tar -czvf home_backup.tar.gz /home/chris --exclude='*.cache' --exclude='*/tmp'

Parameter breakdown

  • -c : Create.
  • -z : Gzip compression.
  • -v : Verbose.
  • -f : Archive file name.
  • --exclude : Skip matching patterns (can be repeated).
# 14. Create uncompressed tar and pipe to remote host
tar -cvf - /etc | ssh backup@remote.host "cat > /backups/etc-$(date +%Y%m%d).tar"

Parameter breakdown

  • -c : Create.
  • -v : Verbose.
  • -f - : Output to stdout (pipe).
    No compression; suitable for fast local-network transfers.
# 15. Append files to existing uncompressed tar
tar -rvf existing.tar new_report.pdf

Parameter breakdown

  • -r : Append files to archive.
  • -v : Verbose.
  • -f : Archive file name.
    Appending is not supported on most compressed formats.
# 16. Create lz4-compressed archive via --use-compress-program
tar --use-compress-program=lz4 -cvf fast-data.tar.lz4 /tmp/large

Parameter breakdown

  • --use-compress-program=lz4 : Use lz4 compressor.
  • -c : Create.
  • -v : Verbose.
  • -f : Archive file name.
# 17. Extract with preserved permissions (requires root)
sudo tar -xzpf system.tar.gz -C /

Parameter breakdown

  • -x : Extract.
  • -z : Gzip.
  • -p : Preserve permissions/ownership.
  • -f : Archive file name.
  • -C : Change to directory before extracting.
# 18. List archive contents without decompression overhead
tar -tf big-archive.tar.zst

Parameter breakdown

  • -t : List contents.
  • -f : Archive file name.
    Compression is auto-detected but only header is read.
# 19. Create reproducible tar (fixed timestamps, no xattrs)
tar --mtime='2026-01-01 00:00:00' --sort=name --no-xattrs -czvf reproducible.tar.gz ./source

Parameter breakdown

  • --mtime=... : Set fixed modification time.
  • --sort=name : Sort entries for determinism.
  • --no-xattrs : Exclude extended attributes.
  • -c : Create.
  • -z : Gzip.
  • -v : Verbose.
  • -f : Archive file name.
# 20. Create archive with brotli compression via external program
tar --use-compress-program="brotli --best" -cvf docs.tar.br ./documents

Parameter breakdown

  • --use-compress-program="brotli --best" : Use brotli at maximum compression.
  • -c : Create.
  • -v : Verbose.
  • -f : Archive file name (custom extension .br recommended).

These examples demonstrate the flexibility of tar across common use cases. Modern GNU tar versions support -a for automatic compression selection based on file extension, which simplifies workflows when the desired format is reflected in the filename suffix.