
{{ $('Map tags to IDs').item.json.title }}
How to Compress Files with gzip
The gzip
command in Linux is a widely used utility for compressing files, often used to decrease file size for storage or network transfers. This tutorial will guide you through the basic usage and options of the gzip
command.
1. Installing gzip
Most Linux distributions come with gzip
pre-installed. To check if it’s installed, run:
gzip --version
If it’s not installed, you can install it using:
- For Ubuntu:
sudo apt update sudo apt install gzip
- For CentOS:
sudo yum install gzip
2. Compressing Files
The basic command to compress a file using gzip is:
gzip filename
This command replaces the original file with a compressed version, adding a .gz
extension. For example, compressing file.txt
will result in file.txt.gz
.
2.1. Keeping the Original File
If you want to compress a file while keeping the original, use the -k
option:
gzip -k filename
This command will create a compressed file without deleting the original.
3. Decompressing Files
To decompress a file that was compressed with gzip, use the following command:
gzip -d filename.gz
Alternatively, you can use:
gunzip filename.gz
Both commands will restore the original uncompressed file.
3.1. Keeping the Compressed File
If you want to decompress a file while keeping the compressed version, use the -k
option:
gzip -dk filename.gz
4. Compressing Directories
To compress an entire directory, you’ll first need to create a tarball and then compress it. Use:
tar -czvf archive.tar.gz /path/to/directory
This command tars and compresses the directory into a .tar.gz
file.
5. Conclusion
By following this tutorial, you have learned how to use the gzip
command to compress and decompress files in Linux. Efficient file compression not only saves space but also optimizes bandwidth during file transfers. Continue exploring other compression utilities and options to enhance your file management on Linux!