views:

1168

answers:

3

Dear all,

I have several directories that look like this:

dir1/
  |_foo.txt
  |_bar.txt
dir2/
  |_qux.txt
  |_bar.txt

For each of this directory I want to compress the files inside it into *.gz format then delete the uncompressed ones. So finally we hope to get something like this:

 dir1/
   |_foo.gz
   |_bar.gz
 dir2/
   |_qux.gz
   |_bar.gz

Is there a simple Unix way to do it?

+3  A: 
gzip */*.txt

But the extension for each file will be .txt.gz, as gzip uses it to know the original filename.

Juliano
Just for clarification as it is not obvious: gzip will delete the uncompressed files itself.
ypnos
is there a way to not delete the originals? I currently am using the -c flag (gzip -9 -c style.css > style.css.gz)
Daniel Hai
+2  A: 

gzip -r dir1 dir2

vovick
Interesting; certainly works with the Cygwin version. I wasn't aware of the '-r' option applying to gzip, but it isn't surprising, either.
Jonathan Leffler
+1  A: 

The following will work even if you have sub-directories. E.g. dir1/dir2/.../foo.txt

find . -type f -name "*.txt" -exec gzip {} \;

dogbane