Enhancing Skills

gzip: Compress or decompress files

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 the file.txt file, creating file.txt.gz. Sample Command and Output:
  $ gzip file.txt

Description:

  • file.txt: The file to be compressed.
  • After execution, file.txt is replaced by file.txt.gz, and the original file is deleted.
  • gunzip file.txt.gz: Decompresses the file.txt.gz file, restoring the original file.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 by file.txt, restoring the original file.
  • gzip -c file.txt > file.txt.gz: Compresses file.txt and writes the output to file.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 to file.txt.gz, leaving the original file.txt intact.
  • gunzip -c file.txt.gz > file.txt: Decompresses file.txt.gz and writes the output to file.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 to file.txt, leaving the compressed file.txt.gz intact.
  • gzip -d file.txt.gz: Decompresses file.txt.gz using an alternative to gunzip. Sample Command and Output:
  $ gzip -d file.txt.gz

Description:

  • -d: Decompress the file. This is equivalent to using gunzip.
  • 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).


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.