|

Linux CLI 39 ๐Ÿง tar and zip commands

tar -cvf files.tar 1.txt 2.txt
tar -tf files.tar
tar -xvf files.tar
tar -cvfz files.tar.gz 1.txt 2.txt
tar -xvfz files.tar.gz
tar -cvfj files.tar.bz2 1.txt 2.txt
tar -xvfj files.tar.bz2

zip -r 1.zip 1/
zipinfo 1.zip
unzip -v 1.zip

tar and zip both bundle multiple files into a single archive, but they come from different worlds. tar is the Unix-native archiver โ€” it bundles first, then optionally compresses with gzip or bzip2. zip is the cross-platform format โ€” it bundles and compresses in one step, and Windows, macOS, and Linux can all open it.

Key point: tar by itself does not compress โ€” it just bundles. Add z (gzip) or j (bzip2) to compress. zip always compresses. Use tar for Unix backups and long-term archives; use zip when you need compatibility with other operating systems.


a – tar command

tar is a tool for creating and manipulating archive files. It can pack multiple files into a single file and even compress them. You can also list the files in the archive and, of course, extract them.

The name: tar stands for Tape ARchive โ€” it was originally designed to write backups to magnetic tape.

Syntax:

tar [options] [archive] [files...]

Common options:

OptionPurpose
cCreate an archive from files and/or directories
xExtract an archive
tList the contents of an archive
vVerbose mode โ€” show details
rUpdate or add files to an existing archive
WVerify the integrity of an archive
zUse gzip compression
jUse bzip2 compression
JUse xz compression
fSpecify the filename of the archive
AConcatenate multiple archive files into one
CChange to a directory first
pPreserve permissions
--excludeSkip matching files

The three modes at a glance:

ModeOptionPurpose
CreatecMake a new archive
ExtractxUnpack an archive
ListtShow contents without extracting

Tip: You almost always need f (filename) with these โ€” otherwise tar reads or writes to a tape device. The order is usually tar -cvf archive.tar files....


b – tar command examples

Here’s how the three modes look in practice, with and without compression:

CommandDescription
tar -cvf files.tar 1.txt 2.txtCreates an archive
tar -tf files.tarLists contents of an archive
tar -xvf files.tarExtracts files from an archive
tar -cvfz files.tar.gz 1.txt 2.txtCreates a compressed tarball with gzip
tar -xvfz files.tar.gzExtracts a compressed tarball with gzip
tar -cvfj files.tar.bz2 1.txt 2.txtCreates a compressed tarball with bzip2
tar -xvfj files.tar.bz2Extracts a compressed tarball with bzip2

Examples:

# Create an uncompressed archive
$ tar -cvf files.tar 1.txt 2.txt
1.txt
2.txt

$ ls -l files.tar
-rw-r--r-- 1 kronos kronos 10240 Jan 15 10:00 files.tar

# List contents
$ tar -tf files.tar
1.txt
2.txt

# List with details (like ls -l)
$ tar -tvf files.tar
-rw-r--r-- kronos/kronos    1024 2024-01-15 10:00 1.txt
-rw-r--r-- kronos/kronos    2048 2024-01-15 10:00 2.txt

# Extract
$ tar -xvf files.tar
1.txt
2.txt

# Create with gzip compression
$ tar -cvfz files.tar.gz 1.txt 2.txt
1.txt
2.txt

$ ls -l files.tar.gz
-rw-r--r-- 1 kronos kronos 2048 Jan 15 10:00 files.tar.gz

# Extract gzip tarball
$ tar -xvfz files.tar.gz
1.txt
2.txt

# Create with bzip2 compression
$ tar -cvfj files.tar.bz2 1.txt 2.txt
1.txt
2.txt

# Extract bzip2 tarball
$ tar -xvfj files.tar.bz2
1.txt
2.txt

# Archive a whole directory
$ tar -cvf project.tar project/
project/
project/file1.txt
project/file2.txt
project/subdir/
project/subdir/file3.txt

# Extract to a specific directory
$ mkdir /tmp/restore
$ tar -xvf project.tar -C /tmp/restore

