542

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

Anthon
  • 78,313
  • 42
  • 165
  • 222
user1269502
  • 5,521
  • 4
  • 13
  • 4
  • 17
    Use `tar`; it supports options for compression. `gzip` is designed as a complement to `tar`, not as a replacement. – devnull Oct 01 '13 at 08:38
  • 15
    Note that @val0x00ff 's suggestion is almost certainly not what you want to accomplish. – Shadur Oct 01 '13 at 11:49
  • 4
    @Shadur that's why I posted it as `comment` since that was not the answer to his question. – Valentin Bajrami Oct 01 '13 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 05 '16 at 12:13
  • @Shadur - I had the same issue using the answer at https://www.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 '17 at 15:19
  • You can zip an entire folder using `zip` (w/o ‘g’): `zip -r sample1.zip sample1/*`. This creates a [(pkware) zip file format file](https://en.wikipedia.org/wiki/Zip_(file_format)) (most common under windows, but handled well on all OSes), not a `….tar.gz.` Also see [this superuser post](https://superuser.com/a/173825/75914) – Frank N Jul 04 '18 at 08:29

7 Answers7

867

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.

Black
  • 1,989
  • 7
  • 28
  • 58
Shadur
  • 30,641
  • 11
  • 58
  • 69
  • 59
    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 02 '13 at 00: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 05 '16 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? – hello_there_andy Feb 09 '17 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 09 '17 at 16:00
  • Can we see the overall progress of creating archive by any means other than `-v` flag? I mean like `56/100% is done` or something like that – Vadim Kotov Mar 19 '18 at 13:40
  • 2
    @vadimkotov This might be what you're looking for: https://superuser.com/questions/168749/is-there-a-way-to-see-any-tar-progress-per-file/168756 – Shadur Mar 19 '18 at 13:56
60

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
slm
  • 363,520
  • 117
  • 767
  • 871
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>
Stephen P
  • 159
  • 1
  • 7
ize
  • 229
  • 2
  • 2
  • If you have a new question, please ask it by clicking the [Ask Question](https://unix.stackexchange.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/213535) – Jeff Schaller Oct 12 '17 at 18:29
  • 4
    @JeffSchaller: This is an answer.  It’s a grievously wrong one (I believe), but it clearly is an answer. – Scott - Слава Україні Oct 12 '17 at 18:42
  • 1
    @ize: I'm pretty sure the command you posted does not, in fact, use gzip. – dhag Oct 12 '17 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. – Florian Heigl Apr 15 '18 at 19:49
  • 4
    It’s tricky when the question specifies ... “using gzip”. – Jeff Schaller Apr 15 '18 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 03 '19 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.

Archemar
  • 31,183
  • 18
  • 69
  • 104
lepe
  • 391
  • 4
  • 9
  • 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) – andrew lorien Jun 29 '17 at 05:49
3

I recommend using pigz(Parallel Implementation of GZip)

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

nyxee
  • 474
  • 3
  • 6
2

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

tar -cvf - dir | gzip > dir.tar.gz
Jeff
  • 21
  • 1
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/ &
shiramy
  • 121
  • 2