19

I have tried both gzip and gunzip commands but I get either

gunzip *.gz 
gzip: invalid option -- 'Y' 

gunzip -S-1800-01-01-000000-g01.h5.gz  
gzip: compressed data not read
 from a terminal. Use -f to force decompression. For help, type: gzip -h

If I try the -f option it takes a very long time to work on one single file and the command is not executed successfully. Am I missing something?

3
  • 3
    Does the directory contain .gz files whose names start with hyphens, such as -Y.something.gz? If so you may need to use the Gnu -- flag to ensure that they are treated as filenames rather than options i.e. gunzip -- *.gz – steeldriver Nov 3 '15 at 13:29
  • @steeldriver yes I do have a few files starting with - – Herman Toothrot Nov 3 '15 at 13:47
  • that caused at least one of the two problems. – Herman Toothrot Nov 3 '15 at 14:25
25

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.

3
  • Have you read what I wrote in my question? gunzip *.gz is not working for me. But the for loop works, I am puzzled. – Herman Toothrot Nov 3 '15 at 13:24
  • but it is working for me and others also why is showing ` invalid option -- 'Y' ` . i think either your file format is not ok or you missing some thing in command. – pl_rock Nov 3 '15 at 13:26
  • also see unix.stackexchange.com/questions/56421/… – pl_rock Nov 3 '15 at 13:27
10

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.

5
  • have you checked gunzip ‘*.gz’ this command . i am not able to run this command . it giving error . – pl_rock Nov 3 '15 at 13:08
  • use gunzip '*.gz' not gunzip ‘*.gz’ (' ' not `‘ ``) – Bilal Nov 3 '15 at 13:16
  • i am just copy pasting your command and it giving gzip: *.gz: No such file or directory new also not working . have u tried ? – pl_rock Nov 3 '15 at 13:19
  • It does not work gunzip '*.gz' gzip: *.gz: No such file or directory – Herman Toothrot Nov 3 '15 at 13:23
  • Sorry, i didn'ttry it ! i'm on WIndows Right now :( – Bilal Nov 3 '15 at 13:26
1

Linux Users:

Use the following command for extracting minimum amount of .gz files in the current directory and its sub directories

gunzip *.gz

Use the following command for extracting any number of .gz files in the current directory and its sub directories

sudo find . -name "*.gz" | xargs gunzip
1
  • how to understand "minimum amount of .gz files"? as "0 or 1"? – qdinar Jan 8 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.