How to Zip a File into a .gz File in Linux Terminal

Creating .gz compressed files in Linux can be easily done using command-line tools built into the system. This guide will walk you through the steps to compress a file into a .gz archive using the terminal.

Using gzip Command

  1. Navigate to the File’s Directory:
  • Open a terminal window and navigate (cd) to the directory where the file you want to compress is located. For example:
    cd /path/to/directory
  1. Compressing the File:
  • To compress a file named example.txt into example.txt.gz, use the following command:
    gzip example.txt
  • This will create a compressed file named example.txt.gz in the same directory as the original example.txt.

Using gzip with Options

  1. Preserve Original File:
  • By default, gzip compresses a file and removes the original. To keep the original file after compression, use the -k option:
    gzip -k example.txt
  • This will produce example.txt.gz while retaining example.txt.

Conclusion

Creating .gz compressed files in Linux is straightforward using the gzip command-line tool. Whether you’re compressing a single file or multiple files, these commands provide an efficient way to archive data while conserving disk space.

Next time you need to compress a file into a .gz archive on Linux, refer back to this guide for quick and effective solutions using built-in tools. Happy zipping!


This guide focuses on practical steps for zipping files into .gz archives using the Linux terminal, omitting unnecessary details.