1. Overview

Compressing and uncompressing files and folders are common operations in Linux system administration. The gzip and gunzip utilities allow us to perform these operations from the command line, which is especially important when no GUI is available.

In this tutorial, we’ll learn the steps for using gzip to compress multiple files individually. We’ll also see how to use gunzip to uncompress files that have been compressed using gzip.

Finally, we’ll take a quick look at a couple of alternatives to gzip and note their pros and cons.

2. Compressing a Single File With gzip

The simplest use for gzip is compressing a single file.

We’ll start with an example directory called testdir in our user home directory.

$ cd ~/testdir
$ pwd
$ /home/user1/testdir

Here, we changed directories using the cd command. In Linux, the  ~ (tilde) character expands to the current user’s home directory, in this case, user1.

The testdir directory for this example contains a single plaintext file:

$ ls 
file0.txt

Above, we used the ls command to list the contents of the current directory.

If we include the -l flag (for long listing), ls provides us with more details about the file:

ls -l
total 8
-rw-r--r--+ 1 user1 user1 7980 Jul 1 19:24 file0.txt

From the above output, we can see that file0.txt is owned by user1 and that its size is 7980 bytes.  The first line of the output shows that the contents of the directory use 8 total 1K blocks of disk space.

To compress that text file, we can issue the gzip command with the filename as the only argument:

$ gzip file0.txt

We can see the results of running the above command by listing the directory contents again:

$ ls -l
total 1
-rw-r--r--+ 1 user1 user1 565 Jul 1 19:31 file0.txt.gz

As we see above, the original file0.txt has been replaced with the compressed version, file0.txt.gz. The size of the compressed file is 565 blocks.

We can undo the compression operation using the gunzip command:

$ gzip file0.txt
ls -l
<code class="language-bash">total 8
-rw-r--r--+ 1 user1 user1 7980 Jul 1 19:24 file0.txt 

Finally, if we want to compress a file without deleting the original uncompressed file, we can use gzip with the -k (for “keep“) flag:

$ gzip -k file0.txt
ls -l total 9
-rw-r--r--+ 1 user1 user1 7980 Jul 1 19:24 file0.txt
-rw-r--r--+ 1 user1 user1 565 Jul 1 19:31 file0.txt.gz

We see from the above output that both files are present after running the gzip -k command. The total space used by the two files is 9 blocks.

3. Compressing Multiple Individual Files With gzip

While compressing or uncompressing a single file is useful, we often need to process multiple files distributed among multiple directories.

We could compress or uncompress each file individually by running the gzip or gunzip commands on each file as in the discussion above. However, this process becomes tedious when more than a couple of files are involved.

Instead, let’s look at other options.

3.1. Compressing Multiple Files at Once With gzip

To compress multiple files at once, we can use gzip with wildcard expansion.

First, we’ll change back to the top-level testdir:

$ cd ~/testdir

Next, we run gzip with a single argument:

$ gzip */*

The asterisk is a wildcard in Linux, which expands to any file or folder matching the pattern. In this case, */* expands to any file under any subfolder of testdir.

We can check the results of the above command by running ls -R again:

$ ls -R
.: dir1 dir2 file0.txt.gz

./dir1: 
file1.txt.gz file2.txt.gz file3.txt.gz

./dir2: 
file4.txt.gz file5.txt.gz file6.txt.gz

As we see above, the previous gzip command compressed both dir1/file3.txt and dir2/file6.txt.

3.2. Uncompressing Multiple Files at Once With gunzip

We can uncompress all of the files in the subdirectories in a similar fashion using gunzip:

$ gunzip */*
$ ls -R
.: dir1 dir2 file0.txt.gz

./dir1: 
file1.txt file2.txt file3.txt

./dir2: 
file4.txt file5.txt file6.txt

In the above example, file0.txt.gz wasn’t uncompressed because it doesn’t match the */* pattern since it’s not in a subdirectory of testdir.

To uncompress that file, we can use gunzip and pass it the filename explicitly or use the * wildcard, which expands to all individual files in the current directory:

$ gunzip *
$ ls -R
.: dir1 dir2 file0.txt

./dir1: 
file1.txt file2.txt file3.txt

./dir2: 
file4.txt file5.txt file6.txt

Now we have compressed and then uncompressed all of the individual files in our testdir and its subdirectories.

4. gzip Alternatives

While gzip is a powerful and handy tool for compressing files in Linux, there are alternatives that may be better suited for some purposes.

We previously looked at Zip and 7-Zip in Linux, two widely used alternatives, among a few other similar utilities

Both offer certain advantages over gzip. Zip, for example, can compress entire directory hierarchies, while gzip can compress only individual files. In order to compress complete directory structures using gzip, we first need to tar the top-level directory.

7-Zip is a popular open-source and cross-platform utility that handles many different archive formats.

The primary advantage that gzip offers is wide availability. Nearly every Linux core has gzip already installed, which makes it a reliable tool and helps to keep Docker images and other cloud deployments as small and tight as possible.

5. Conclusion

In this article, we first saw how to use gzip and gunzip to compress and uncompress a single file.

Then we looked at a few methods for compressing multiple files spread among subdirectories using gzip with explicit file names. Next, we saw how to compress and uncompress multiple files at once using gzip and gunzip with Linux wildcard expansion.

Finally, we looked at a couple of common alternatives to gzip and noted the pros and cons of each.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.