Compress and remove original directory with limited storage












4















On my 3 TB external hard drive, I have a 2.7 TB directory containing relatively small files. I would like to compress this 2.7 TB directory and remove it to keep only the compressed version. The issue is that I do not have enough storage to first zip and then rm the non-zipped directory.



Is there a way around this problem or do I necessarily have to acquire more storage for the manipulation?










share|improve this question

























  • That may help: superuser.com/questions/656111/…

    – alpert
    Apr 25 '16 at 21:18











  • Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

    – ott--
    Apr 25 '16 at 21:26











  • @ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

    – Remi.b
    Apr 25 '16 at 21:35


















4















On my 3 TB external hard drive, I have a 2.7 TB directory containing relatively small files. I would like to compress this 2.7 TB directory and remove it to keep only the compressed version. The issue is that I do not have enough storage to first zip and then rm the non-zipped directory.



Is there a way around this problem or do I necessarily have to acquire more storage for the manipulation?










share|improve this question

























  • That may help: superuser.com/questions/656111/…

    – alpert
    Apr 25 '16 at 21:18











  • Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

    – ott--
    Apr 25 '16 at 21:26











  • @ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

    – Remi.b
    Apr 25 '16 at 21:35
















4












4








4


0






On my 3 TB external hard drive, I have a 2.7 TB directory containing relatively small files. I would like to compress this 2.7 TB directory and remove it to keep only the compressed version. The issue is that I do not have enough storage to first zip and then rm the non-zipped directory.



Is there a way around this problem or do I necessarily have to acquire more storage for the manipulation?










share|improve this question
















On my 3 TB external hard drive, I have a 2.7 TB directory containing relatively small files. I would like to compress this 2.7 TB directory and remove it to keep only the compressed version. The issue is that I do not have enough storage to first zip and then rm the non-zipped directory.



Is there a way around this problem or do I necessarily have to acquire more storage for the manipulation?







rm zip compression storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 2 '17 at 1:39









Jeff Schaller

41.5k1056132




41.5k1056132










asked Apr 25 '16 at 21:09









Remi.bRemi.b

3351515




3351515













  • That may help: superuser.com/questions/656111/…

    – alpert
    Apr 25 '16 at 21:18











  • Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

    – ott--
    Apr 25 '16 at 21:26











  • @ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

    – Remi.b
    Apr 25 '16 at 21:35





















  • That may help: superuser.com/questions/656111/…

    – alpert
    Apr 25 '16 at 21:18











  • Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

    – ott--
    Apr 25 '16 at 21:26











  • @ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

    – Remi.b
    Apr 25 '16 at 21:35



















That may help: superuser.com/questions/656111/…

– alpert
Apr 25 '16 at 21:18





That may help: superuser.com/questions/656111/…

– alpert
Apr 25 '16 at 21:18













Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

– ott--
Apr 25 '16 at 21:26





Your title is misleading. Why do you want to compress if you want to remove it anyway? Or do you want to move+compress it to another drive?

– ott--
Apr 25 '16 at 21:26













@ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

– Remi.b
Apr 25 '16 at 21:35







@ott Given directory d, I would like to compress it into d.zip and then rm -r d but I don't have enough storage for that. Does it make sense? Sorry if the title is misleading. I welcome suggestions to improve it. Thanks

– Remi.b
Apr 25 '16 at 21:35












4 Answers
4






active

oldest

votes


















1














You can try the --remove-files argument to tar. Say you want to compress everything on directory FOO, you would:
tar -czf FooCompressed.tar.gz --remove-files FOO



Arguments explained:




  • c: create TAR

  • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

  • f: output to file instead of of STDOUT

  • remove-files: self explanatory






share|improve this answer
























  • ** I said try because I don't know if the released space will be available on the fly.

    – Dalvenjia
    Apr 25 '16 at 23:59



















1














If the largest file in the directory is less than 300GB (the amount of free space), the easiest option is to compress files individually rather than creating an archive; something like



find directory -type f ! -name '*.xz' -print 0 | xargs -0 xz -9


will compress all non-compressed files in directory using xz at compression level 9. This will replaces files one at a time with the corresponding compressed version, adding a .xz extension (so foo.txt becomes foo.txt.xz).



In fact this can work even if the largest file is larger than the available free space, as long as you have enough files smaller than the available free space: start by compressing the smaller files, which will progressively free more and more space up, until (hopefully) you have enough free space to handle the largest file.






