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

share|improve this question
5  
you can use gzip -r myfolder which will gzip files recursively. The same way you can unzip files using gunzip -r myfolder which will unzip files recursively. For more information, man gzip – val0x00ff Oct 1 '13 at 8:27
11  
Use tar; it supports options for compression. gzip is designed as a complement to tar, not as a replacement. – devnull Oct 1 '13 at 8:38
12  
Note that @val0x00ff 's suggestion is almost certainly not what you want to accomplish. – Shadur Oct 1 '13 at 11:49
4  
@Shadur that's why I posted it as comment since that was not the answer to his question. – val0x00ff Oct 1 '13 at 12:19
2  
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. – Jeff Xiao Oct 5 '16 at 12:13

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 c (create) an archive from the files in directory (tar is recursive by default), compress it using the z (gzip) algorithm, store the output as a f (file) named archive.tar.gz, and v (verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.

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.

share|improve this answer
39  
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. – Patrick Oct 2 '13 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) – Jeff Xiao Oct 5 '16 at 12:11
    
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 9 '17 at 15:58
2  
@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 '17 at 16:00

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
share|improve this answer

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 (!!!).

gunzipdir /home/MyDirectory.tgz

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

share|improve this answer
    
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 5:49

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

tar -cvf - dir | gzip > dir.tar.gz
share|improve this answer

I recommend using pigz(Parallel Implementation of GZip)

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

share|improve this answer

The Answer to the question “Can I zip an entire folder using gzip?” is to use the syntax:

zip -r <zip file name> <folder to zip>
share|improve this answer
    
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Jeff Schaller Oct 12 '17 at 18:29
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.