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
Discussions

Can I zip an entire folder using gzip? - Unix & Linux Stack Exchange
I'm trying to zip a folder in unix. Can that be done using the gzip command? More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
October 1, 2013
linux - GZip every file separately - Stack Overflow
How can we GZip every file separately? I don't want to have all of the files in a big tar. More on stackoverflow.com
๐ŸŒ stackoverflow.com
linux - How to gzip all files in all sub-directories in bash - Stack Overflow
I want to iterate among sub directories of my current location and gzip each file seperately. For zipping files in a directory, I use for file in *; do gzip "$file"; done but this can just work on 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
๐ŸŒ
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 ...
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
๐ŸŒ
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 - Letโ€™s say you have a directory ... this with a simple command: ... -r stands for recursive compression, which tells gzip to compress all files and subdirectories within โ€œmy_directory.โ€...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Commandlinefu
commandlinefu.com โ€บ commands โ€บ view โ€บ 5200 โ€บ individually-compress-each-file-in-a-directory
Individually compress each file in a directory Using gzip
March 29, 2010 - gzip * - (Individually compress each file in a directory Should do exactly the same - compress every file in the current directory. You can even use it recursively: $gzip -r .). The best command line collection on the internet, submit yours and save your favorites.
๐ŸŒ
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!
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-gzip-multiple-files-in-Linux
How to gzip multiple files in Linux - Quora
Answer (1 of 6): Gzip is one of the most popular compression algorithms that allow you to reduce the size of a file and keep the original file mode, ownership, and timestamp. Gzip also refers to the .gz file format and the gzip utility used to compress and decompress files. If you want to compr...
๐ŸŒ
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
๐ŸŒ
Bobcares
bobcares.com โ€บ blog โ€บ gzip-directory-and-subdirectories
Gzip Directory and Subdirectories | Guide
October 31, 2023 - Our experts would like to point out that gzip works for individual files, not directories. To compress entire directories, our experts recommend tar or combining the two. To compress a directory and its subdirectories, we can combine the tar ...
๐ŸŒ
Linux Handbook
linuxhandbook.com โ€บ gzip-directory
How to gzip a Directory in Linux Command Line
November 21, 2024 - How to gzip compress a file in Linux? Hereโ€™s what you can do. Instead of trying to compress the folder directly, you should use tar on it first. The tar command will collate all the files into one archive file.