Extract and delete all .gz in a directory- Linux

LinuxGzipExtractGunzip

Linux Problem Overview


I have a directory. It has about 500K .gz files.

How can I extract all .gz in that directory and delete the .gz files?

Linux Solutions


Solution 1 - Linux

This should do it:

gunzip *.gz

Solution 2 - Linux

@techedemic is correct but is missing '.' to mention the current directory, and this command go throught all subdirectories.

find . -name '*.gz' -exec gunzip '{}' \;

Solution 3 - Linux

There's more than one way to do this obviously.

    # This will find files recursively (you can limit it by using some 'find' parameters. 
    # see the man pages
    # Final backslash required for exec example to work
    find . -name '*.gz' -exec gunzip '{}' \;

    # This will do it only in the current directory
    for a in *.gz; do gunzip $a; done

I'm sure there's other ways as well, but this is probably the simplest.

And to remove it, just do a rm -rf *.gz in the applicable directory

Solution 4 - Linux

Extract all gz files in current directory and its subdirectories:

 find . -name "*.gz" | xargs gunzip 

Solution 5 - Linux

If you want to extract a single file use:

gunzip file.gz

It will extract the file and remove .gz file.

Solution 6 - Linux

for foo in *.gz
do
  tar xf "$foo"
  rm "$foo"
done

Solution 7 - Linux

Try:

ls -1 | grep -E "\.tar\.gz$" | xargs -n 1 tar xvfz

Then Try:

ls -1 | grep -E "\.tar\.gz$" | xargs -n 1 rm

This will untar all .tar.gz files in the current directory and then delete all the .tar.gz files. If you want an explanation, the "|" takes the stdout of the command before it, and uses that as the stdin of the command after it. Use "man command" w/o the quotes to figure out what those commands and arguments do. Or, you can research online.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2247643View Question on Stackoverflow
Solution 1 - LinuxAdrian DunnView Answer on Stackoverflow
Solution 2 - LinuxelhajView Answer on Stackoverflow
Solution 3 - LinuxtechedemicView Answer on Stackoverflow
Solution 4 - LinuxJokeriusView Answer on Stackoverflow
Solution 5 - LinuxNeelamView Answer on Stackoverflow
Solution 6 - LinuxZomboView Answer on Stackoverflow
Solution 7 - LinuxSupermath101View Answer on Stackoverflow