Master Linux File Compression: tar, gzip, bzip2, and zip Commands Explained
This guide walks you through the most common Linux compression tools—tar, gzip, bzip2, and zip—showing how to create archives, compress files, and extract contents with clear command examples and practical usage tips.
Compressing and decompressing files is a routine task in Linux, useful for reducing transfer size and saving storage space. The following sections detail the syntax and options for the most widely used compression utilities.
1. tar command
The tar utility bundles multiple files or directories into a single archive, typically with a .tar extension.
1.1 Create an archive
tar -cvf archive.tar file1 file2 file3This creates archive.tar containing file1, file2, and file3. The -c flag creates the archive, and -v shows progress.
1.2 Archive a directory
tar -cvf archive.tar directory/All files and sub‑directories under directory are packed into archive.tar.
1.3 Extract an archive
tar -xvf archive.tarThe -x flag extracts files, with -v displaying progress.
1.4 Extract to a specific directory
tar -xvf archive.tar -C /path/to/directoryThe -C option changes to the target directory before extraction.
1.5 Combine compression with tar
tar -czvf archive.tar.gz file1 file2 file3The -z flag compresses the archive using gzip, producing archive.tar.gz. To extract such an archive:
tar -xzvf archive.tar.gz2. gzip command
gzipcompresses a single file to the .gz format.
2.1 Compress a file
gzip file1Produces file1.gz.
2.2 Decompress a file
gzip -d file1.gzRestores the original file1.
3. bzip2 command
bzip2creates .bz2 archives, offering higher compression ratios.
3.1 Compress a file
bzip2 file1Generates file1.bz2.
3.2 Decompress a file
bzip2 -d file1.bz2Restores the original file1.
4. zip command
zipcreates archives compatible with Windows and other platforms, using the .zip extension.
4.1 Create a zip archive
zip archive.zip file1 file2 file3Compresses the listed files into archive.zip.
4.2 Zip an entire directory
zip -r archive.zip directory/The -r flag recursively adds all files and sub‑directories under directory.
4.3 Extract a zip archive
unzip archive.zipExtracts all contents to the current directory.
4.4 Extract to a specific directory
unzip archive.zip -d /path/to/directoryPlaces the extracted files into the given path.
5. Summary
While many compression tools exist, mastering tar and zip covers most Linux use‑cases. Use tar for bundling large collections, gzip for fast, moderate compression, bzip2 when space savings are critical, and zip for cross‑platform sharing. Choose the tool that best fits your storage and transfer requirements.
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.
