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.txt
file, creatingfile.txt.gz
. Sample Command and Output:
$ gzip file.txt
Description:
file.txt
: The file to be compressed.- After execution,
file.txt
is replaced byfile.txt.gz
, and the original file is deleted. gunzip file.txt.gz
: Decompresses thefile.txt.gz
file, 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.gz
is replaced byfile.txt
, restoring the original file. gzip -c file.txt > file.txt.gz
: Compressesfile.txt
and 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.txt
intact.gunzip -c file.txt.gz > file.txt
: Decompressesfile.txt.gz
and 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.gz
intact.gzip -d file.txt.gz
: Decompressesfile.txt.gz
using 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
).