How to gzip a directory in Linux

Key takeaways

  • gzip is a tool for compressing individual files using the DEFLATE algorithm.

  • gzip alone cannot compress directories. Instead, use tar to create an archive of the directory, and then apply gzip to compress the archive. This can be done in one step using tar -z:

    • Use tar -zcvf filename.tar.gz directory_name command

  • tar -z efficiently reduces file size and transfer times while preserving the directory structure.

gzip is a file compression tool by Jean-Loup Gailly and Mark Adler that uses the DEFLATE algorithm (a combination of LZ77 and Huffman coding). It is effective for compressing individual files but does not support directory structures by itself. On the other hand, the tar command is used to generate archive files. It can combine several files and directories into a single file. When combined with the gzip command, it enables both archiving and compression.

Compression reduces file size, whereas archiving combines multiple files into a single one. The tar command is used for archiving files, while gzip command is used for compressing them.

gzip a directory in Linux

The gzip command in Linux can only be used to compress a single file. In order to compress a directory, the combination of the tar + gzip command (which is basically tar -z) is used​. Let’s have a look at how to use tar -z command to compress an entire directory in Linux.

svg viewer

Compressing the directories simplifies file management, reduces storage requirements, and makes directory transfers quick and easy. Lossless data compression ensures that the original data can be extracted from the compressed copy.

Suppose we have a myfolder directory whose contents are as follows:

myfolder/.DS_Store
myfolder/LOG
myfolder/extension.xml
myfolder/LICENSE
myfolder/myfolder.yml
myfolder/README.txt
Content of the myfolder directory

To compress the myfolder directory, we run the following tar -z command:

tar -zcvf myfolder.tar.gz myfolder
  • tar: The command for archiving.

  • -z: Compress the archive using gzip.

  • -c: Create a new archive.

  • -v: Verbose mode; lists files being archived.

  • -f: Specifies the filename for the archive.

  • myfolder.tar.gz: The name of the resulting compressed file.

  • myfolder: The directory we want to compress.

Note that the order of the option flags matters. For example, -f expects the name of the file immediately after it. So adding another flag after it instead of the file name would not give the expected results.

To view the contents of the myfolder.tar.gz directory, we run the following command:

tar -tf myfolder.tar.gz
  • -t: List the contents of the archive.

  • -f: Specifies the filename for the archive.

The output of the above command would be:

my_folder/
myfolder/._.DS_Store
myfolder/.DS_Store
myfolder/._LOG
myfolder/LOG
myfolder/._extension.xml
myfolder/extension.xml
myfolder/._LICENSE
myfolder/LICENSE
myfolder/._myfolder.yml
myfolder/myfolder.yml
myfolder/._README.txt
myfolder/README.txt
Content of the myfolder directory

gzip multiple directories

We can gzip the multiple directories at once by listing all directories in the following way:

tar -zcvf multiplefolders.tar.gz dir1/ dir2/ dir3/

Handling compression level

We can specify the compression level with tar -z as follows:

tar -cvf - myfolder | gzip -9 > myfolder.tar.gz
  • - operator: Sends the archive output to standard output, so it can be used by another command right after.

  • | pipe operator: Send the output of left-hand side command as the input to the right-hand side command.

  • gzip -9: Compresses data with the highest compression level (9) using gzip.

Hands-on practice

We've provided a directory named gzip in the following terminal. Please execute the tar -zcvf command to compress Linux directory named gzip directory and display its contents.

Terminal 1
Terminal
Loading...

Let's quickly test your understanding of gzipping a directory in Linux in the following quiz:

Quiz!

1

What does the gzip command do in Linux?

A)

Archives files

B)

Manages directories

C)

Compresses individual files

D)

Extracts .tar files

Question 1 of 40 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do we decompress and unpack the archive?

We can decompress and unpack the archive using tar -zxvf archivename.tar.gz command to decompress and unpack a .tar.gz file. This command extracts the files into the current directory.


What is tar and gz?

tar is a Unix command used to multiple files into one and gz is a file extension used for files compressed with the gzip utility.


Does gzip work on directories?

No, gzip compresses single files only.


How do I create a .gz folder in Linux?

We cannot create a .gz folder directly because .gz is a file compression format, not a directory format. We can compress a directory into a .tar.gz file in the following way:

tar -zcvf myfolder.tar.gz myfolder

What is the gzip command?

The gzip command in Linux is used to compress or decompress files.


How do we zip a whole directory in Linux?

We can zip a whole directory in Linux using -r flag in the zip command:

zip -r folders.zip folder_1

Copyright ©2024 Educative, Inc. All rights reserved