share|improve this answer

































    0














    I normally never make zip files, so I'm no expert, but it looks like zip by default just adds files to an archive, so something like (untested - contains an rm command = potentially dangerous - Test yourself before risking your data):

    for f in $(ls --sort=size --reverse); do zip -9 archive.zip $f; rm $f; done

    in the directory, could probably do it.






    share|improve this answer
























    • One safety valve I'd add would be to change zip...; rm... to `zip... && rm

      – Jeff Schaller
      Apr 25 '16 at 23:17






    • 3





      @JeffSchaller - and never do for f in $(ls whatever) ...

      – don_crissti
      Apr 25 '16 at 23:19






    • 2





      @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

      – Stephen Kitt
      Apr 26 '16 at 18:07



















    0















    • c: create TAR

    • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

    • f: output to file instead of of STDOUT

    • remove-files: self explanatory


    something happens on stage 1-3 and you lose all your original files on step 4






    share|improve this answer

























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f279011%2fcompress-and-remove-original-directory-with-limited-storage%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can try the --remove-files argument to tar. Say you want to compress everything on directory FOO, you would:
      tar -czf FooCompressed.tar.gz --remove-files FOO



      Arguments explained:




      • c: create TAR

      • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

      • f: output to file instead of of STDOUT

      • remove-files: self explanatory






      share|improve this answer
























      • ** I said try because I don't know if the released space will be available on the fly.

        – Dalvenjia
        Apr 25 '16 at 23:59
















      1














      You can try the --remove-files argument to tar. Say you want to compress everything on directory FOO, you would:
      tar -czf FooCompressed.tar.gz --remove-files FOO



      Arguments explained:




      • c: create TAR

      • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

      • f: output to file instead of of STDOUT

      • remove-files: self explanatory






      share|improve this answer
























      • ** I said try because I don't know if the released space will be available on the fly.

        – Dalvenjia
        Apr 25 '16 at 23:59














      1












      1








      1







      You can try the --remove-files argument to tar. Say you want to compress everything on directory FOO, you would:
      tar -czf FooCompressed.tar.gz --remove-files FOO



      Arguments explained:




      • c: create TAR

      • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

      • f: output to file instead of of STDOUT

      • remove-files: self explanatory






      share|improve this answer













      You can try the --remove-files argument to tar. Say you want to compress everything on directory FOO, you would:
      tar -czf FooCompressed.tar.gz --remove-files FOO



      Arguments explained:




      • c: create TAR

      • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

      • f: output to file instead of of STDOUT

      • remove-files: self explanatory







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 25 '16 at 23:58









      DalvenjiaDalvenjia

      1,20648




      1,20648













      • ** I said try because I don't know if the released space will be available on the fly.

        – Dalvenjia
        Apr 25 '16 at 23:59



















      • ** I said try because I don't know if the released space will be available on the fly.

        – Dalvenjia
        Apr 25 '16 at 23:59

















      ** I said try because I don't know if the released space will be available on the fly.

      – Dalvenjia
      Apr 25 '16 at 23:59





      ** I said try because I don't know if the released space will be available on the fly.

      – Dalvenjia
      Apr 25 '16 at 23:59













      1














      If the largest file in the directory is less than 300GB (the amount of free space), the easiest option is to compress files individually rather than creating an archive; something like



      find directory -type f ! -name '*.xz' -print 0 | xargs -0 xz -9


      will compress all non-compressed files in directory using xz at compression level 9. This will replaces files one at a time with the corresponding compressed version, adding a .xz extension (so foo.txt becomes foo.txt.xz).



      In fact this can work even if the largest file is larger than the available free space, as long as you have enough files smaller than the available free space: start by compressing the smaller files, which will progressively free more and more space up, until (hopefully) you have enough free space to handle the largest file.






      share|improve this answer






























        1














        If the largest file in the directory is less than 300GB (the amount of free space), the easiest option is to compress files individually rather than creating an archive; something like



        find directory -type f ! -name '*.xz' -print 0 | xargs -0 xz -9


        will compress all non-compressed files in directory using xz at compression level 9. This will replaces files one at a time with the corresponding compressed version, adding a .xz extension (so foo.txt becomes foo.txt.xz).



        In fact this can work even if the largest file is larger than the available free space, as long as you have enough files smaller than the available free space: start by compressing the smaller files, which will progressively free more and more space up, until (hopefully) you have enough free space to handle the largest file.






        share|improve this answer




























          1












          1








          1







          If the largest file in the directory is less than 300GB (the amount of free space), the easiest option is to compress files individually rather than creating an archive; something like



          find directory -type f ! -name '*.xz' -print 0 | xargs -0 xz -9


          will compress all non-compressed files in directory using xz at compression level 9. This will replaces files one at a time with the corresponding compressed version, adding a .xz extension (so foo.txt becomes foo.txt.xz).



          In fact this can work even if the largest file is larger than the available free space, as long as you have enough files smaller than the available free space: start by compressing the smaller files, which will progressively free more and more space up, until (hopefully) you have enough free space to handle the largest file.






          share|improve this answer















          If the largest file in the directory is less than 300GB (the amount of free space), the easiest option is to compress files individually rather than creating an archive; something like



          find directory -type f ! -name '*.xz' -print 0 | xargs -0 xz -9


          will compress all non-compressed files in directory using xz at compression level 9. This will replaces files one at a time with the corresponding compressed version, adding a .xz extension (so foo.txt becomes foo.txt.xz).



          In fact this can work even if the largest file is larger than the available free space, as long as you have enough files smaller than the available free space: start by compressing the smaller files, which will progressively free more and more space up, until (hopefully) you have enough free space to handle the largest file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 8 '18 at 12:33

























          answered Apr 25 '16 at 21:17









          Stephen KittStephen Kitt

          172k24386465




          172k24386465























              0














              I normally never make zip files, so I'm no expert, but it looks like zip by default just adds files to an archive, so something like (untested - contains an rm command = potentially dangerous - Test yourself before risking your data):

              for f in $(ls --sort=size --reverse); do zip -9 archive.zip $f; rm $f; done

              in the directory, could probably do it.






              share|improve this answer
























              • One safety valve I'd add would be to change zip...; rm... to `zip... && rm

                – Jeff Schaller
                Apr 25 '16 at 23:17






              • 3





                @JeffSchaller - and never do for f in $(ls whatever) ...

                – don_crissti
                Apr 25 '16 at 23:19






              • 2





                @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

                – Stephen Kitt
                Apr 26 '16 at 18:07
















              0














              I normally never make zip files, so I'm no expert, but it looks like zip by default just adds files to an archive, so something like (untested - contains an rm command = potentially dangerous - Test yourself before risking your data):

              for f in $(ls --sort=size --reverse); do zip -9 archive.zip $f; rm $f; done

              in the directory, could probably do it.






              share|improve this answer
























              • One safety valve I'd add would be to change zip...; rm... to `zip... && rm

                – Jeff Schaller
                Apr 25 '16 at 23:17






              • 3





                @JeffSchaller - and never do for f in $(ls whatever) ...

                – don_crissti
                Apr 25 '16 at 23:19






              • 2





                @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

                – Stephen Kitt
                Apr 26 '16 at 18:07














              0












              0








              0







              I normally never make zip files, so I'm no expert, but it looks like zip by default just adds files to an archive, so something like (untested - contains an rm command = potentially dangerous - Test yourself before risking your data):

              for f in $(ls --sort=size --reverse); do zip -9 archive.zip $f; rm $f; done

              in the directory, could probably do it.






              share|improve this answer













              I normally never make zip files, so I'm no expert, but it looks like zip by default just adds files to an archive, so something like (untested - contains an rm command = potentially dangerous - Test yourself before risking your data):

              for f in $(ls --sort=size --reverse); do zip -9 archive.zip $f; rm $f; done

              in the directory, could probably do it.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 25 '16 at 22:49









              HenrikHenrik

              3,6511419




              3,6511419













              • One safety valve I'd add would be to change zip...; rm... to `zip... && rm

                – Jeff Schaller
                Apr 25 '16 at 23:17






              • 3





                @JeffSchaller - and never do for f in $(ls whatever) ...

                – don_crissti
                Apr 25 '16 at 23:19






              • 2





                @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

                – Stephen Kitt
                Apr 26 '16 at 18:07



















              • One safety valve I'd add would be to change zip...; rm... to `zip... && rm

                – Jeff Schaller
                Apr 25 '16 at 23:17






              • 3





                @JeffSchaller - and never do for f in $(ls whatever) ...

                – don_crissti
                Apr 25 '16 at 23:19






              • 2





                @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

                – Stephen Kitt
                Apr 26 '16 at 18:07

















              One safety valve I'd add would be to change zip...; rm... to `zip... && rm

              – Jeff Schaller
              Apr 25 '16 at 23:17





              One safety valve I'd add would be to change zip...; rm... to `zip... && rm

              – Jeff Schaller
              Apr 25 '16 at 23:17




              3




              3





              @JeffSchaller - and never do for f in $(ls whatever) ...

              – don_crissti
              Apr 25 '16 at 23:19





              @JeffSchaller - and never do for f in $(ls whatever) ...

              – don_crissti
              Apr 25 '16 at 23:19




              2




              2





              @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

              – Stephen Kitt
              Apr 26 '16 at 18:07





              @Henrik If I remember correctly, when adding files to an existing archive, zip creates a new archive alongside the old one — so when space is short this doesn't work.

              – Stephen Kitt
              Apr 26 '16 at 18:07











              0















              • c: create TAR

              • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

              • f: output to file instead of of STDOUT

              • remove-files: self explanatory


              something happens on stage 1-3 and you lose all your original files on step 4






              share|improve this answer






























                0















                • c: create TAR

                • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

                • f: output to file instead of of STDOUT

                • remove-files: self explanatory


                something happens on stage 1-3 and you lose all your original files on step 4






                share|improve this answer




























                  0












                  0








                  0








                  • c: create TAR

                  • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

                  • f: output to file instead of of STDOUT

                  • remove-files: self explanatory


                  something happens on stage 1-3 and you lose all your original files on step 4






                  share|improve this answer
















                  • c: create TAR

                  • z: compress using GZIP, you can switch to -j for BZIP2 or -J for LZMA(xz)

                  • f: output to file instead of of STDOUT

                  • remove-files: self explanatory


                  something happens on stage 1-3 and you lose all your original files on step 4







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 6 at 1:35









                  Jeff Schaller

                  41.5k1056132




                  41.5k1056132










                  answered Feb 6 at 0:56









                  DmitryDmitry

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • 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%2funix.stackexchange.com%2fquestions%2f279011%2fcompress-and-remove-original-directory-with-limited-storage%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

                      How to make a Squid Proxy server?

                      第一次世界大戦

                      Touch on Surface Book