Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

directory
folder
linux

How to gzip a directory in Linux

Educative Answers Team

The gzip command in Linux can only be used to compress a single file. In order to compress a folder, tar + gzip (which is basically tar -z) is used​.

svg viewer

Let’s have a look at how to use tar -z to compress an entire directory in Linux.

Suppose we have the folder myfolder whose content is:

myfolder/.DS_Store
myfolder/LOG
myfolder/extension.xml
myfolder/LICENSE
myfolder/myfolder.yml
myfolder/README.txt

To compress myfolder, the following tar -z command needs to be run:

$ tar -zcvf myfolder.tar.gz myfolder

The parameters after the -zcvf flag are the compressed file name and the original folder to compress, respectively. The command above will compress myfolder and make a new, compressed file named ​myfolder.tar.gz.

To view the content of myfolder.tar.gz, run the following command:

$ tar -tf myfolder.tar.gz

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

RELATED TAGS

directory
folder
linux
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring