546

I'm trying to zip a folder in unix. Can that be done using the gzip command?

6
  • 18
    Use tar; it supports options for compression. gzip is designed as a complement to tar, not as a replacement.
    – devnull
    Oct 1, 2013 at 8:38
  • 15
    Note that @val0x00ff 's suggestion is almost certainly not what you want to accomplish.
    – Shadur
    Oct 1, 2013 at 11:49
  • 4
    @Shadur that's why I posted it as comment since that was not the answer to his question. Oct 1, 2013 at 12:19
  • 4
    To demostrate what @Shadur means. Given files 1 and 2 under folder playground. gzip -r ./playground will give you 1.gz and 2.gz (and no more 1 and 2 files) under folder playground, NOT a zipped file with everything in it.
    – XoXo
    Oct 5, 2016 at 12:13
  • @Shadur - I had the same issue using the answer at lifewire.com/example-uses-of-the-linux-gzip-command-4078675 coz I blindly went for the most plausible command - they need to specify RECURSIVELY - I mean who would want to do that more often than an entire dir ?
    – killjoy
    Dec 28, 2017 at 15:19

7 Answers 7

871

No.

Unlike zip, gzip functions as a compression algorithm only.

Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip, bzip2, 7zip, etc.

In order to "zip" a directory, the correct command would be

tar -zcvf archive.tar.gz directory/ 

This will tell tar to

  • compress it using the z (gzip) algorithm

  • c (create) an archive from the files in directory (tar is recursive by default)

  • v (verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.

  • and store the output as a f (file) named archive.tar.gz

The tar command offers gzip support (via the -z flag) purely for your convenience. The gzip command/lib is completely separate. The command above is effectively the same as

tar -cv directory | gzip > archive.tar.gz

To decompress and unpack the archive into the current directory you would use

tar -zxvf archive.tar.gz

That command is effectively the same as

gunzip < archive.tar.gz | tar -xv

tar has many, many, MANY other options and uses as well; I heartily recommend reading through its manpage sometime.

6
  • 61
    Just to make things even more explicit, this is exactly equivilant to tar -cv directory | gzip > archive.tar.gz. The resulting archive can then be extracted with tar -zxvf or even zcat file.tar.gz | tar -xv. Point being that the tar is completely independent from the gzip, the tar command just includes gzip support for convenience.
    – phemmer
    Oct 2, 2013 at 0:16
  • i have been using tar cvzf for quite a while. one thing to note: if you use Windows (7-zip to be specified) to unzip a *.tar.gz file, it takes two rounds. One to unzip *.tar.gz file into a *.tar file, the next one to unzip that tar file into the original content. it increases the total unzipping time, especially for large files (e.g. logs)
    – XoXo
    Oct 5, 2016 at 12:11
  • 1
    Does the naming of the archive affect anything machine-wise? I know it would be nice to let people know the algorithm originally used to zip it (hence .gz), but other than that does is it actually matter how you name the archive? Feb 9, 2017 at 15:58
  • 4
    @hello_there_andy It makes no difference to most unixes, but windows (and smart tab completion in linux) will makes assumptions based on filename extension.
    – Shadur
    Feb 9, 2017 at 16:00
  • 2
    @vadimkotov This might be what you're looking for: superuser.com/questions/168749/…
    – Shadur
    Mar 19, 2018 at 13:56
61

The gzip command will not recursively compress a directory into a single zip file, when using the -r switch. Rather it will walk that directory structure and zip each file that it finds into a separate file.

Example

before

$ tree dir1/
dir1/
|-- dir11
|   |-- file11
|   |-- file12
|   `-- file13
|-- file1
|-- file2
`-- file3

now run the gzip command

$ gzip -r dir1

after

$ tree dir1/
dir1/
|-- dir11
|   |-- file11.gz
|   |-- file12.gz
|   `-- file13.gz
|-- file1.gz
|-- file2.gz
`-- file3.gz

If you'd prefer to zip up the directory structure then you'll likely want to use the tar command, and then compress the resulting .tar file.

$ tar zcvf dir1.tar.gz dir1/

Example

$ tar zcvf dir1.tar.gz dir1/
dir1/
dir1/file1
dir1/file2
dir1/dir11/
dir1/dir11/file11.gz
dir1/dir11/file12.gz
dir1/dir11/file13.gz
dir1/file3

Which results in the following single file:

$ ls -l | grep tar
-rw-rw-r-- 1 saml saml  271 Oct  1 08:07 dir1.tar.gz

You can confirm its contents:

$ tar ztvf dir1.tar.gz 
drwxrwxr-x saml/saml         0 2013-10-01 08:05 dir1/
-rw-rw-r-- saml/saml         0 2013-10-01 07:45 dir1/file1
-rw-rw-r-- saml/saml         0 2013-10-01 07:45 dir1/file2
drwxrwxr-x saml/saml         0 2013-10-01 08:04 dir1/dir11/
-rw-rw-r-- saml/saml        27 2013-10-01 07:45 dir1/dir11/file11.gz
-rw-rw-r-- saml/saml        27 2013-10-01 07:45 dir1/dir11/file12.gz
-rw-rw-r-- saml/saml        27 2013-10-01 07:45 dir1/dir11/file13.gz
-rw-rw-r-- saml/saml         0 2013-10-01 07:45 dir1/file3
20

The Answer to the question “Can I zip an entire folder using gzip [on linux]?” is that you can use the zip program in place of gzip, with the syntax:

zip -r <zip file name> <folder to zip>
6
  • 4
    @JeffSchaller: This is an answer.  It’s a grievously wrong one (I believe), but it clearly is an answer. Oct 12, 2017 at 18:42
  • 1
    @ize: I'm pretty sure the command you posted does not, in fact, use gzip.
    – dhag
    Oct 12, 2017 at 18:46
  • 2
    Points for using zip like the question asked, most likely the author would have liked to know it's possible to DO THE EXACT SAME THING as he used to do on windows. It is sad most people don't wish to think as far. Apr 15, 2018 at 19:49
  • 4
    It’s tricky when the question specifies ... “using gzip”.
    – Jeff Schaller
    Apr 15, 2018 at 20:57
  • 3
    I'm upvoting this answer since I think the product (a zip file) is likely more important than the method (using gzip).
    – andyvanee
    Sep 3, 2019 at 18:02
5

I scripted these 2 commands:

gzipdir:

#!/bin/bash
if [[ -d $1 ]]; then
    cd "$1"
    cd ..
    base=$(basename "$1")
    tar -zcvf "$base.tgz" "$base"
    if [[ $? == 0 && -f "$base.tgz" ]]; then
        rm -rf "$base"
    fi
else
    echo "Usage: $0 DIRECTORY";
fi

ungzipdir:

#!/bin/bash
if [[ -f $1 ]]; then
    base=${1%%.*}
    file=$(basename "$1");
    dir=$(basename "$base");
    if [[ ! -d $base ]]; then
        mkdir "$base"
        cd "$base"
        cd ..
        tar -xvf "$file"
        if [[ $? == 0 && -d "$dir" ]]; then
            rm -f "$file"
        fi
    else
        echo "Directory $base already exists. Nothing done."
    fi
else
    echo "Usage: $0 file.tgz";
fi

(!!!) Please test before use (as there is a 'rm -f' which could potentially remove important data if used in an uncommon way).

How to use:

cd /home/; gzipdir MyDirectory or gzipdir /home/MyDirectory

Will create /home/MyDirectory.tgz and remove MyDirectory on success (!!!).

ungzipdir /home/MyDirectory.tgz

Will create /home/MyDirectory and remove /home/MyDirectory.tgz on success.

1
  • To zip up ALL the files in a directory: find ./* -maxdepth 0 -type d -exec gzipdir {} \; (be careful with the find command, test it with -exec echo {} first, and make sure you're not capturing './' which will zip up the PARENT directory, possibly breaking your whole home dir) Jun 29, 2017 at 5:49
3

I recommend using pigz(Parallel Implementation of GZip)

tar -cvf - dir | pigz -9 > /path/to/dir.tar.gz

1
  • I use it a lot too (and pixz), but does it work when pipes and output redirection come into play? Dec 19, 2018 at 23:38
2

If your linux tar doesn't support the -z option you can use the following:

tar -cvf - dir | gzip > dir.tar.gz
2

For completeness, if you want to tar the file in the background, use @Shadur's answer and append & in the end:

tar -zcvf archive.tar.gz directory/ &

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .