Operations 6 min read

Master Advanced tar Techniques: Compression, Incremental Backups, and Encryption

This guide explains the tar command’s advanced capabilities—including adding files to existing archives, using gzip or bzip2 compression, extracting specific files with wildcards, creating incremental backups, excluding paths, encrypting archives, limiting archive size, and directing output to custom directories—providing clear examples for each scenario.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Advanced tar Techniques: Compression, Incremental Backups, and Encryption

Compression and Decompression

The most basic tar usage packs files into an archive and extracts them.

tar -cvf archive.tar file1 file2 ...
tar -xvf archive.tar

Add Files to an Existing Archive

Use the -r option to append new files without recreating the archive.

tar -rvf archive.tar newfile

Compress the Archive

Combine tar with gzip ( .gz) or bzip2 ( .bz2) to produce compressed archives.

tar -cvzf archive.tar.gz directory
tar -cvjf archive.tar.bz2 directory

Extract Only Specific Files

Use --wildcards with a pattern to extract matching files.

tar -xvf archive.tar --wildcards '*.txt'

Create Incremental Backups

The --listed-incremental option records changes since the last backup, allowing incremental archives.

tar --create --file=backup.tar --listed-incremental=snapshot.snar /directory/to/backup

Extract Specific Files from an Archive

Combine --extract with --wildcards to pull only selected files.

tar -xvf archive.tar --wildcards '*.txt'

Archive and Compress to a Specific Directory

Use -C to change to a target directory before creating the archive.

tar -cvzf /path/to/destination/archive.tar.gz -C /path/to/source directory

Archive While Excluding Certain Files or Directories

The --exclude option omits paths such as a .git folder.

tar -cvf backup.tar --exclude='.git' /home/user

Encrypt an Archive

Pipe the tar output to openssl for AES‑256 encryption.

tar -cvf - directory | openssl aes-256-cbc -salt -out archive.tar.enc

Limit Archive File Size

The --tape-length option caps the archive size; excess data is written to a new file.

tar -cvf backup.tar --tape-length=1G directory

Summary

The article covers several advanced tar usages: adding files to existing archives, compressing with gzip or bzip2, extracting selective files, creating incremental backups, excluding paths, encrypting archives, limiting archive size, and directing output to specific directories. These techniques help Linux users manage files more flexibly and efficiently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-lineBackupFile Compressiontar
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.