Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some directories (linux machine) full of highly compressible *.foo files.

Right now I just have a script that does gzip *.foo and it gzips each file into its own .gz file, and removes the original. 7z will compress these files to half the size that gzip will, so I'd like to switch to that.

How can I use 7z to start with a directory like this

and end up with

Or similar. I don't want all of the files in one .7z archive.

share|improve this question
up vote 3 down vote accepted
for i in *.foo; do 7za a $i.7z $i; if [ $? -eq 0 ]; then rm $i; fi; done
share|improve this answer
Simplified: for i in *.foo; do 7za a $i.7z $i && rm $i; done – Dennis Williamson Feb 1 '11 at 21:10
Correct! thanks a bunch! – LVLAaron Feb 1 '11 at 21:33

Give this a try:

find -type f -maxdepth 1 -name "*.foo" -print0 | xargs -0 -I % 7z a %.7z %

but that will leave the original file after it's compressed.

share|improve this answer

Your Answer


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.