Using zip, unzip, and tar for Compression and Archiving on Linux
This guide explains how to install and use the zip, unzip, and tar commands on Linux to compress files, create archives, list contents, extract data, and apply gzip, bzip2, or xz compression with various options and examples.
1. zip compression tool
Install the zip package if it is missing: yum install -y zip . Compress a single file with zip 1.txt.zip 1.txt (adds 1.txt, deflated 74%). Compress a directory recursively with zip -r test.zip test . List archive contents using unzip -l test.zip . The original files remain after compression.
2. unzip extraction
Install unzip if needed: yum install -y unzip . Extract an archive with unzip 1.txt.zip . To specify an extraction directory, use unzip test.zip -d /tmp/test . Overwrite prompts can be answered with A for all or N for none.
3. tar packaging
The tar command bundles files and directories into a single archive. Basic syntax: tar -cvf archive.tar files... to create, tar -xvf archive.tar to extract, and tar -tvf archive.tar to list contents. Options include -z for gzip, -j for bzip2, -J for xz compression, -v for verbose, and --exclude to omit specific files.
Examples:
Create a plain tar: tar -cvf test.tar test 1.txt 2.txt
Create a tar.gz (gzip) archive: tar -zcvf test.tar.gz test 1.txt 2.txt
Create a tar.bz2 (bzip2) archive: tar -jcvf test.tar.bz2 test 1.txt 2.txt
Create a tar.xz (xz) archive: tar -Jcvf test.tar.xz test 1.txt 2.txt
Extract a gzip-compressed archive: tar -zxvf test.tar.gz
Extract a bzip2-compressed archive: tar -jxvf test.tar.bz2
List contents of any archive: tar -tf test.tar.gz
Exclude files while creating: tar -cvf test.tar --exclude 3.txt test 1.txt 2.txt
All commands preserve the original files; the archives are created in the same directory unless a different path is specified.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.