# Add a file to an existing archive
$ tar -rvf files.tar 3.txt
3.txt

# Verify integrity
$ tar -Wvf files.tar

# Concatenate two archives
$ tar -Avf combined.tar files.tar more.tar

# Exclude files
$ tar -cvf backup.tar --exclude='*.log' project/

# Use xz compression (best ratio, slower)
$ tar -cvfJ files.tar.xz 1.txt 2.txt

Modern style: You can also write tar -czvf or tar -cavf (with a for auto-detect by extension). GNU tar detects the compression from the extension, so tar -caf files.tar.gz dir/ works too.

Directory archiving โ€” important detail:

# Archive the directory itself
$ tar -cvf project.tar project/
project/
project/file1.txt
...

# Archive only the CONTENTS (no leading directory)
$ tar -cvf project.tar -C project .
./
./file1.txt
...

The first version preserves the project/ prefix on extraction. The second extracts into the current directory without creating project/.


c – zip command

zip is used to compress and package files. It can pack multiple files and directories. The extension of the archive file is .zip. Unlike tar, zip compresses as it archives โ€” there’s no separate step.

Why use zip? Compatibility. Windows, macOS, and Linux all open .zip files without extra software. .tar.gz is Unix-centric.

CommandDescription
zip -r 1.zip 1/Creates a Zip archive (-r = recursive)
zipinfo 1.zipLists content of a Zip archive
unzip -v 1.zipExtracts files from an archive (-v = verbose)
zip -r -1 output.zip /Creates an archive with compression level 1
--encryptAdds a password to the archive

Compression levels: 0โ€“9, default is 6.

  • 0 = no compression (store only)
  • 1 = fastest, least compression
  • 9 = slowest, best compression
  • 6 = default

Examples:

# Create a zip of a directory
$ zip -r 1.zip 1/
  adding: 1/ (stored 0%)
  adding: 1/file1.txt (deflated 60%)
  adding: 1/file2.txt (deflated 55%)
  adding: 1/subdir/ (stored 0%)
  adding: 1/subdir/file3.txt (deflated 70%)

# Create from multiple files
$ zip archive.zip file1.txt file2.txt
  adding: file1.txt (deflated 60%)
  adding: file2.txt (deflated 55%)

# List contents
$ zipinfo 1.zip
Archive:  1.zip
Zip file size: 2048 bytes, number of entries: 5
drwxr-xr-x  3.0 unx        0 bx stor 24-Jan-15 10:00 1/
-rw-r--r--  3.0 unx     1024 tx defN 24-Jan-15 10:00 1/file1.txt
-rw-r--r--  3.0 unx     2048 tx defN 24-Jan-15 10:00 1/file2.txt
drwxr-xr-x  3.0 unx        0 bx stor 24-Jan-15 10:00 1/subdir/
-rw-r--r--  3.0 unx     4096 tx defN 24-Jan-15 10:00 1/subdir/file3.txt
5 files, 7168 bytes uncompressed, 2800 bytes compressed:  61.0%

# Extract
$ unzip 1.zip
Archive:  1.zip
   creating: 1/
  inflating: 1/file1.txt
  inflating: 1/file2.txt
   creating: 1/subdir/
  inflating: 1/subdir/file3.txt

# Extract verbosely
$ unzip -v 1.zip
Archive:  1.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 2024-01-15 10:00 00000000  1/
    1024  Defl:N      410  60% 2024-01-15 10:00 a1b2c3d4  1/file1.txt
...

# Create with a specific compression level
$ zip -r -1 fast.zip 1/
$ zip -r -9 best.zip 1/

# Add a password (encrypted)
$ zip -r --encrypt secure.zip 1/
Enter password:
Verify password:
  adding: 1/ (stored 0%)
  ...

# Extract a password-protected zip
$ unzip secure.zip
Archive:  secure.zip
[secure.zip] 1/file1.txt password:
  inflating: 1/file1.txt

# Extract to a specific directory
$ unzip 1.zip -d /tmp/restore

# Extract a single file
$ unzip 1.zip 1/file1.txt

