Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 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 files.tar /path/to/file0 /path/to/file1 ; gzip -9 files.tar

Or you could use:

tar cvf - /path/to/file0 /path/to/file1 | gzip -9 - > files.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.

share|improve this answer
2  
Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz – depesz Jul 1 '11 at 18:40
1  
1st example should end with file.tar, since gzip adds the ".gz" extension. – bonsaiviking Feb 4 at 18:20
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.

share|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
Is -9 on purpose or a typo? – We are the World Feb 21 at 2:29
On purpose. This is specifically the answer to OP question. – depesz Feb 22 at 18:13

Modern versions of tar support the xz archive format (GNU tar, since 1.22 in 2009, Busybox since 1.17.0 in 2010).

It's based on lzma2, kind of like a 7-Zip version of gz. This gives better compression if you are ok with the requirement of needing xz support.

tar -Jcvf file.tar.xz /path/to/directory

I just found out here (basically a dupe of this question, but in the Unix stackexchange) that there is also a XZ_OPT=-9 environment variable to control the XZ compression level similar to the GZIP one in the other post.

XZ_OPT=-9 tar -Jcvf file.tar.xz /path/to/directory
share|improve this answer
+1 xz is far better than both bzip2 and gzip. Here's a comparison: tukaani.org/lzma/benchmarks.html – User1 Dec 25 '12 at 15:44

Matrix Mole's second solution could be slightly shortened:

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

When calling tar, option 'f' states that the output is a file. Setting it to '-' (stdout) makes tar write its output to stdout which is the default behavior without both 'f' and '-'.

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.