How to set compression level here?

nice -n 19 tar -czf $out $in

nice won't do anything to the level of compression. It will only affect the scheduling of the process by the kernel. All commands below may be prefixed with nice to run them at a different "nice"-level, it will not influence the level of compression, only the time taken to perform the action (if the system is under heavy load).

If you're using GNU tar then the compression program may be set with either the -I or the --use-compress-program options:

$ tar -I "gzip --best" -c -f archive.tar.gz dir


$ tar --use-compress-program="gzip --best" -c -f archive.tar.gz dir

Note that the -z option should not be used if you set the compression program explicitly.

The GNU tar manual states that the program used must support an -d option for decompression (if you use these options for decompressing a compressed archive), which gzip does.

With BSD tar it's not possible to specify the level of compression in a similar way, as far as I have seen.

The other way to achieve this is obviously to create an un-compressed archive and then compress that in the way one wants:

$ tar -c -f archive.tar dir
$ gzip --best archive.tar


$ tar -c dir | gzip --best -o archive.tar.gz

Yet another way is to set the GZIP environment variable to the flags that you'd like to pass to gzip:

$ export GZIP='--best'
$ tar -c -z -f archive.tar.gz dir

Except for the use of -I and --use-compress-program, the last few alternatives work with both GNU and BSD tar.

  • 1
    tar -c dir | gzip --best > archive.tar.gz May 20 '17 at 13:04
  • yes, but I need to use nice
    – clarkk
    May 20 '17 at 13:18
  • 3
    @clarkk You may use nice. nice have no impact on the creation of the archive or on the compression, it just changes the scheduling of the tar/gzip process. My answer deals with setting the level of compression. Just prefix the commands with nice (as you did in your question) and it will work nicely.
    – Kusalananda
    May 20 '17 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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