# Test integrity
$ unzip -t 1.zip
Archive:  1.zip
    testing: 1/                       OK
    testing: 1/file1.txt              OK
    testing: 1/file2.txt              OK
No errors detected in compressed data of 1.zip.

# Update existing zip (add/replace changed files)
$ zip -r -u 1.zip 1/

# Delete a file from a zip
$ zip -d 1.zip 1/file1.txt
deleting: 1/file1.txt

# Exclude files
$ zip -r 1.zip 1/ -x '*.log'

Common zip options:

OptionPurpose
-rRecursive (include directories)
-1 โ€ฆ -9Compression level
-e / --encryptEncrypt with a password
-dDelete entries from the archive
-uUpdate (add/replace changed files)
-x PATTERNExclude files
-jJunk paths (don’t store directories)
-qQuiet mode
-mMove (delete source files after zipping)

Common unzip options:

OptionPurpose
-lList contents (short)
-vList contents (verbose)
-d DIRExtract to a directory
-tTest integrity
-oOverwrite without prompting
-nNever overwrite existing files
-qQuiet mode
-pExtract to stdout

Complete Example Session

# ============================================
# PART 1: TAR โ€” CREATE
# ============================================

# Uncompressed
$ tar -cvf files.tar 1.txt 2.txt
1.txt
2.txt

$ ls -l files.tar
-rw-r--r-- 1 kronos kronos 10240 Jan 15 10:00 files.tar

# Gzip compressed
$ tar -cvfz files.tar.gz 1.txt 2.txt
1.txt
2.txt

$ ls -l files.tar.gz
-rw-r--r-- 1 kronos kronos 2048 Jan 15 10:00 files.tar.gz

# Bzip2 compressed
$ tar -cvfj files.tar.bz2 1.txt 2.txt
1.txt
2.txt

$ ls -l files.tar.bz2
-rw-r--r-- 1 kronos kronos 1800 Jan 15 10:00 files.tar.bz2

# ============================================
# PART 2: TAR โ€” LIST
# ============================================

$ tar -tf files.tar
1.txt
2.txt

$ tar -tvf files.tar
-rw-r--r-- kronos/kronos    1024 2024-01-15 10:00 1.txt
-rw-r--r-- kronos/kronos    2048 2024-01-15 10:00 2.txt

# ============================================
# PART 3: TAR โ€” EXTRACT
# ============================================

$ tar -xvf files.tar
1.txt
2.txt

$ tar -xvfz files.tar.gz
1.txt
2.txt

$ tar -xvfj files.tar.bz2
1.txt
2.txt

# ============================================
# PART 4: TAR โ€” DIRECTORY
# ============================================

$ tar -czvf project.tar.gz project/
project/
project/file1.txt
project/file2.txt
project/subdir/
project/subdir/file3.txt

# Extract to a specific location
$ mkdir /tmp/restore
$ tar -xzvf project.tar.gz -C /tmp/restore

# ============================================
# PART 5: TAR โ€” EXCLUDE
# ============================================

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

# ============================================
# PART 6: ZIP โ€” CREATE
# ============================================

$ zip -r 1.zip 1/
  adding: 1/ (stored 0%)
  adding: 1/file1.txt (deflated 60%)
  adding: 1/file2.txt (deflated 55%)
  adding: 1/subdir/ (stored 0%)
  adding: 1/subdir/file3.txt (deflated 70%)

# ============================================
# PART 7: ZIP โ€” LIST
# ============================================

$ zipinfo 1.zip
Archive:  1.zip
Zip file size: 2048 bytes, number of entries: 5
drwxr-xr-x  3.0 unx        0 bx stor 24-Jan-15 10:00 1/
-rw-r--r--  3.0 unx     1024 tx defN 24-Jan-15 10:00 1/file1.txt
...

# ============================================
# PART 8: ZIP โ€” EXTRACT
# ============================================

$ unzip 1.zip
Archive:  1.zip
   creating: 1/
  inflating: 1/file1.txt
  inflating: 1/file2.txt
  creating: 1/subdir/
  inflating: 1/subdir/file3.txt

$ unzip -v 1.zip
Archive:  1.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
...

