Use find:
find . -exec gunzip '{}' +
or you can look explicitly for files that have not been gunzipped yet:
find . -name \*.gz -exec gunzip '{}' +
Those will do it for everything in the current directory and below. If you want to restrict it to the current directory, add -maxdepth 1 to the options.
The single quotes around the braces are so that spaces in file and directory names are properly handled. That is why xargs will have issues when trying to solve problems like this.
Use find:
find . -exec gunzip '{}' +
or you can look explicitly for files that have not been gunzipped yet:
find . -name \*.gz -exec gunzip '{}' +
Those will do it for everything in the current directory and below. If you want to restrict it to the current directory, add -maxdepth 1 to the options.
The single quotes around the braces are so that spaces in file and directory names are properly handled. That is why xargs will have issues when trying to solve problems like this.
Move a fourth of the files into a new temporary folder? Moving them is very quick. Then you can move some from the temp folder back to the original folder. Actually, the simplest method may be to create a temp folder inside the folder with the 40,000 files. Then they will be easy to move in and out. Gunzip seems to work on around 12,200 files but not on 13,300 files. So doing 10,000 of your files at once should be a breeze.
Opening a .gz and Gzip archive on mac
linux - Unzip all gz files in all subdirectories in the terminal - Stack Overflow
zip - Terminal - Unzip all .gz files in a folder without combining resulting files - Stack Overflow
how to extract a gz file - Apple Community
What software do I need to unzip .gz files?
What is a .gz file on Mac?
Does tar.gz work on Mac?
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
-Noption. 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
Use this command to gunzip (unzip gz files) all files in the current directory and keep the original ones:
gunzip -k *.gz
Hi there. Any help would be much appreciated.
I have some files that are .gz and Gzip archive and can't open them.
I've downloaded The Unarchiver and configured but that doesn't work. Error message: this file cannot be opened with
Have also tried Archive Utility.
Can anyone help or does it mean the files aren't useable? Thank you so much xo
You can use below command.
Go to the directory where your .gz file is and run command:
for f in *.gz ; do gunzip -c "$f" > /home/$USER/"${f%.*}" ; done
It will extract all file with original name and store it to current user home directory(/home/username). You can change it to somewhere else.
EDIT :
gunzip *.gz
This command also will work. But, by default, it replaces original file.
Option # 1 : unzip multiple files using single quote (short version)
gunzip '*.gz'
Note that *.gz word is put in between two single quote, so that shell will not recognize it as a wild card character.
Option # 2 : unzip multiple files using shell for loop (long version)
for g in *.gz; do gunzip $g; done
The Source
EDIT :
I have just tried :
gunzip -dk *.gz
and it worked.
-d to decompress and k to keep original files.
If you want, for each of those, to launch "gzip -d" on them:
cd theparentdir && gzip -d $(find ./ -type f -name '*.gz')
and then, to gzip them back:
cd theparentdir && gzip $(find ./ -type f -name '*.csv')
This will however choke in many cases
- if filenames have some special characters (spaces, tabs, newline, etc) in them
- other similar cases
- or if there are TOO MANY files to be put after the gzip command!
A solution would be instead, if you have GNU find, to do :
find ... -print0 | xarsg -0 gzip -d # for the gunzip one, but still choke on files with "newline" in them
Another (arguably better?) solution, if you have GNU find at your disposal:
cd theparentdir && find ./ -type f -name '*.gz' -exec gzip -d '{}' '+'
and to re-zip all csv in that parentdir & all subdirs:
cd theparentdir && find ./ -type f -name '*.csv' -exec gzip '{}' '+'
"+" tells GNU find to try to put as many found files as it can on each gzip invocation (instead of doing 1 gzip incocation per file, very very ressource intensive and very innefficient and slow), similar to xargs, but with some benefits (1 command only, no pipe needed)
There is an option for recursivity (-r).
gzip -dr ./datasets
All archive will be decompressed in their own directory.
Example: gzip -dr ./a
a/b/c/test1.gz
a/b/d/test2.gz
a/e/test3.gz
After execution:
a/b/c/test1
a/b/d/test2
a/e/test3
This uses gunzip to unzip all files in a folder and have the ending .out.gz
gunzip */*.out.gz
This will "loop"* through all folders that have a zipped file in them. Let me add an example:
A
a.out.gz
B
b.out.gz
C
c.out.gz
D
d.out.gz
E
e.out
Using the command above, a.out.gz b.out.gz c.out.gz d.out.gz will all get unzipped, but it won't touch e.out since it isn't zipped.
*this is called globbing or filename expansion. You might like to read some more about it here.
find . -type f -name "*.gz" -execdir gunzip {} \;
the execdir option to find causes 'find' to 'cd' into each directory in turn and run the exec command (gunzip in this case) and then 'cd' back to the cwd