Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a folder with 36,348 files gz files. I want to unzip all of them.

Running:

gunzip ./*

results in

-bash: /usr/bin/gunzip: Argument list too long

Whats the easiest way to get around this?

Thanks!

share|improve this question

4 Answers 4

up vote 5 down vote accepted

Try:

find . -type f -exec gunzip {} +

This assumes that current directory only contains files that you want to unzip.

share|improve this answer
    
Is there any version of find where find . doesn't work? –  Patrick Aug 23 at 19:38
    
@Patrick: I don't remember ./filename can work with gunzip. I have tried and it works. Roll back! –  cuonglm Aug 23 at 19:40
2  
find . -type f -iname "*.gz" -exec gunzip {} + to match only files that end .gz ignoring case. –  richard Aug 23 at 22:51
1  
@richard: -iname is not POSIX. –  cuonglm Aug 24 at 4:53

The limit is in the kernel and is on the cumulative size of the arguments and environment passed to the execve() system call used to execute a command. You can split it in several invocations.

To gunzip all the .gz files in the current directory:

with zsh:

autoload zargs # best in ~/.zshrc
zargs ./*.gz -- gunzip

With ksh93:

command -x gunzip ./*.gz

GNUly:

printf '%s\0' *.gz | xargs -r0 gunzip

POSIXly (and with gunzip):

find . ! -name . -prune -name '*.gz' -exec gunzip {} +

(beware that one will also uncompress the hidden .gz files)

Or you can raise that limit on some systems. On recent versions of Linux:

ulimit -s unlimited
gunzip ./*.gz
share|improve this answer
    
Why we need -r in GNUly? –  cuonglm Aug 23 at 19:52
    
@Gnouc, not needed strictly speaking, except with nullglob. But doesn't harm and it's a good habit to have to use it unless you have a good reason not to. –  Stéphane Chazelas Aug 23 at 19:54

A bit efficient solution would be,

find $PWD -type f -name "*.gz" -print0 | xargs -0 gunzip

But if your find does not have -print0 option and your files/dir does not have space in naming, then you can skip -print0 and -0 arguments.

share|improve this answer
    
You can save the call to xarg by using the -exec {} + construct of find. –  Michael Grünewald Aug 24 at 8:40
    
Yes, but doing exec on every file would take more time, wouldn't it ? I guess usings xargs would be more efficient. –  vknha Aug 24 at 18:18
    
It will not exec the program once for each file, but rather behave much like xargs does, this is what the + at the end of the line is for. –  Michael Grünewald Aug 24 at 20:21
    
@MichaelGrünewald okay got it, I was not aware of + option. thanks. –  vknha Aug 25 at 7:41

A less efficient ( but long-run very flexible ) process is make a batch file:

ls | grep .gz | sed -e 's/^/gunzip /' | less

shows you what will happen so a simple typo doesn't annihilate your system

ls | grep .gz | sed -e 's/^/gunzip /' | bash

does it right now

ls | grep .gz | sed -e 's/^/gunzip /' > unpack
at 02:00
bash unpack
<ctrl-d>

does it overnight

(note this example does not account for spaces in filenames.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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