# ============================================
# PART 9: ZIP โ€” COMPRESSION LEVELS
# ============================================

$ zip -r -1 fast.zip 1/        # fastest
$ zip -r -9 best.zip 1/        # best
$ zip -r 1.zip 1/              # default (6)

# ============================================
# PART 10: ZIP โ€” ENCRYPTION
# ============================================

$ zip -r --encrypt secure.zip 1/
Enter password:
Verify password:
  adding: 1/ (stored 0%)
  adding: 1/file1.txt (deflated 60%)
  ...

$ unzip secure.zip
Archive:  secure.zip
[secure.zip] 1/file1.txt password:
  inflating: 1/file1.txt

# ============================================
# PART 11: COMPARE SIZES
# ============================================

$ ls -l files.tar files.tar.gz files.tar.bz2 1.zip
-rw-r--r-- 1 kronos kronos 10240 Jan 15 10:00 files.tar
-rw-r--r-- 1 kronos kronos  2048 Jan 15 10:00 files.tar.gz
-rw-r--r-- 1 kronos kronos  1800 Jan 15 10:00 files.tar.bz2
-rw-r--r-- 1 kronos kronos  2200 Jan 15 10:00 1.zip

# tar alone: biggest
# tar.gz: smaller
# tar.bz2: smallest
# zip: comparable to tar.gz

Quick Reference

tar โ€” Modes

OptionPurpose
cCreate
xExtract
tList
rAppend/update
AConcatenate archives
WVerify integrity

tar โ€” Compression

OptionCompression
(none)No compression
zgzip (.tar.gz / .tgz)
jbzip2 (.tar.bz2 / .tbz2)
Jxz (.tar.xz)
aAuto-detect by extension

tar โ€” Common Modifiers

OptionPurpose
f FILEArchive filename
vVerbose
C DIRChange to directory
pPreserve permissions
--exclude=PATExclude matching files

tar โ€” Recipes

CommandPurpose
tar -cvf a.tar filesCreate uncompressed
tar -tf a.tarList
tar -xvf a.tarExtract
tar -czvf a.tar.gz filesCreate gzip tarball
tar -xzvf a.tar.gzExtract gzip tarball
tar -cjvf a.tar.bz2 filesCreate bzip2 tarball
tar -xjvf a.tar.bz2Extract bzip2 tarball
tar -cJvf a.tar.xz filesCreate xz tarball
tar -xJvf a.tar.xzExtract xz tarball
tar -xvf a.tar -C /tmpExtract to a directory
tar -rvf a.tar newfileAppend a file
tar -czvf a.tar.gz dir/ --exclude='*.log'Exclude files

zip / unzip

CommandPurpose
zip -r a.zip dir/Create recursive zip
zip a.zip f1 f2Create from files
zip -1 a.zip dir/Fastest compression
zip -9 a.zip dir/Best compression
zip -e a.zip dir/Encrypt with password
zip -d a.zip fileDelete an entry
zip -u a.zip dir/Update changed files
zip -x '*.log' -r a.zip dir/Exclude files
zipinfo a.zipList contents
unzip a.zipExtract
unzip -v a.zipExtract verbosely
unzip -l a.zipList (short)
unzip -d DIR a.zipExtract to a directory
unzip -t a.zipTest integrity
unzip -o a.zipOverwrite without prompt

tar vs zip

Aspecttarzip
OriginUnixDOS/Windows
CompressionSeparate (z, j, J)Built-in
Common extension.tar, .tar.gz, .tar.bz2.zip
Preserves permissionsYesLimited
Preserves symlinksYesLimited
Cross-platformUnix-centricUniversal
StreamingYesLimited
Best forBackups, UnixSharing with Windows

Best Practices

โœ… Do This:

# Use tar for Unix backups (permissions preserved)
tar -czvf backup.tar.gz /home/kronos/  # โœ…

# Use zip for cross-platform sharing
zip -r share.zip documents/            # โœ…

# Use -C to extract to a specific place
tar -xzvf a.tar.gz -C /tmp/restore     # โœ…

# Check contents before extracting
tar -tf a.tar.gz                       # โœ…
unzip -l a.zip                         # โœ…

