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

2 comments on “Linux : How to gzip a folder

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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *