tar -zcvf compressFileName.tar.gz folderToCompress

everything in folderToCompress will go to compressFileName

Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName

Answer from amitchhajer on Stack Overflow
Top answer
1 of 7
901

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 of 7
63

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
Discussions

linux - How to gzip all files in all sub-directories in bash - Stack Overflow
I want to iterate among sub directories ... and gzip each file seperately. For zipping files in a directory, I use ... but this can just work on current directory and not the sub directories of the current directory. How can I rewrite the above statements so that It also zips the files in all ... More on stackoverflow.com
🌐 stackoverflow.com
How do I gzip thousand of files in one directory ?
I recommend that you create a series of zip files (as you suggest). This also addresses the undesirable issue of having all the files in one gigantic archive, that would lose all the files in the event of corruption. More on reddit.com
🌐 r/linux4noobs
8
4
June 25, 2020
linux - GZip an entire directory - Stack Overflow
It seems you have all your files, concatenated and then compressed, in your directory.gz. How many files are there, were they important? ... For future reference, you want to use tar, zip, 7z or some other container format to bundle multiple files together. ... Save this answer. ... Show activity on this post. gzip ... More on stackoverflow.com
🌐 stackoverflow.com
gzip all the files in a directory
Hi, There are multiple files in a directory with different names.How can they be gzipped such that the timestamp of the files is not changed. More on community.unix.com
🌐 community.unix.com
2
0
November 6, 2007
🌐
Linuxize
linuxize.com › home › linux commands › gzip command in linux
gzip Command in Linux | Linuxize
April 24, 2026 - To compress all files in a given directory, use the -r option: ... gzip will recursively traverse through the whole directory structure and compress all the files in the directory and its subdirectories.
🌐
LinuxBlog
linuxblog.io › home › how to gzip a directory using linux command line
How to Gzip a Directory Using Linux Command Line | LinuxBlog.io
September 11, 2025 - After running this command, gzip will compress all the files within the directory and its subdirectories. You’ll find that each file is individually compressed, and the original directory structure is preserved.
Find elsewhere
🌐
Reddit
reddit.com › r/linux4noobs › how do i gzip thousand of files in one directory ?
r/linux4noobs on Reddit: How do I gzip thousand of files in one directory ?
June 25, 2020 -

I have a directory which contains about 17GB of files. There's so many files that when I run gzip with a glob pattern, it would not proceed.

$ gzip *.json
-bash: /bin/gzip: Argument list too long

I know the solution is probably to chunk the list of files. What would you suggest I do ?

Top answer
1 of 4
3
You could use tar to create a compressed archive.
2 of 4
3
xargs will chunk a file list for you. Here's a command line that should be more-or-less functionally equivalent (except where noted): find . -maxdepth 1 -name '*.json' -type f -print0 | xargs -0 gzip Explanations: find: The find program can find files/directories for you based on metadata and print their paths. .: Find items in the current directory, .. -maxdepth 1: Do not find items in subdirectories, but just directly within the current directory. -name '*.json': Items found have to end with the extension .json. Note that '*.json' is in quotes to prevent the shell from expanding this itself. -type f: Only find files, not subdirectories or symlinks. Note that this is a slight functional change from your original command line; you can omit this if you want the exact functionality as before, which would include subdirectories and symlinks. (But note that gzip will not work on subdirectories with the way we're using it.) -print0: Separate the output records with NUL characters instead of newlines. This is important in certain situations as filenames can contain newlines in them. |: Pipe the output to the next program, which is: xargs: This program takes an input list and runs a command with the items in the list as command-line arguments. -0: Let xargs know to expect NUL-separated records. gzip: This is the command to execute. Note that unlike with how the shell normally expands wildcards, find will not return items in lexical order. If you specifically want that, insert a sort -z pipe between the find and xargs commands. (It shouldn't be necessary in this example, though.) By default, xargs will batch items up into one or more command lines. So it might end up executing something like this: gzip ./000001.json ./000002.json ./000003.json [...] ./009360.json ./009361.json gzip ./009362.json ./009363.json ./009364.json [...] ...etc. (Of course, as stated, it might not do these in lexical order as in this example, but hopefully you get the gist.) Note that xargs will not print out the command lines it is doing unless you specifically ask it to do so; to do that, include a -t switch before gzip. Hopefully this helps!
🌐
Baeldung
baeldung.com › home › files › file compression › compression of multiple files individually with gzip
Compression of Multiple Files Individually With gzip | Baeldung on Linux
March 18, 2024 - To uncompress that file, we can use gunzip and pass it the filename explicitly or use the * wildcard, which expands to all individual files in the current directory: $ gunzip * $ ls -R .: dir1 dir2 file0.txt ./dir1: file1.txt file2.txt file3.txt ./dir2: file4.txt file5.txt file6.txt · Now we have compressed and then uncompressed all of the individual files in our testdir and its subdirectories. While gzip is a powerful and handy tool for compressing files in Linux, there are alternatives that may be better suited for some purposes.
Top answer
1 of 2
21

gzip will compress 1+ files, though not meant to function like an archive utility. The posted cmd-line would yield N compressed file images concatenated to stdout, redirected to the named output file; unfortunately stuff like filenames and any dirs would not be recorded. A pair like this should work:

(create)

tar -czvf dir.tar.gz <some-dir>

(extract)

tar -xzvf dir.tar.gz
2 of 2
5

As others have already mentioned, gzip is a file compression tool and not an archival tool. It cannot work with directories. When you run it with -r, it will find all files in a directory hierarchy and compress them, i.e. replacing path/to/file with path/to/file.gz. When you pass -c the gzip output is written to stdout instead of creating files. You have effectively created one big file which contains several gzip-compressed files.

Now, you could look for the gzip file header/magic number, which is 1f8b and then reconstruct your files manually.

The sensible thing to do now is to create backups (if you haven't already). Backups always help (especially with problems such as yours). Create a backup of your directory.gz file now. Then read on.

Fortunately, there's an easier way than manually reconstructing all files: using binwalk, a forensics utility which can be used to extract files from within other files. I tried it with a test file, which was created the same way as yours. Running binwalk -e file.gz will create a folder with all extracted files. It even manages to reconstruct the original file names. The hierarchy of the directories is probably lost. But at least you have your file contents and their names back. Good luck!

Remember: backups are essential.

(For completeness' sake: What you probably intended to run: tar czf directory.tar.gz directory and then tar xf directory.tar.gz)

🌐
Educative
educative.io › answers › how-to-gzip-a-directory-in-linux
How to gzip a directory in Linux
The gzip command in Linux is used to compress or decompress files. We can zip a whole directory in Linux using -r flag in the zip command: ... Copyright ©2026 Educative, Inc. All rights reserved
🌐
Unix Community
community.unix.com › unix for beginners q & a › unix for dummies questions & answers
gzip all the files in a directory - UNIX for Dummies Questions & Answers - Unix Linux Community
November 6, 2007 - Hi, There are multiple files in a directory with different names.How can they be gzipped such that the timestamp of the files is not changed.
Top answer
1 of 4
70

How about just this?

$ gunzip *.txt.gz

gunzip will create a gunzipped file without the .gz suffix and remove the original file by default (see below for details). *.txt.gz will be expanded by your shell to all the files matching.

This last bit can get you into trouble if it expands to a very long list of files. In that case, try using find and -exec to do the job for you.


From the man page gzip(1):

gunzip takes a list of files on its command line and  replaces  each  file
whose  name  ends  with  .gz, -gz, .z, -z, or _z (ignoring case) and which
begins with the correct magic number with an uncompressed file without the
original  extension.

Note about 'original name'

gzip can store and restore the filename used at compression time. Even if you rename the compressed file, you can be surprised to find out it restores to the original name again.

From the gzip manpage:

By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the -N option. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.

And these file names stored in metadata can also be viewed with file:

$ echo "foo" > myfile_orig
$ gzip myfile_orig 
$ mv myfile_orig.gz myfile_new.gz 
$ file myfile_new.gz 
myfile_new.gz: gzip compressed data, was "myfile_orig", last modified: Mon Aug  5 08:46:39 2019, from Unix
$ gunzip myfile_new.gz        # gunzip without -N
$ ls myfile_*
myfile_new

$ rm myfile_*
$ echo "foo" > myfile_orig
$ gzip myfile_orig
$ mv myfile_orig.gz myfile_new.gz 
# gunzip with -N
$ gunzip -N myfile_new.gz     # gunzip with -N
$ ls myfile_*
myfile_orig
2 of 4
10

Use this command to gunzip (unzip gz files) all files in the current directory and keep the original ones:

gunzip -k *.gz
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › how do i compress a whole linux or unix directory?
How do I Compress a Whole Linux or UNIX Directory? - nixCraft
May 19, 2023 - The zstd command is a fast lossless compression algorithm and data compression tool, with command line syntax similar to gzip and xz. The syntax is: $ sudo tar --zstd -cf archive.tar.zst dir1 folder1 file2.txt The --zstd option run the archive through the zstd command. Let us see the compression size for all three formats. Run the following three commands to make /etc/ directory backup in the /tmp/ # Use gzip command for compression # $ sudo tar -zcf /tmp/archive.tar.gz /etc/ # Use bzip2 command for compression # $ sudo tar -jcf /tmp/archive.tar.bz2 /etc/ # Use xz for compression # $ sudo tar -Jcf /tmp/archive.tar.xz /etc/ # Use compress command for compression # $ sudo tar -Zcf /tmp/archive.tar.Z /etc/ # Use zstd command for compression # $ sudo tar --zstd -cf /tmp/archive.tar.zst /etc/ Verify sizes using the ls command: $ cd /tmp/ $ ls -l archive.tar.*
Top answer
1 of 3
9

No, gzip can't do this, -r just means "descend into subdirectories" but there is no option for "descend into subdirectories and then look for files matching this glob". The expansion of the *.vtu glob happens before grep is launched, and it is handled by the shell not grep, so grep is given a specific list of files: those files matching *.vtu in the current directory.

So yes, globstar is your best bet. As for the use of -r, that is explained in man gzip:

-r --recursive
       Travel the directory structure recursively.  If any of the file
       names  specified on the command line are directories, gzip will
       descend into the directory and compress all the files it  finds
       there (or decompress them in the case of gunzip ).

So gzip -r foo means "descend into foo if foo is a directory and gzip any files in it". If foo matches both files and directories, if for example you had both file.vtu and my.vtu/ in the directory you ran gzip in, then the contents of my.vtu would also be compressed. Without it, you would get my.vtu is a directory -- ignored.

Other options include:

  • find . -name "*.vtu" -exec gzip {} + to compress all matching files.
  • gzip **/*.vtu with globstar set.
  • find . -name "*.vtu" | xargs gzip (as long as your names are sane and don't contain newlines)
  • find . -name "*.vtu" -print0 | xargs -0 gzip (if your file names can contain newlines)
2 of 3
2

After the answer by terdon, and upon tinkering a bit, I came to the conclusion that the way -r works is the following:

  1. If what is matched is a file (only in the present directory) do gzip.
  2. If what is matched is a directory, enter that directory, and down there execute gzip -r *.

For me, this is extremely weird (and therefore I would have never imagined this is how it works). For instance, if in ./ I have

foo
foo.vtk
test.vtk/
test.vtk/another.vtk/
test.vtk/another.vtk/cake.vtk
test.vtk/another.vtk/dow.txt
test.vtk/cake.vtk
test.vtk/dow.txt
test.vtk/this/
test.vtk/this/cake.vtk
test.vtk/this/dow.txt

command gzip -r -v *.vtk would gzip all files except ./foo. All files (not only *.vtk), in all subdirectories *.vtk (with depth=1) and * (with depth>1) would be gzipped.

🌐
Quora
quora.com › How-do-I-gzip-all-files-in-a-directory-on-a-Mac
How to gzip all files in a directory on a Mac - Quora
Answer (1 of 3): If you want to create an archive of the directory, see Paul Olaru's answer. If you want to gzip each file individually, from a term window you can do something like: [code]find /which/ever/directory/ -type f -print | while read -r file; do gzip "$file"; done [/code](can be all ...
🌐
Linux Handbook
linuxhandbook.com › gzip-directory
How to gzip a Directory in Linux Command Line
November 21, 2024 - Interesting, isn’t it? gzip command cannot compress a directory because essentially, gzip works on individual files, not the entire folder. What can you do now? How to gzip compress a file in Linux?
🌐
TecMint
tecmint.com › home › linux commands › 13 practical examples of using the gzip command in linux
13 Gzip Command Examples [Compress Files in Linux]
July 14, 2023 - $ gzip alma-linux-1.iso alma-linux-2.iso alma-linux-3.iso $ ls -l ... In the previous example, we saw how to compress multiple files. In a similar way, we can also compress all the files from a directory.