Compress files and directories | LinuxGist
This article will provide details on how we can compress/decompress files and directories in Linux using command line tools. We will look into some commonly used tools like gzip, bzip2, xz, tar, and zip.
Introduction
Compression and decompression of files and directories are common operations when using computers. Compression reduces the size of files and directories, making them easier to store and transfer. Decompression restores the original file or directory from a compressed format. Linux offers several powerful tools for compressing and decompressing files, each with its own advantages and use cases.
For this article we will give examples for a single files (foo.txt) and a directory (foo) containing multiple files.
Compressing Files
- gzip and gunzip
- gzip: Compresses files using the DEFLATE algorithm.
- gunzip: Decompresses files compressed by gzip.
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt gzip foo.txt # Verify compression (file will be renamed to foo.txt.gz) ls -lh
1
-rw-r--r-- 1 harpal docker 28 Jan 5 12:51 foo.txt.gz
- bzip2 and bunzip2
- bzip2: Compresses files using the Burrows-Wheeler algorithm.
- bunzip2: Decompresses files compressed by bzip2.
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt bzip2 foo.txt # Verify compression (file will be renamed to foo.txt.bz2) ls -lh
1
-rw-r--r-- 1 harpal docker 14 Jan 5 12:53 foo.txt.bz2
- xz and unxz
- xz: Compresses files using the LZMA algorithm.
- unxz: Decompresses files compressed by xz.
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt xz foo.txt # Verify compression (file will be renamed to foo.txt.xz) ls -lh
1
-rw-r--r-- 1 harpal docker 32 Jan 5 12:54 foo.txt.xz
- tar with gzip, bzip2, or xz
- tar: Creates archives and can be used with different compression tools.
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt into a gzip archive tar -czvf archive.tar.gz foo.txt # Verify compression (archive will be in the current directory) ls -lh
1 2
-rw-r--r-- 1 harpal docker 0 Jan 5 12:55 foo.txt -rw-r--r-- 1 harpal docker 125 Jan 5 12:55 archive.tar.gz
- zip and unzip
- zip: Compresses files into a ZIP archive.
- unzip: Decompresses files compressed in a ZIP archive.
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt into a zip archive zip archive.zip foo.txt # Verify compression (archive will be in the current directory) ls -lh
- 7z and 7za
- 7z: A powerful compression tool supporting multiple formats.
Installation in Ubuntu:
1
sudo apt install 7zip
Installation in Fedora:
1
sudo dnf install 7zip
Example for
foo.txt
:1 2 3 4 5
# Compress foo.txt into a 7z archive 7z a archive.7z foo.txt # Verify compression (archive will be in the current directory) ls -lh
1
-rw-r--r-- 1 harpal docker 90 Jan 5 12:57 archive.7z
Compressing Directories
- tar with gzip, bzip2, or xz
- tar: Creates archives and can be used with different compression tools.
Example for
foo
directory:1 2 3 4 5
# Compress the foo directory into a gzip archive tar -czvf archive.tar.gz foo # Verify compression (archive will be in the current directory) ls -ltr
1 2 3 4 5 6 7 8 9
harpal@precision:$ tar -czvf archive.tar.gz foo foo/ foo/file2 foo/file3 foo/file1 harpal@precision:$ ls -ltr total 8 drwxr-xr-x 2 harpal docker 4096 Jan 5 12:59 foo -rw-r--r-- 1 harpal docker 177 Jan 5 12:59 archive.tar.gz
- 7z and 7za
- 7z: A powerful compression tool supporting multiple formats.
Example for
foo
directory:1 2 3 4 5
# Compress the foo directory into a 7z archive 7z a archive.7z foo # Verify compression (archive will be in the current directory) ls -ltr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
harpal@precision:$ 7z a archive.7z foo 7-Zip 23.01 (x64) : Copyright (c) 1999-2023 Igor Pavlov : 2023-06-20 64-bit locale=en_GB.UTF-8 Threads:16 OPEN_MAX:1024 Scanning the drive: 1 folder, 3 files, 0 bytes Creating archive: archive.7z Add new data to archive: 1 folder, 3 files, 0 bytes Files read from disk: 0 Archive size: 150 bytes (1 KiB) Everything is Ok harpal@precision:$ ls -ltr total 8 drwxr-xr-x 2 harpal docker 4096 Jan 5 12:59 foo -rw-r--r-- 1 harpal docker 150 Jan 5 13:02 archive.7z
Decompressing Files and Directories
- gzip and gunzip
- gzip: Compresses files using the DEFLATE algorithm.
- gunzip: Decompresses files compressed by gzip.
Example for
foo.txt.gz
:1 2 3 4 5
# Decompress foo.txt.gz gunzip foo.txt.gz # Verify decompression (file will be renamed to foo.txt) ls -lh
- bzip2 and bunzip2
- bzip2: Compresses files using the Burrows-Wheeler algorithm.
- bunzip2: Decompresses files compressed by bzip2.
Example for
foo.txt.bz2
:1 2 3 4 5
# Decompress foo.txt.bz2 bunzip2 foo.txt.bz2 # Verify decompression (file will be renamed to foo.txt) ls -lh
- xz and unxz
- xz: Compresses files using the LZMA algorithm.
- unxz: Decompresses files compressed by xz.
Example for
foo.txt.xz
:1 2 3 4 5
# Decompress foo.txt.xz unxz foo.txt.xz # Verify decompression (file will be renamed to foo.txt) ls -lh
- tar with gzip, bzip2, or xz
- tar: Extracts archives created with different compression tools.
Example for
archive.tar.gz
:1 2 3 4 5
# Extract archive.tar.gz tar -xzvf archive.tar.gz # Verify extraction (files will be in the current directory) ls -lh
- zip and unzip
- zip: Compresses files into a ZIP archive.
- unzip: Decompresses files compressed in a ZIP archive.
Example for
archive.zip
:1 2 3 4 5
# Extract archive.zip unzip archive.zip # Verify extraction (files will be in the current directory) ls -lh
- 7z and 7za
- 7z: A powerful compression tool supporting multiple formats.
Example for
archive.7z
:1 2 3 4 5
# Extract archive.7z 7z x archive.7z # Verify extraction (files will be in the current directory) ls -lh
Conclusion
These commands provide a comprehensive guide on compressing and decompressing files and directories in Linux using various tools and compression methods.