38

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

4 Answers 4

65

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
0
10

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

gunzip -k *.gz
1
  • This should be the accepted answer for directness and simplicity. Very helpful. Unless you only want to work files of a specific type (.txt, .csv, etc.,) then I suppose the current best answer will fit.
    – DtechNet
    Jun 1, 2020 at 16:37
1

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

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .