92

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

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

Phonolog
  • 6,321
  • 3
  • 36
  • 64
user2247643
  • 953
  • 1
  • 6
  • 6

8 Answers8

177

This should do it:

gunzip *.gz
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Adrian Dunn
  • 1,728
  • 1
  • 10
  • 7
  • 2
    ... unless that gives you an 'argmuent too big' error. In which case, you'll want to use something like `find "$dir" -maxdepth 1 -name '*.gz' -print0 | xjobs -0 -l50 -v2 gunzip` to restrict instances to 50 arguments each (and to run them in parallel). – Toby Speight Jul 27 '17 at 10:01
  • ... or just `find "$dir" -maxdepth 1 -name '*.gz' -exec gunzip {} +` to run them sequentially. Remove the `-maxdepth 1` to also traverse subdirectories, and replace `"$dir"` with the directory you want to examine (just `.` for current directory, or on Linux simply omit the directory). – tripleee May 22 '23 at 06:18
25

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

find . -name '*.gz' -exec gunzip '{}' \;
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
elhaj
  • 438
  • 5
  • 10
21

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

catch23
  • 17,519
  • 42
  • 144
  • 217
techedemic
  • 341
  • 2
  • 6
4

Extract all gz files in current directory and its subdirectories:

 find . -name "*.gz" | xargs gunzip 
Jokerius
  • 1,310
  • 1
  • 14
  • 22
3

If you want to extract a single file use:

gunzip file.gz

It will extract the file and remove .gz file.

Neelam
  • 360
  • 4
  • 14
1
for foo in *.gz
do
  tar xf "$foo"
  rm "$foo"
done
Zombo
  • 1
  • 62
  • 391
  • 407
0

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.

Supermath101
  • 417
  • 3
  • 10
-1

for deleting all .gz files in each and every directory. Please use this commands

step:1

cd /

step :2

find . -type f -iname *.gz -delete