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.
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.tarAdd Files to an Existing Archive
Use the -r option to append new files without recreating the archive.
tar -rvf archive.tar newfileCompress 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 directoryExtract 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/backupExtract 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 directoryArchive While Excluding Certain Files or Directories
The --exclude option omits paths such as a .git folder.
tar -cvf backup.tar --exclude='.git' /home/userEncrypt an Archive
Pipe the tar output to openssl for AES‑256 encryption.
tar -cvf - directory | openssl aes-256-cbc -salt -out archive.tar.encLimit 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 directorySummary
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
