Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress multiple files individually with Gzip

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?

like image 458
neversaint Avatar asked Apr 25 '09 13:04

neversaint


People also ask

How do I gzip multiple files at once?

If you want to compress multiple files or directory into one file, first you need to create a Tar archive and then compress the . tar file with Gzip.

Does gzip support multiple files?

Gzip is not capable of compressing multiple files into one.

What is the difference between zip and gzip?

The most important difference is that gzip is only capable to compress a single file while zip compresses multiple files one by one and archives them into one single file afterwards. Thus, gzip comes along with tar most of the time (there are other possibilities, though).


2 Answers

gzip */*.txt 

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

like image 81
Juliano Avatar answered Oct 11 '22 12:10

Juliano


gzip -r dir1 dir2

like image 25
vovick Avatar answered Oct 11 '22 14:10

vovick