Master Linux Compression: zip, gzip, bzip2, tar.gz & tar.bz2
This guide explains how to create and extract common Linux compression formats—including zip, gzip, bzip2, tar.gz, and tar.bz2—by using the appropriate command‑line options, showing practical examples for files and directories.
zip, gz, bz2 formats
Linux provides several simple compression utilities. The zip command creates .zip archives, optionally recursing into directories, while unzip extracts them. zip archive.zip file.txt – compress a single file. zip -r archive.zip directory – compress an entire directory (including its contents). unzip archive.zip – extract a .zip archive.
For gzip, the gzip utility produces .gz files. By default it removes the original file; the -c option writes the compressed data to stdout, preserving the source. gzip file.txt – compress and delete the original. gzip -c file.txt > file.txt.gz – keep the original file. gzip -r directory – compress every regular file under a directory (directories themselves are not archived). gunzip file.txt.gz – decompress a .gz file.
For bzip2, the bzip2 command creates .bz2 files. It cannot archive directories directly. bzip2 file.txt – compress and delete the original. bzip2 -k file.txt – compress while keeping the original. bzip2 -d file.txt.bz2 or bunzip2 file.txt.bz2 – decompress, optionally keeping the compressed file with -k.
tar.gz and tar.bz2 formats
Because gzip and bzip2 cannot archive directories, the common practice is to first bundle files with tar and then compress the resulting tarball. tar -cvf archive.tar file1 file2 – create an uncompressed tar archive. tar -zcvf archive.tar.gz directory – create a .tar.gz archive (tar + gzip). tar -zxvf archive.tar.gz – extract a .tar.gz archive. tar -ztvf archive.tar.gz – list contents without extracting. tar -jcvf archive.tar.bz2 directory – create a .tar.bz2 archive (tar + bzip2). tar -jxvf archive.tar.bz2 – extract a .tar.bz2 archive. tar -jtvf archive.tar.bz2 – list contents of a .tar.bz2 archive.
These combined formats are the most frequently used for packaging and distributing source code, binaries, and other collections on Linux systems. While knowing the basic .zip, .gz, and .bz2 commands is useful, mastering tar.gz and tar.bz2 is essential for everyday Linux administration.
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.
