How To Make Tar File In Linux

How To Make Tar File In Linux

Reading time1 min
#Linux#Backup#Compression#Tar#Archiving#Encryption

Mastering the Art of Creating Secure and Efficient tar Files in Linux

Forget the simple tar cvf commands—discover how combining compression and advanced options can transform your archive game, ensuring data integrity and speed like a Linux pro.

If you’re a Linux user, chances are you’ve used tar at some point. But how often do you stop to think about how to create tar files securely and efficiently? Simply tarring up files without optimizing can lead to bloated archives, slow transfers, or even loss of important file metadata.

In this post, I’ll walk you through actionable tips on mastering tar files in Linux — from basics to advanced options — helping you create archives that are compact, secure, and preserve everything you care about.


Why Care About Efficient & Secure tar Files?

  • Optimized file size saves storage space and bandwidth.
  • Preserved metadata (permissions, timestamps) ensures files behave correctly when restored.
  • Data integrity avoids corrupted archives.
  • Speed & performance matter when dealing with large datasets or backups.

Basic tar Syntax Recap

The classic way to create an archive is:

tar cvf archive.tar /path/to/folder
  • c — create new archive
  • v — verbose output (optional)
  • f — filename follows

While this works fine for bundling files, it creates an uncompressed .tar archive. Let’s improve it.


Step 1: Use Compression for Smaller Archives

gzip Compression

Add z option for gzip compression:

tar czvf archive.tar.gz /path/to/folder
  • z — invoke gzip compression on the fly
  • Resulting file: .tar.gz
Pros:
  • Fast compression & decompression
  • Widely supported
Cons:
  • Compression ratio not the best compared to newer algorithms

bzip2 Compression

Use j for better compression ratio at cost of speed:

tar cjvf archive.tar.bz2 /path/to/folder

xz Compression (Best Compression)

Use J for xz compression:

tar cJvf archive.tar.xz /path/to/folder

Pro Tip: If time is a constraint, gzip offers good speed; if storage is a premium, xz compresses best but takes longer.


Step 2: Preserve File Permissions & Metadata

Tar usually preserves most metadata by default (ownership, permissions). But sometimes it helps to explicitly specify options or ensure you're running as a user with proper permissions.

For maximum safety use:

tar czvpf archive.tar.gz /path/to/folder
  • p — preserve permissions (important when extracting as root)

Checking your extracted files inherit all permissions can save headaches later.


Step 3: Verify Archive Integrity with Checksums

Tar itself doesn’t have built-in integrity verification beyond errors during creation and extraction. To verify integrity:

  1. Create MD5 or SHA256 checksum of archive after creation:
sha256sum archive.tar.gz > archive.tar.gz.sha256
  1. Later verify by re-checksum:
sha256sum -c archive.tar.gz.sha256

This helps confirm your tarball wasn’t corrupted during transfer or storage.


Step 4: Exclude Unwanted Files for Efficiency

You might not want everything in the folder. Use --exclude:

tar czvf backup.tar.gz --exclude='*.log' /home/user/project/

Exclude .log files or any big cache directories to keep your archive lean.

You can chain exclusions too:

tar czvf backup.tar.gz --exclude='*.log' --exclude='node_modules' /home/user/project/

Step 5: Splitting Large Tar Files

If dealing with very large tarballs and you need transfers across FAT32 drives or smaller emails/USB drives, split archives into chunks:

  1. Create the tarball without compression first (or use compression pipeline):
tar cvf - /path/to/folder | split -b 500M - backup_part_

This splits the stream into 500MB parts named like backup_part_aa, backup_part_ab.

To reassemble later:

cat backup_part_* | tar xvf -

Bonus: Encrypt Your Tarball for Security

Tar doesn’t encrypt natively but can be combined with tools like gpg:

tar czvf - /path/to/folder | gpg -c -o backup.tar.gz.gpg

This prompts for a passphrase and creates an encrypted archive.

To decrypt and extract:

gpg -d backup.tar.gz.gpg | tar xzvf -

Encrypting archives is essential when backing up sensitive data or transferring over public networks.


Putting It All Together: A Practical Example

Say you want to backup /home/user/projects, exclude logs, compress with xz for max size saving, preserve permissions, then encrypt it all securely.

Command:

tar cJvpf --exclude='*.log' /home/user/projects | gpg -c -o projects_backup.tar.xz.gpg

This single command packs all best practices into one neat step!


Summary Tips for Mastering tar Archives in Linux

TipCommand snippetBenefit
Basic compressed tartar czvf file.tar.gz dir/Quick & easy gzip compression
Better compressiontar cJvf file.tar.xz dir/Best size at slower speed
Preserve permissionsAdd p optionMaintains file ownership info
Exclude unnecessary filesUse --exclude='pattern'Avoid bloat
Verify checksumssha256sum file.tar.* > checksum.sha256Data integrity confirmation
Split large archivesUse split -b size on tar streamManage huge backups
Encrypt backupsPipe through gpgSecure sensitive archives

With these techniques under your belt, your archiving workflow will be faster, safer, and more professional-looking!


Feel free to try these out today on your own systems and share your results below — what creative uses do you have for tar? Happy archiving!