1. Overview

gunzip is a popular command for decompressing files compressed using gzip. It removes the original zipped file during unzipping. However, we may want gunzip to keep the original zipped file for reusing it later or for safety.

In this tutorial, we’ll discuss how to extract the contents of a zipped file without removing it. Firstly, we’ll examine the default behavior of gunzip while decompressing a zipped file. Then, we’ll discuss three methods for keeping the zipped file after decompression.

2. The Default Behavior of gunzip

We’ll inspect the default behavior of the gunzip command in this section. First, let’s start by creating a file, named example_file, using the echo command:

$ echo Hello World > example_file
$ cat example_file
Hello World

Having created the file, we’ll now zip it using the gzip command:

$ gzip example_file
$ ls
example_file.zip

As the output of ls shows, gzip zipped the file with the name example_file.zip. However, the original file, example_file, doesn’t exist anymore.

Now, let’s unzip the zipped file, example_file.zip¸ using the gunzip command:

$ gunzip example_file.zip
$ ls
example_file

gunzip extracted the contents of example_file.zip. However, the original zipped file, example_file.zip doesn’t exist anymore.

In the following sections, we’ll examine several methods for extracting the contents of a zipped file without removing the zipped file itself.

3. Using gunzip

We can use the -k option of gunzip for retaining the original zipped file:

$ gunzip -k example_file.zip
$ ls
example_file example_file.zip

After zipping example_file, we used the gunzip command together with the -k option. As the output of the ls command shows, we’re successful in unzipping example_file.zip. Additionally, we keep the original zipped file, example_file.zip.

We can use the –keep option instead of -k. They’re the same.

Instead of using the -k option, we can try to use gunzip for retaining the original zipped file by redirecting the zipped file to the standard input of gunzip:

$ gunzip < example_file.zip
Hello World
$ ls
example_file.zip

As the output of gunzip < example_file.zip command shows, the contents of the unzipped file, example_file, were displayed. However, the unzipped file wasn’t created according to the output of the ls command. Therefore, by redirecting the output of gunzip < example_file.zip, we can get the unzipped file:

$ gunzip < example_file.zip > example_file
$ ls
example_file  example_file.zip
$ cat example_file
Hello World

This is another alternative to using the -k option of gunzip.

4. Using zcat

Another alternative to retaining the zipped file is using the zcat command. It’s identical to using the gunzip command together with its -c option, i.e., gunzip -c. It lists the contents of the zipped file:

$ zcat example_file.zip
Hello World
$ ls
example_file.zip

As the output of zcat command shows, zcat just lists the contents of the unzipped file in the terminal without extracting the file. Therefore, if we redirect the output of zcat to example_file, we’ll have both the zipped file and the unzipped one:

$ zcat example_file.zip > example_file
$ ls
example_file  example_file.zip
$ cat example_file
Hello World

We’re successful in both getting the unzipped file and retaining the zipped one by redirecting the output of zcat to a file.

5. Using gzip

gzip is useful for zipping a file. However, we can also use it for unzipping it using its -d option:

$ gzip -d example_file.zip
gzip: example_file already exists; do you wish to overwrite (y or no)? y
$ ls
example_file

As the output of ls shows, gzip unzipped example_file.zip successfully without retaining example_file.zip.

We can use the -c option of gzip together with its -d option to keep the original zipped file. The -c option is for copying the content of the zipped file to the standard output:

$ gzip -dc example_file.zip
Hello World
$ ls
example_file.zip

As the output of gzip -dc example_file.zip shows, gzip printed the contents of the unzipped file. Additionally, the zipped file, example_file.zip, was retained. However, the unzipped file, example_file, wasn’t created.

We can create the unzipped file by redirecting the output of gzip -dc example_file.zip to a file:

$ gzip -dc example_file.zip > example_file
$ ls
example_file  example_file.zip
$ cat example_file
Hello World

Therefore, gzip is another alternative for extracting the contents of a zipped file without removing the zipped file by using its -d and -c options together with redirection.

6. Conclusion

In this article, we discussed how to extract the contents of a zipped file without removing the zipped file.

Firstly, we learned that gunzip removes the zipped file during unzipping, which may not be desirable in some cases. Then, we saw, using examples, that we can use the gunzip, zcat, and gzip commands to keep the original zipped file during decompression.

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