# Exclude junk when archiving projects
tar -czvf p.tar.gz project/ --exclude='node_modules'  # โœ…

# Encrypt sensitive zips
zip -e secure.zip secrets/             # โœ…

# Test integrity after transfer
tar -Wvf a.tar                         # โœ…
unzip -t a.zip                         # โœ…

# Use xz for the smallest tarballs
tar -cJvf a.tar.xz bigdir/             # โœ…

โŒ Don’t Do This:

# Don't forget -f with tar
tar -cv files/                         # โŒ writes to tape!

# Don't extract without checking
tar -xvf unknown.tar                   # โŒ may overwrite files

# Don't use zip for Unix-only backups
zip -r backup.zip /etc/                # โŒ loses permissions

# Don't forget -r with zip on directories
zip a.zip dir/                         # โŒ "nothing to do"

# Don't create .tar.gz without -z
tar -cvf a.tar.gz files                # โŒ misnamed

# Don't store passwords in scripts
zip -P secret a.zip files              # โŒ visible in ps

# Don't zip already-compressed files
zip -r a.zip videos/                   # โŒ no gain

Common Pitfalls

PitfallProblemSolution
Forgot -ftar writes to tapeAlways use -f
Wrong option orderConfusing errorstar -cvf a.tar files
.tar.gz without -zNot actually compressedAdd z
zip skips directories“nothing to do”Add -r
Extract overwrites filesData lossUse -C to a temp dir
Permissions lost with zipScripts brokenUse tar instead
Password visibleSecurity leakUse zip -e (prompts)
Huge tarballSlow transferUse -J (xz) or split

Real-World Examples

1. Back Up Your Home Directory

$ tar -czvf home-backup-$(date +%F).tar.gz /home/kronos/
...
$ ls -lh home-backup-*.tar.gz
-rw-r--r-- 1 kronos kronos 4.2G Jan 15 10:00 home-backup-2024-01-15.tar.gz

2. Extract a Downloaded Tarball

$ wget 'https://example.com/project-1.0.tar.gz'
$ tar -xzvf project-1.0.tar.gz
$ cd project-1.0/
$ ./configure && make && sudo make install

3. Archive a Project Excluding Junk

$ tar -czvf project.tar.gz project/ \
    --exclude='node_modules' \
    --exclude='.git' \
    --exclude='*.log'

4. Extract a Tarball to a Specific Place

$ mkdir -p /opt/apps
$ tar -xzvf app.tar.gz -C /opt/apps

5. Share Files with a Windows User

$ zip -r report.zip report/
$ # send report.zip โ€” Windows opens it directly

6. Compress a Directory Recursively with Zip

$ zip -r website.zip /var/www/html/
  adding: var/www/html/ (stored 0%)
  adding: var/www/html/index.html (deflated 65%)
  ...

7. Add a File to an Existing tar

$ tar -rvf backup.tar newfile.txt
newfile.txt

8. Inspect an Archive Before Extracting

$ tar -tvf suspicious.tar.gz
-rw-r--r-- root/root  1024 2024-01-15 10:00 etc/passwd
drwxr-xr-x root/root     0 2024-01-15 10:00 etc/
# โœ… Safe to extract

9. Test a Zip Before Extracting

$ unzip -t archive.zip
Archive:  archive.zip
    testing: file1.txt                OK
    testing: file2.txt                OK
No errors detected in compressed data of archive.zip.

10. Create an Encrypted Zip

$ zip -e private.zip documents/
Enter password:
Verify password:
  adding: documents/ (stored 0%)
  adding: documents/secret.txt (deflated 60%)

11. Split a Large Tarball into Pieces

$ tar -czvf - bigdir/ | split -b 1G - bigdir.tar.gz.part-
$ ls bigdir.tar.gz.part-*
bigdir.tar.gz.part-aa  bigdir.tar.gz.part-ab  ...

# Reassemble
$ cat bigdir.tar.gz.part-* | tar -xzvf -

12. Extract a Single File from a tarball

$ tar -xzvf backup.tar.gz path/to/single-file.txt
path/to/single-file.txt

13. List Contents Without Extracting

