gzip: Compress or decompress files
August 9th, 2024 1:31 PM Mr. Q Categories: Command
Command: gzip / gunzip
The gzip command is used to compress files using the GNU zip algorithm, which reduces the size of files. The gunzip command is used to decompress files that have been compressed with gzip.
Sample Commands and Outputs:
gzip file.txt: Compresses thefile.txtfile, creatingfile.txt.gz. Sample Command and Output:
$ gzip file.txt
Description:
file.txt: The file to be compressed.- After execution,
file.txtis replaced byfile.txt.gz, and the original file is deleted. gunzip file.txt.gz: Decompresses thefile.txt.gzfile, restoring the originalfile.txt. Sample Command and Output:
$ gunzip file.txt.gz
Description:
file.txt.gz: The compressed file to be decompressed.- After execution,
file.txt.gzis replaced byfile.txt, restoring the original file. gzip -c file.txt > file.txt.gz: Compressesfile.txtand writes the output tofile.txt.gz, preserving the original file. Sample Command and Output:
$ gzip -c file.txt > file.txt.gz
Description:
-c: Write the compressed output to standard output (stdout), allowing redirection to a file.> file.txt.gz: Redirects the output tofile.txt.gz, leaving the originalfile.txtintact.gunzip -c file.txt.gz > file.txt: Decompressesfile.txt.gzand writes the output tofile.txt, preserving the compressed file. Sample Command and Output:
$ gunzip -c file.txt.gz > file.txt
Description:
-c: Write the decompressed output to standard output (stdout), allowing redirection to a file.> file.txt: Redirects the output tofile.txt, leaving the compressedfile.txt.gzintact.gzip -d file.txt.gz: Decompressesfile.txt.gzusing an alternative togunzip. Sample Command and Output:
$ gzip -d file.txt.gz
Description:
-d: Decompress the file. This is equivalent to usinggunzip.gzip -l file.txt.gz: Lists information about the compressed file. Sample Command and Output:
$ gzip -l file.txt.gz
compressed uncompressed ratio uncompressed_name
1234 5678 78.0% file.txt
Description:
-l: Lists details about the compressed file, including its original size and the compression ratio.
Note: gzip is commonly used for compressing single files. For compressing multiple files or directories, you often use tar combined with gzip (e.g., tar -cvzf archive.tar.gz /path/to/directory).