I'm trying to zip a folder in unix. Can that be done using the gzip command?
7 Answers
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.

- 2,079

- 31,260
-
61Just 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 withtar -zxvf
or evenzcat file.tar.gz | tar -xv
. Point being that the tar is completely independent from the gzip, thetar
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 -
1Does 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-don't-feed-the-AI Feb 09 '17 at 16:00
-
Can we see the overall progress of creating archive by any means other than
-v
flag? I mean like56/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-don't-feed-the-AI Mar 19 '18 at 13:56
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

- 369,824
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>
-
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
-
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
-
2Points 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
-
3I'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
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.
-
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
I recommend using pigz
(Parallel Implementation of GZip)
tar -cvf - dir | pigz -9 > /path/to/dir.tar.gz

- 484
-
I use it a lot too (and pixz), but does it work when pipes and output redirection come into play? – Michael Wolf Dec 19 '18 at 23:38
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/ &

- 131
If your linux tar doesn't support the -z
option you can use the following:
tar -cvf - dir | gzip > dir.tar.gz

- 21
tar
; it supports options for compression.gzip
is designed as a complement totar
, not as a replacement. – devnull Oct 01 '13 at 08:38comment
since that was not the answer to his question. – Valentin Bajrami Oct 01 '13 at 12:191
and2
under folderplayground
.gzip -r ./playground
will give you1.gz
and2.gz
(and no more1
and2
files) under folderplayground
, NOT a zipped file with everything in it. – XoXo Oct 05 '16 at 12:13zip
(w/o ‘g’):zip -r sample1.zip sample1/*
. This creates a (pkware) zip file format file (most common under windows, but handled well on all OSes), not a….tar.gz.
Also see this superuser post – Frank N Jul 04 '18 at 08:29