Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| system:linux:file-compression-archiving [2026/01/26 20:41] – removed - external edit (Unknown date) 127.0.0.1 | system:linux:file-compression-archiving [2026/02/01 21:08] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Linux : Compression and Archiving ====== | ||
| + | ===== What is Compression? | ||
| + | |||
| + | **Compression** is a process or technique to reduce the size of a data file. This is achieved using specific algorithms that identify patterns in the data to reduce its size. | ||
| + | |||
| + | Compression is divided into two types: | ||
| + | |||
| + | * **Lossless Compression**: | ||
| + | * **Lossy Compression**: | ||
| + | |||
| + | ===== What is Archiving? ===== | ||
| + | |||
| + | Before exploring compression, | ||
| + | |||
| + | **Archiving** is the process of collecting multiple files or directories into a single file. This archive can then be compressed using compression tools. | ||
| + | |||
| + | ==== tar ==== | ||
| + | |||
| + | **tar** is a common archiving tool on Linux. Example usage: | ||
| + | |||
| + | <code bash> | ||
| + | tar -cvf archive.tar file1 file2 directory | ||
| + | </ | ||
| + | |||
| + | In the example above: | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | |||
| + | <WRAP info> | ||
| + | You can add compression options: < | ||
| + | </ | ||
| + | |||
| + | ===== Compression Tools ===== | ||
| + | |||
| + | Common compression tools on Linux: | ||
| + | |||
| + | ^ Compression Tool ^ Compression Algorithm ^ | ||
| + | | **gzip** | ||
| + | | **bzip2** | Burrows-Wheeler | | ||
| + | | **xz** | ||
| + | | **zip** | ||
| + | |||
| + | ==== gzip ==== | ||
| + | |||
| + | **gzip** is a widely used compression utility using the **DEFLATE** algorithm. | ||
| + | |||
| + | * Compress a file: | ||
| + | <code bash> | ||
| + | gzip filename | ||
| + | </ | ||
| + | → Produces < | ||
| + | |||
| + | * Decompress a file: | ||
| + | <code bash> | ||
| + | gunzip filename.gz | ||
| + | # or | ||
| + | gzip -d filename.gz | ||
| + | </ | ||
| + | |||
| + | * View compression info: | ||
| + | <code bash> | ||
| + | gzip -l filename.gz | ||
| + | </ | ||
| + | |||
| + | ==== bzip2 ==== | ||
| + | |||
| + | **bzip2** uses the **Burrows-Wheeler** algorithm for better compression than gzip. | ||
| + | |||
| + | * Compress a file: | ||
| + | <code bash> | ||
| + | bzip2 filename | ||
| + | </ | ||
| + | → Produces < | ||
| + | |||
| + | * Decompress a file: | ||
| + | <code bash> | ||
| + | bunzip2 filename.bz2 | ||
| + | # or | ||
| + | bzip2 -d filename.bz2 | ||
| + | </ | ||
| + | |||
| + | * View compression info: | ||
| + | <code bash> | ||
| + | bzcat filename.bz2 | wc -c | ||
| + | </ | ||
| + | |||
| + | ==== xz ==== | ||
| + | |||
| + | **xz** uses the **LZMA (Lempel-Ziv-Markov chain algorithm)**. It offers higher compression ratios but is slower and more resource-intensive. | ||
| + | |||
| + | * Compress a file: | ||
| + | <code bash> | ||
| + | xz filename | ||
| + | </ | ||
| + | → Produces < | ||
| + | |||
| + | * Decompress a file: | ||
| + | <code bash> | ||
| + | unxz filename.xz | ||
| + | # or | ||
| + | xz -d filename.xz | ||
| + | </ | ||
| + | |||
| + | * View compression info: | ||
| + | <code bash> | ||
| + | xz -l filename.xz | ||
| + | </ | ||
| + | |||
| + | ==== zip ==== | ||
| + | |||
| + | **zip** is commonly used to compress and archive multiple files. | ||
| + | |||
| + | * Compress files: | ||
| + | <code bash> | ||
| + | zip archive.zip file1 file2 folder1 | ||
| + | </ | ||
| + | |||
| + | * Recursively compress a folder: | ||
| + | <code bash> | ||
| + | zip -r archive.zip folder1 | ||
| + | </ | ||
| + | |||
| + | * Add files to existing zip: | ||
| + | <code bash> | ||
| + | zip -u archive.zip file3 | ||
| + | </ | ||
| + | |||
| + | * Password-protect a zip: | ||
| + | <code bash> | ||
| + | zip -r -e archive.zip folder1 | ||
| + | </ | ||
| + | |||
| + | → Produces < | ||
| + | |||
| + | * Decompress: | ||
| + | <code bash> | ||
| + | unzip archive.zip | ||
| + | </ | ||
| + | |||
| + | * View compression info: | ||
| + | <code bash> | ||
| + | unzip -l archive.zip | ||
| + | </ | ||
| + | |||
| + | ===== Archiving and Compression with tar ===== | ||
| + | |||
| + | **tar** supports built-in compression in one command: | ||
| + | |||
| + | ==== tar + gzip ==== | ||
| + | <code bash> | ||
| + | # Compress | ||
| + | tar -czvf archive.tar.gz directory/ | ||
| + | # Decompress | ||
| + | tar -xzvf archive.tar.gz | ||
| + | </ | ||
| + | |||
| + | ==== tar + bzip2 ==== | ||
| + | <code bash> | ||
| + | # Compress | ||
| + | tar -cjvf archive.tar.bz2 directory/ | ||
| + | # Decompress | ||
| + | tar -xjvf archive.tar.bz2 | ||
| + | </ | ||
| + | |||
| + | ==== tar + xz ==== | ||
| + | <code bash> | ||
| + | # Compress | ||
| + | tar -cJvf archive.tar.xz directory/ | ||
| + | # Decompress | ||
| + | tar -xJvf archive.tar.xz | ||
| + | </ | ||
| + | |||
| + | <WRAP info> | ||
| + | Explanation of flags: | ||
| + | |||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | </ | ||