gunzip all .gz files in directory

I have a directory with plenty of .txt.gz files (where the names do not follow a specific pattern.)

What is the simplest way to gunzip them? I want to preserve their original names, so that they go from whatevz.txt.gz to whatevz.txt

Asked By: The Unfun Cat


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_*

$ 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_*
Answered By: gertvdijk

If you want to keep the .gz file, you can use the following:
$ gunzip -c inputfile.gz >outputfile

Answered By: sumion

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

gunzip -k *.gz
Answered By: gansvv

I got it by going to the directory and using gzip -d *.gz.

Answered By: Christopher Venezia
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.