2024-07-31
gzipgzip is a file compression program that uses the DEFLATE algorithm, a combination of LZ77 and Huffman coding. This results in highly efficient compression, especially for text files and other data with repetitive patterns. The compressed files typically have a .gz extension.
The simplest way to compress a file using gzip is:
gzip myfile.txtThis command will compress myfile.txt and create a new file named myfile.txt.gz. The original myfile.txt will be deleted.
To avoid deleting the original file, use the -k (keep) option:
gzip -k myfile.txtThis will leave myfile.txt intact and create myfile.txt.gz.
You can compress multiple files simultaneously:
gzip file1.txt file2.txt file3.txtThis will compress each file individually, creating file1.txt.gz, file2.txt.gz, and file3.txt.gz. Remember that the -k option can be used here as well to preserve the original files.
Instead of letting gzip automatically append .gz, you can specify the output filename directly using the -c option and redirection:
gzip -c myfile.txt > myfile.gzThis compresses myfile.txt and writes the compressed output to myfile.gz. The original myfile.txt remains unchanged.
To decompress a .gz file, use the gunzip command:
gunzip myfile.txt.gzThis will restore myfile.txt from myfile.txt.gz. The compressed file myfile.txt.gz will be deleted unless you use the -k option.
gzipYou can also decompress with gzip itself using the -d option:
gzip -d myfile.txt.gzThis achieves the same result as gunzip.
It’s useful to identify compressed files in your directory. gzip doesn’t directly provide this, but ls with appropriate options helps:
ls *.gzThis lists all files ending with .gz in the current directory.
gzip allows you to control the compression level with the -n option, ranging from 1 (fastest) to 9 (best compression, slowest). The default is 6:
gzip -1 myfile.txt # Fastest compression
gzip -9 myfile.txt # Best compression (slowest)Choosing the right level depends on your priorities; higher levels yield smaller files but take longer to compress and decompress.
gzip does not directly compress directories. You need to use other tools like tar in conjunction with gzip to achieve this:
tar -czvf myarchive.tar.gz mydirectory/This command uses tar to create an archive (myarchive.tar.gz) of mydirectory/ and compresses it using gzip. The -c creates the archive, -z uses gzip, -v shows verbose output, and -f specifies the archive filename. Unpacking can be done with:
tar -xzvf myarchive.tar.gzThis detailed guide should equip you to effectively use gzip for your file compression needs in Linux. Remember to always back up important files before performing any compression or decompression operations.