Linux Compression & Archiving Essentials: compress, gzip, bzip2, xz, zip, tar, cpio
This guide reviews the most common Linux compression and archiving utilities—compress, gzip, bzip2, xz, zip, tar, and cpio—detailing their file formats, key options for compressing, decompressing, preserving original files, displaying progress, and adjusting compression levels with practical command examples.
compress
The compress utility creates .Z files, deleting the original file after compression. Use -d to decompress, which also removes the .Z file and restores the original. uncompress works like -d but supports tab‑completion. To view decompressed output without creating a file, use zcat X.Z > X or zcat X.Z with -c to print to stdout. Additional flags include -f for forced compression on hard‑linked files, -v for verbose output, and -# to set compression level (higher numbers give better compression but slower speed).
gzip
gzipproduces .gz files. Decompression uses -d (or the gunzip alias) and supports tab‑completion. The command zcat X.gz > X works similarly to the compress example. Use -c to output to stdout, -f to force compression, -v for verbose mode, and -# to adjust compression level (e.g., -1 for fast, -9 for best).
bzip2
The bzip2 tool creates .bz2 files. Decompression uses -d (or bunzip2) with tab‑completion. Use bzcat X.bz2 > X to view output. The -k flag preserves the original file during compression or decompression, a feature not available in compress or gzip. Like the others, -c prints to stdout, -f forces compression, -v shows progress, and -# controls compression level.
xz
xzgenerates .xz files with higher compression ratios than the previous tools. Decompression uses -d and supports tab‑completion. Use xzcat X.xz > X to view output. The -k flag preserves the original file, and the same set of options ( -c, -f, -v, -#) apply, where higher -# values increase compression at the cost of speed.
Packaging Tools: zip and tar
zip creates .zip archives. Use zip -r archive.zip directory/ to recursively add files. You can pipe data into zip (e.g., cat file | zip archive -) and extract with unzip -p archive.zip > file. Options include -p to preview contents and -v for verbose output.
tar bundles files without compression by default. Common flags: -f specifies the archive name (required for most operations). -c creates an archive (e.g., tar -cf archive.tar file1 file2). -t lists archive contents ( tar -tf archive.tar). -x extracts files ( tar -xf archive.tar). -v adds verbose output. -z compresses with gzip ( .tar.gz), -j with bzip2 ( .tar.bz2), and -J with xz ( .tar.xz). -C extracts to a specific directory. --delete removes files from an archive. split -b SIZE -d archive.tar prefix splits a large tar file into chunks; concatenate with cat to reassemble.
cpio (less common)
The cpio utility archives files via redirection and can handle .cpio or .tar extensions. Common options: -o creates an archive (e.g., find ./etc -print | cpio -ov > etc.cpio). -i extracts an archive ( cpio -idv < etc.cpio). -t lists archive contents ( cpio -tv < etc.cpio). -v shows progress. -d creates directories as needed during extraction. -c uses a newer portable format.
Source: https://www.cnblogs.com/along21/p/7382095.html
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.
