commands.page Website Logo

  • Home
  • Categories
  • Search

How to Compress a Single File Using Gzip on Ubuntu Terminal

Introduction

This is an article about how to compress files in the Linux operating system using the gzip tool specifically from the command line interface (CLI) of Ubuntu. In this guide, we will walk through various steps and commands that allow users to easily and effectively compress single files into a compressed .gz file format.

In this article you will find information about how to use the gzip utility in Ubuntu Linux to create a compressed archive from individual files. This article is aimed at both beginners and experienced users looking for an efficient way to manage their file storage space by reducing file sizes through compression without compromising on data integrity.

Read this article to find out about:

  • What gzip does
  • How to install gzip (if necessary)
  • The basic syntax of the gzip command
  • How to compress a single file using gzip
  • How to extract files compressed with gzip
  • Tips for efficient usage of gzip

What is Gzip?

Gzip stands for GNU zip, which is a software application used for file compression and decompression. It’s widely used in Unix-like operating systems such as Ubuntu to manage space by compressing large files into smaller .gz files.

How Does Compression Work with Gzip?

When you use gzip to compress a file, the original uncompressed data is replaced or removed if the -f option (force) is not specified. Conversely, decompressing a file will restore its original state and size.

Installing Gzip

Before proceeding, it’s important to verify whether gzip is already installed on your Ubuntu system. Open your terminal and enter:

which gzip

If this command returns the path /usr/bin/gzip, then gzip is correctly installed. If not, you can install gzip using apt-get by entering the following command in your terminal:

sudo apt update sudo apt install -y gzip

Basic Syntax of Gzip Command

The basic syntax for compressing a file with gzip is as follows:

gzip [options] filename.ext

Here are some common options you might use when compressing files with gzip:

  • -c: Output the compressed or uncompressed data to standard output instead of a file.
  • -d: Decompress the specified .gz file. This option can also be written as gunzip.
  • -f: Force compression even if the input file is not a regular file, such as when it’s a symbolic link, directory, or block device.
  • -k: Keep the original file after compression (by default, gzip removes the source file).
  • -l: List the contents of the compressed file without decompressing it.

Compressing a Single File Using Gzip

To compress an individual file using gzip, simply run:

gzip filename.ext

For example, if you have a text file named example.txt and want to compress it, you would use the command:

gzip example.txt

After running this command, your original file (example.txt) will be replaced by an archive file called example.txt.gz. If you wish to preserve the original file while creating a .gz version of it, add the -k option:

gzip -k filename.ext

This retains both versions: one compressed and one uncompressed.

Extracting Files Compressed with Gzip

To extract files that were previously compressed using gzip, use the -d flag. The command to decompress a file named example.txt.gz is:

gzip -d example.txt.gz

Alternatively, you can achieve the same result by running:

gunzip example.txt.gz

Both commands will produce an uncompressed version of the original file.

Tips for Efficient Usage of Gzip

Preserving Original Files During Compression

If you want to retain both the compressed and uncompressed versions of a file, use the -k option as described earlier:

gzip -k filename.ext

This is particularly useful when you need access to both versions during development or testing.

Checking the Content of Gzip Archives Without Decompression

You can inspect details about .gz files without decompressing them by using the -l flag followed by the file name:

gzip -l example.txt.gz

This will display information such as compression ratio and uncompressed size, which can be helpful for monitoring space savings.

Combining Commands with Pipes

You might also want to pipe compressed data directly into other commands. For instance, if you’re compressing text output from a command:

some_command | gzip > output.txt.gz

This example sends the result of some_command through gzip, then writes it to an archive file named output.txt.gz.

Conclusion

By mastering how to use gzip on Ubuntu, you gain significant control over managing disk space and transferring files more efficiently. Whether you’re working with large datasets or just looking to optimize your storage, the ability to compress single files into .gz archives is a powerful skill that enhances file management.

Remember, the simplicity of gzip makes it an excellent choice for both quick data compression needs as well as integrating into scripts for automated tasks. Always keep in mind best practices like preserving original files and checking archive contents before fully decompressing them to avoid potential issues down the line.

Last Modified: 22/03/2018 - 18:34:25