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.
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.
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.
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.
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
As indicated in this similar question, there is also a XZ_OPT environment variable to control the XZ compression level (similar to GZIP in user7385's answer). For example, for maximum (level 9) compression:
XZ_OPT=-9 tar -Jcvf file.tar.xz /path/to/directory
tar cv /path/to/directory | gzip --best > file.tar.gz
This is Matrix Mole's second solution, but slightly shortened:
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 -.
And as stated by the gzip man page, if no files are specified gzip will compress from standard input. There is no need for - in the gzip call.
Option --best (equivalent to -9) sets the highest compression level.
Starting from GNU tar 1.27 (2013), there is also the option to specify the compression program using -I. This can include the compression level option.
tar -I 'gzip -9' -cvf file.tar.gz /path/to/directory
Note that the -I option is shorthand for --use-compress-program=COMMAND. This is important if you're not using GNU tar but BSD tar.
The latter uses the -I option as shorthand for the --files-from filename option.
So to make your command "cross-platform" you could write:
tar --use-compress-program='gzip -9' -cvf file.tar.gz /path/to/directory
And of course macOS bsd-derived tar has to be different:
tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory
Instead of w gzip, one could employ ZStandard compression instead which is both fast and offers similar or better compression ratios to DEFLATE (used by gzip).
tar -cf - dir/ | zstd -o dir.tar.zst
If you want maximal compression (with maximal speeds),
tar -cf - dir/ | zstd -19 -T0 -o dir.tar.zst
Compression levels for zstd range from -7 to 22; 1 (default) to 19 being the most effective.