How to specify level of compression when using tar -zcvf?












131















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



























    131















    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

























      131












      131








      131


      64






      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














      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.







      linux compression tar gzip






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 1 '11 at 18:00









      LazerLazer

      6,4113695130




      6,4113695130






















          6 Answers
          6






          active

          oldest

          votes


















          127














          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



















          • 13





            pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

            – Felipe Alvarez
            Dec 9 '13 at 2:01






          • 1





            FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

            – Tomofumi
            Mar 2 '17 at 2:59








          • 3





            The environment variable seems to now be GZIP_OPT, the usage should be the same.

            – Seer
            Dec 5 '17 at 11:59






          • 2





            From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

            – Ponyboy47
            Jun 2 '18 at 16:13





















          63














          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





















          • 4





            Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

            – user7385
            Jul 1 '11 at 18:40






          • 1





            1st example should end with file.tar, since gzip adds the ".gz" extension.

            – bonsaiviking
            Feb 4 '13 at 18:20






          • 4





            why don't you skip f -? if there is no file, then it is stdin/out

            – akostadinov
            Sep 19 '13 at 18:52











          • addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

            – Mikl
            Sep 24 '13 at 17:08






          • 2





            and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

            – Mikl
            Sep 24 '13 at 17:18





















          45














          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





            +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








          • 3





            The trade-off is speed. XZ is significantly slower.

            – Bell
            Apr 6 '17 at 23:02



















          29














          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.






          share|improve this answer





















          • 1





            This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

            – PJ Brunet
            Dec 12 '13 at 4:04













          • This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

            – joelostblom
            Feb 12 '15 at 21:52





















          10














          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





          share|improve this answer



















          • 2





            Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

            – Cheetah
            Dec 7 '17 at 0:30



















          0














          And of course macOS bsd-derived tar has to be different:



          tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f305128%2fhow-to-specify-level-of-compression-when-using-tar-zcvf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            127














            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



















            • 13





              pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

              – Felipe Alvarez
              Dec 9 '13 at 2:01






            • 1





              FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

              – Tomofumi
              Mar 2 '17 at 2:59








            • 3





              The environment variable seems to now be GZIP_OPT, the usage should be the same.

              – Seer
              Dec 5 '17 at 11:59






            • 2





              From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

              – Ponyboy47
              Jun 2 '18 at 16:13


















            127














            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



















            • 13





              pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

              – Felipe Alvarez
              Dec 9 '13 at 2:01






            • 1





              FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

              – Tomofumi
              Mar 2 '17 at 2:59








            • 3





              The environment variable seems to now be GZIP_OPT, the usage should be the same.

              – Seer
              Dec 5 '17 at 11:59






            • 2





              From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

              – Ponyboy47
              Jun 2 '18 at 16:13
















            127












            127








            127







            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













            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












            share|improve this answer



            share|improve this answer










            answered Jul 1 '11 at 18:38







            user7385















            • 13





              pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

              – Felipe Alvarez
              Dec 9 '13 at 2:01






            • 1





              FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

              – Tomofumi
              Mar 2 '17 at 2:59








            • 3





              The environment variable seems to now be GZIP_OPT, the usage should be the same.

              – Seer
              Dec 5 '17 at 11:59






            • 2





              From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

              – Ponyboy47
              Jun 2 '18 at 16:13
















            • 13





              pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

              – Felipe Alvarez
              Dec 9 '13 at 2:01






            • 1





              FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

              – Tomofumi
              Mar 2 '17 at 2:59








            • 3





              The environment variable seems to now be GZIP_OPT, the usage should be the same.

              – Seer
              Dec 5 '17 at 11:59






            • 2





              From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

              – Ponyboy47
              Jun 2 '18 at 16:13










            13




            13





            pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

            – Felipe Alvarez
            Dec 9 '13 at 2:01





            pigz is "parallel gzip" which uses all your cores for gzip compression. You can watch top and see it using anywhere between 200%-400$ CPU.

            – Felipe Alvarez
            Dec 9 '13 at 2:01




            1




            1





            FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

            – Tomofumi
            Mar 2 '17 at 2:59







            FYI, for .bz2 format, use: BZIP2=-9 tar cvjf file.tar.bz2 /path/to/directory

            – Tomofumi
            Mar 2 '17 at 2:59






            3




            3





            The environment variable seems to now be GZIP_OPT, the usage should be the same.

            – Seer
            Dec 5 '17 at 11:59





            The environment variable seems to now be GZIP_OPT, the usage should be the same.

            – Seer
            Dec 5 '17 at 11:59




            2




            2





            From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

            – Ponyboy47
            Jun 2 '18 at 16:13







            From the man page on Ubuntu 16.04 for gzip: "On Vax/VMS, the name of the environment variable is GZIP_OPT, to avoid a conflict with the symbol set for invocation of the program." For sh, csh, and MSDOS it should still just be GZIP

            – Ponyboy47
            Jun 2 '18 at 16:13















            63














            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





















            • 4





              Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

              – user7385
              Jul 1 '11 at 18:40






            • 1





              1st example should end with file.tar, since gzip adds the ".gz" extension.

              – bonsaiviking
              Feb 4 '13 at 18:20






            • 4





              why don't you skip f -? if there is no file, then it is stdin/out

              – akostadinov
              Sep 19 '13 at 18:52











            • addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

              – Mikl
              Sep 24 '13 at 17:08






            • 2





              and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

              – Mikl
              Sep 24 '13 at 17:18


















            63














            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





















            • 4





              Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

              – user7385
              Jul 1 '11 at 18:40






            • 1





              1st example should end with file.tar, since gzip adds the ".gz" extension.

              – bonsaiviking
              Feb 4 '13 at 18:20






            • 4





              why don't you skip f -? if there is no file, then it is stdin/out

              – akostadinov
              Sep 19 '13 at 18:52











            • addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

              – Mikl
              Sep 24 '13 at 17:08






            • 2





              and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

              – Mikl
              Sep 24 '13 at 17:18
















            63












            63








            63







            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















            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














            share|improve this answer



            share|improve this answer








            edited May 6 '13 at 21:27









            dfarrell07

            1053




            1053










            answered Jul 1 '11 at 18:25









            Matrix MoleMatrix Mole

            3,1851614




            3,1851614








            • 4





              Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

              – user7385
              Jul 1 '11 at 18:40






            • 1





              1st example should end with file.tar, since gzip adds the ".gz" extension.

              – bonsaiviking
              Feb 4 '13 at 18:20






            • 4





              why don't you skip f -? if there is no file, then it is stdin/out

              – akostadinov
              Sep 19 '13 at 18:52











            • addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

              – Mikl
              Sep 24 '13 at 17:08






            • 2





              and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

              – Mikl
              Sep 24 '13 at 17:18
















            • 4





              Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

              – user7385
              Jul 1 '11 at 18:40






            • 1





              1st example should end with file.tar, since gzip adds the ".gz" extension.

              – bonsaiviking
              Feb 4 '13 at 18:20






            • 4





              why don't you skip f -? if there is no file, then it is stdin/out

              – akostadinov
              Sep 19 '13 at 18:52











            • addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

              – Mikl
              Sep 24 '13 at 17:08






            • 2





              and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

              – Mikl
              Sep 24 '13 at 17:18










            4




            4





            Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

            – user7385
            Jul 1 '11 at 18:40





            Using pipes should be done with: tar cvf - /path/to/directory | gzip -9 - > file.tar.gz

            – user7385
            Jul 1 '11 at 18:40




            1




            1





            1st example should end with file.tar, since gzip adds the ".gz" extension.

            – bonsaiviking
            Feb 4 '13 at 18:20





            1st example should end with file.tar, since gzip adds the ".gz" extension.

            – bonsaiviking
            Feb 4 '13 at 18:20




            4




            4





            why don't you skip f -? if there is no file, then it is stdin/out

            – akostadinov
            Sep 19 '13 at 18:52





            why don't you skip f -? if there is no file, then it is stdin/out

            – akostadinov
            Sep 19 '13 at 18:52













            addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

            – Mikl
            Sep 24 '13 at 17:08





            addition to the previos comment. From "man tar" section Environtment: TAPE Device or file to use for the archive if --file is not specified. If this environment variable is unset, use stdin or stdout instead.

            – Mikl
            Sep 24 '13 at 17:08




            2




            2





            and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

            – Mikl
            Sep 24 '13 at 17:18







            and we can reduce "gzip -9 -" -> "gzip -9". From "man gzip" section Description: If no files are specified, or if a file name is "-", the standard input is compressed to the standard output.

            – Mikl
            Sep 24 '13 at 17:18













            45














            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





              +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








            • 3





              The trade-off is speed. XZ is significantly slower.

              – Bell
              Apr 6 '17 at 23:02
















            45














            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





              +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








            • 3





              The trade-off is speed. XZ is significantly slower.

              – Bell
              Apr 6 '17 at 23:02














            45












            45








            45







            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















            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














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:37









            Community

            1




            1










            answered Dec 19 '12 at 3:03









            David C. BishopDavid C. Bishop

            1,07499




            1,07499








            • 1





              +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








            • 3





              The trade-off is speed. XZ is significantly slower.

              – Bell
              Apr 6 '17 at 23:02














            • 1





              +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








            • 3





              The trade-off is speed. XZ is significantly slower.

              – Bell
              Apr 6 '17 at 23:02








            1




            1





            +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







            +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






            3




            3





            The trade-off is speed. XZ is significantly slower.

            – Bell
            Apr 6 '17 at 23:02





            The trade-off is speed. XZ is significantly slower.

            – Bell
            Apr 6 '17 at 23:02











            29














            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.






            share|improve this answer





















            • 1





              This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

              – PJ Brunet
              Dec 12 '13 at 4:04













            • This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

              – joelostblom
              Feb 12 '15 at 21:52


















            29














            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.






            share|improve this answer





















            • 1





              This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

              – PJ Brunet
              Dec 12 '13 at 4:04













            • This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

              – joelostblom
              Feb 12 '15 at 21:52
















            29












            29








            29







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 28 '16 at 11:05

























            answered Feb 4 '13 at 18:17









            carlitocarlito

            47167




            47167








            • 1





              This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

              – PJ Brunet
              Dec 12 '13 at 4:04













            • This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

              – joelostblom
              Feb 12 '15 at 21:52
















            • 1





              This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

              – PJ Brunet
              Dec 12 '13 at 4:04













            • This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

              – joelostblom
              Feb 12 '15 at 21:52










            1




            1





            This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

            – PJ Brunet
            Dec 12 '13 at 4:04







            This works beautifully. Also if you run as root, permissions & owners are preserved too. Otherwise you must specify. Also if it wasn't obvious "-9" is best compression and "-1" is fastest compression. "-1" still takes a looong time if you have lots of files ;-)

            – PJ Brunet
            Dec 12 '13 at 4:04















            This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

            – joelostblom
            Feb 12 '15 at 21:52







            This works with xz and pixz too. It is a great way to control the number of threads used for parallel compressing without having to create an intermediate .tar file. Like so tar -cv /path/to/dir | pixz -p4 > output.tpxz

            – joelostblom
            Feb 12 '15 at 21:52













            10














            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





            share|improve this answer



















            • 2





              Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

              – Cheetah
              Dec 7 '17 at 0:30
















            10














            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





            share|improve this answer



















            • 2





              Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

              – Cheetah
              Dec 7 '17 at 0:30














            10












            10








            10







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 21 '16 at 4:21









            Chris GibsonChris Gibson

            10112




            10112








            • 2





              Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

              – Cheetah
              Dec 7 '17 at 0:30














            • 2





              Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

              – Cheetah
              Dec 7 '17 at 0:30








            2




            2





            Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

            – Cheetah
            Dec 7 '17 at 0:30





            Older versions of tar such as that provided in CentOS 6 & 7 do not support providing arguments in the -I arg, they will try to treat the whole thing as a program name to exec, and thus fail. At least as of tar 1.29 in Debian Stretch, this does work.

            – Cheetah
            Dec 7 '17 at 0:30











            0














            And of course macOS bsd-derived tar has to be different:



            tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory





            share|improve this answer




























              0














              And of course macOS bsd-derived tar has to be different:



              tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory





              share|improve this answer


























                0












                0








                0







                And of course macOS bsd-derived tar has to be different:



                tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory





                share|improve this answer













                And of course macOS bsd-derived tar has to be different:



                tar -czf file.tar.gz --options gzip:compression-level=9 /path/to/directory






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 19 at 16:26









                rfayrfay

                1011




                1011






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f305128%2fhow-to-specify-level-of-compression-when-using-tar-zcvf%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Plaza Victoria

                    Puebla de Zaragoza

                    Musa