$ tar -tf archive.tar.gz
$ unzip -l archive.zip
$ zipinfo archive.zip

Visual: tar vs zip Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  tar                         โ”‚
โ”‚                                              โ”‚
โ”‚  files โ”€โ”€โ–บ tar bundle โ”€โ”€โ–บ tarball            โ”‚
โ”‚                โ”‚                             โ”‚
โ”‚                โ”œโ”€โ”€โ–บ (optionally compress)    โ”‚
โ”‚                โ”‚      gzip (z) โ†’ .tar.gz     โ”‚
โ”‚                โ”‚      bzip2 (j) โ†’ .tar.bz2   โ”‚
โ”‚                โ”‚      xz (J) โ†’ .tar.xz       โ”‚
โ”‚                โ”‚                             โ”‚
โ”‚                โ””โ”€โ”€โ–บ extract: tar -xvf        โ”‚
โ”‚                                              โ”‚
โ”‚  Two steps: bundle, then compress            โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  zip                         โ”‚
โ”‚                                              โ”‚
โ”‚  files โ”€โ”€โ–บ zip bundle + compress โ”€โ”€โ–บ .zip    โ”‚
โ”‚                                              โ”‚
โ”‚  One step: bundles and compresses together   โ”‚
โ”‚                                              โ”‚
โ”‚  Extract: unzip a.zip                        โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

CommandPurposeExample
tar -cvf a.tar filesCreate uncompressedtar -cvf a.tar 1.txt 2.txt
tar -tf a.tarList contentstar -tf a.tar
tar -xvf a.tarExtracttar -xvf a.tar
tar -czvf a.tar.gz filesCreate with gziptar -czvf a.tar.gz dir/
tar -xzvf a.tar.gzExtract gziptar -xzvf a.tar.gz
tar -cjvf a.tar.bz2 filesCreate with bzip2tar -cjvf a.tar.bz2 dir/
tar -xjvf a.tar.bz2Extract bzip2tar -xjvf a.tar.bz2
tar -cJvf a.tar.xz filesCreate with xztar -cJvf a.tar.xz dir/
tar -xJvf a.tar.xzExtract xztar -xJvf a.tar.xz
tar -xvf a.tar -C /tmpExtract to dirtar -xzvf a.tar.gz -C /tmp
tar -rvf a.tar newfileAppend a filetar -rvf a.tar 3.txt
zip -r a.zip dir/Create recursive zipzip -r 1.zip 1/
zipinfo a.zipList contentszipinfo 1.zip
unzip a.zipExtractunzip 1.zip
unzip -v a.zipExtract verboseunzip -v 1.zip
unzip -d DIR a.zipExtract to dirunzip 1.zip -d /tmp
unzip -t a.zipTest integrityunzip -t 1.zip
zip -e a.zip dir/Encrypted zipzip -e a.zip dir/
zip -1 / -9Compression levelzip -9 a.zip dir/

Key takeaways:

  • tar bundles files into a single archive โ€” it does not compress by itself
  • Add z (gzip), j (bzip2), or J (xz) to compress while archiving
  • The three tar modes are c (create), x (extract), t (list) โ€” always with f
  • zip bundles and compresses in one step โ€” extension is .zip
  • Use zip -r for directories, unzip -d to extract elsewhere, zipinfo or unzip -l to inspect
  • Compression levels: 1 = fastest, 9 = best, default 6 (both tar’s gzip and zip)
  • Use tar for Unix backups (preserves permissions, symlinks, ownership) and zip for sharing with Windows/macOS
  • Use -C DIR with tar to extract to a specific location
  • Use --exclude (tar) or -x (zip) to skip files
  • Always inspect an archive (tar -t, unzip -l, zipinfo) before extracting untrusted files

Remember: tar is the Unix standard โ€” bundle first, compress optionally. zip is the universal format โ€” bundle and compress together. For backups, use tar -czvf. For cross-platform sharing, use zip -r. For the smallest archives, reach for tar -cJvf (xz). Always list contents before extracting, use -C to control where files land, and keep compression levels in mind. Master both, and you can bundle, compress, and move anything.


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!