I gzip directories very often at work. What I normally do is

tar -zcvf file.tar.gz /path/to/directory

Is there a way to specify the compression level here? I want to use the best compression possible even if it takes more time to compress.

link|improve this question

feedback

3 Answers

Instead of using the gzip flag for tar, gzip the files manually after the tar process, then you can specify the compression level for the gzip program:

tar -cvf file.tar /path/to/directory ; gzip -9 file.tar.gz

Or you could use:

tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

The -9 in the gzip command line tells gzip to use the maximum possible compression level (default is -6).

Edit: Fixed pipe command line based on @depesz comment

link|improve this answer
Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz – depesz Jul 1 '11 at 18:40
feedback

No. You will have to drop the compression from tar and filter through gzip yourself if you want to specify gzip options.

link|improve this answer
feedback
GZIP=-9 tar cvzf file.tar.gz /path/to/directory

assuming you're using bash. Generally, set GZIP environment variable to "-9", and run tar normally.

Also - if you really want best compression, don't use gzip. Use lzma or 7z.

And when using gzip (which is good idea for various of reasons anyway) consider using pigz program and not the gzip.

link|improve this answer
What's the logic of using pigz over gzip? But yeah, the remark on compression ratio is, at least according to the initial Google search, quite well founded. – new123456 Jul 2 '11 at 4:03
pigz is faster. That's all. I don't see much point in having compression take longer, while end result is virtually the same. – depesz Jul 2 '11 at 10:10
feedback

Your Answer

 
or
required, but never shown

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