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 - 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
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
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 - We see from the above output that both files are present after running the gzip -k command. The total space used by the two files is 9 blocks. While compressing or uncompressing a single file is useful, we often need to process multiple files ...
๐ŸŒ
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.
๐ŸŒ
The Geek Diary
thegeekdiary.com โ€บ how-to-gzip-all-or-specific-files-in-linux
How to gzip all or specific files in Linux โ€“ The Geek Diary
To gzip all the files in current directory, we can use for command. The below example will gzip all the files from /var/log/audit directory.
๐ŸŒ
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.โ€...
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
GitHub
github.com โ€บ TehShrike โ€บ gzip-all
GitHub - TehShrike/gzip-all: Create .gz files for all files in a folder ยท GitHub
const gzipAll = require('gzip-all') gzipAll('**/content/*.md').then(newFiles => { console.log('yay, created', newFiles.length, 'compressed files!') })
Author ย  TehShrike
๐ŸŒ
RootUsers
rootusers.com โ€บ home โ€บ 11 simple gzip examples
11 Simple Gzip Examples
July 18, 2018 - This example creates a compressed etc.tar.gz file of the entire /etc/ directory. The tar flags are as follows, โ€˜cโ€™ creates a new tar archive, โ€˜zโ€™ specifies that we want to compress with gzip, โ€˜vโ€™ provides verbose information, and โ€˜fโ€™ specifies the file to create. The resulting etc.tar.gz file contains all files within /etc/ compressed using gzip.
๐ŸŒ
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 and gzip commands as seen here: ... This allows us to create compressed archives easily.
๐ŸŒ
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!
๐ŸŒ
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.
๐ŸŒ
codestudy
codestudy.net โ€บ blog โ€บ gzip-multiple-files-individually-and-keep-the-original-files
How to Gzip Multiple Files Individually and Keep Original Files: A Simple Guide โ€” codestudy.net
To compress a directory, first archive it with tar, then gzip the archive (e.g., tar -czf dir.tar.gz directory/), but this is for archiving, not individual files. Use -1 (fastest) to -9 (best compression) to balance speed and size. Default is -6. gzip -k -9 large_file.dat # Best compression (slower) gzip -k -1 quick_file.txt # Fastest compression (larger output) ... Save this as compress_files.sh to compress all .txt files in a target directory (pass the directory as an argument):