Common Linux Compression and Archiving Tools: Commands and Usage
This article provides a comprehensive overview of the most frequently used Linux compression utilities (zip, gzip, bzip2, xz) and the tar archiving command, detailing their syntax, key options, how to combine them, and practical examples for compressing and extracting files and directories.
This article summarizes the common compression tools available on Linux— zip, gzip, bzip2, xz —and the tar archiving command, covering their basic commands, special parameters, and how to combine them for packaging and compression.
The primary compression commands and their associated file extensions are: zip →
.zip gzip→
.gz bzip2→
.bz2 xz→ .xz Packaging commands include tar (producing .tar) and its combinations with compression formats, such as .tar.gz / .tgz, .tar.bz2, and .tar.xz. The compression ratio is calculated as (compressed size) / (original size).
Note that gzip, bzip2, and xz can only compress single files; to compress directories you must use tar together with one of these tools or use zip, which can handle directories directly.
gzip
gzipcompresses a file in place, creating <file_name>.gz. Use the -v flag to display the compression ratio. gzip -v <file_name> Decompress with:
gzip -d <file_name>.gz gunzip <file_name>.gzbzip2
bzip2also compresses in place, producing <file_name>.bz2. The -v flag shows the compression ratio. bzip2 -v <file_name> Decompress with:
bzip2 -d <file_name>.bz2 bunzip2 <file_name>.bz2xz
xzcompresses in place, creating <file_name>.xz. Use -v to see the ratio and -l to list compression details.
xz -v <file_name> xz -l <file_name>.xzDecompress with:
xz -d <file_name>.xzzip
zipdoes not overwrite the original files. To compress a single file: zip <compressed>.zip <file_name> To compress multiple files: zip <compressed>.zip a.txt b.txt c.txt To compress a directory recursively: zip -r dir.zip <dir_name> Decompress with:
unzip <compressed>.ziptar
taris a packaging command that can also compress when combined with gzip, bzip2, or xz. Core options include: -c: create archive -t: list archive contents -x: extract archive -z: filter through
gzip -j: filter through
bzip2 -J: filter through
xz -f: specify archive file name -v: verbose output -C: change to directory before extracting
Examples:
tar -cvf target.tar a.txt b.txt c.txt tar -cvf target.tar dir1 dir2 tar -cvf target.tar dir1 dir2 a.txt b.txtPack and compress with gzip: tar -cvz -f target.tar.gz dir1 dir2 a.txt Pack and compress with bzip2: tar -cvj -f target.tar.bz2 dir1 dir2 a.txt Pack and compress with xz: tar -cvJ -f target.tar.xz dir1 dir2 a.txt List contents:
tar -tvz -f target.tar.gz tar -tvJ -f target.tar.xzExtract: tar -xvz -f target.tar.gz Extract to a specific directory using -C: tar -xvz -f target.tar.gz -C ~/test In summary, tar can bundle any files or directories into a single archive; by default extraction dumps the contents into the current directory, but the -C option allows extraction to a chosen path.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
