Linux : How to gzip a folder

On Linux, gzip is unable to compress a folder, it used to compress a single file only. To compress a folder, you should use tar + gzip, which is tar -z.

Note


$ tar --help
  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma

For example,


tar -zcvf outputFileName folderToCompress

1. Tar + Gzip a folder

Example to gzip a folder named “newrelic”, and also all the files within that folder, into a single compressed file newrelic.tar.gz


$ tar -zcvf newrelic.tar.gz newrelic/

a newrelic
a newrelic/.DS_Store
a newrelic/CHANGELOG
a newrelic/extension-example.xml
a newrelic/extension.xsd
a newrelic/LICENSE
a newrelic/newrelic-api-sources.jar
a newrelic/newrelic-api.jar
a newrelic/newrelic.jar
a newrelic/newrelic.yml
a newrelic/nrcerts
a newrelic/README.txt

$ ls -lsa

     0 drwxr-xr-x  13 mkyong  staff        442 Jan 24 21:40 newrelic
 11072 -rw-r--r--   1 mkyong  staff    5667938 Jul 23 15:44 newrelic.tar.gz

List the contents of newrelic.tar.gz


$ tar -tf newrelic.tar.gz
newrelic/
newrelic/._.DS_Store
newrelic/.DS_Store
newrelic/._CHANGELOG
newrelic/CHANGELOG
newrelic/._extension-example.xml
newrelic/extension-example.xml
newrelic/._extension.xsd
newrelic/extension.xsd
newrelic/._LICENSE
newrelic/LICENSE
newrelic/._newrelic-api-sources.jar
newrelic/newrelic-api-sources.jar
newrelic/._newrelic-api.jar
newrelic/newrelic-api.jar
newrelic/._newrelic.jar
newrelic/newrelic.jar
newrelic/._newrelic.yml
newrelic/newrelic.yml
newrelic/._nrcerts
newrelic/nrcerts
newrelic/._README.txt
newrelic/README.txt

References

  1. Linux : gzip command
  2. Linux : tar command

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Andrew NyagoJim Walone Recent comment authors
newest oldest most voted
Jim Walone
Guest
Jim Walone

Thank you, some good examples of using tar with gzip here, I also found the extra information on what else can be done with gzip at these locations below, hopefully it helps anyone else seeking more info too.

https://www.rootusers.com/11-simple-gzip-examples/
http://lowfatlinux.com/linux-gzip-gunzip.html

Andrew Nyago
Guest
Andrew Nyago

Hi. how do we pull a similar stunt using pigz [Parallel Implementation of GZip]