How do I add files to a folder until a certain size limit is reached?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have a folder with 1.5 million files (mail files) of various sizes. I want to create a directory, for example a, and move (mv) some of these files (which all start with a numeric timestamp) into folder a, until folder a is of up to a specified size. How would I do this?



Avoiding any procedural for or while loops would be a plus (i.e. one line) but anything that works is great!







share|improve this question






















  • post one of your exemplary filenames with a numeric timestamp
    – RomanPerekhrest
    Nov 4 '17 at 19:24










  • I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
    – Hauke Laging
    Nov 4 '17 at 19:45










  • Don't reinvent the wheel, use datapacker
    – Ipor Sircer
    Nov 4 '17 at 19:45














up vote
2
down vote

favorite












I have a folder with 1.5 million files (mail files) of various sizes. I want to create a directory, for example a, and move (mv) some of these files (which all start with a numeric timestamp) into folder a, until folder a is of up to a specified size. How would I do this?



Avoiding any procedural for or while loops would be a plus (i.e. one line) but anything that works is great!







share|improve this question






















  • post one of your exemplary filenames with a numeric timestamp
    – RomanPerekhrest
    Nov 4 '17 at 19:24










  • I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
    – Hauke Laging
    Nov 4 '17 at 19:45










  • Don't reinvent the wheel, use datapacker
    – Ipor Sircer
    Nov 4 '17 at 19:45












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a folder with 1.5 million files (mail files) of various sizes. I want to create a directory, for example a, and move (mv) some of these files (which all start with a numeric timestamp) into folder a, until folder a is of up to a specified size. How would I do this?



Avoiding any procedural for or while loops would be a plus (i.e. one line) but anything that works is great!







share|improve this question














I have a folder with 1.5 million files (mail files) of various sizes. I want to create a directory, for example a, and move (mv) some of these files (which all start with a numeric timestamp) into folder a, until folder a is of up to a specified size. How would I do this?



Avoiding any procedural for or while loops would be a plus (i.e. one line) but anything that works is great!









share|improve this question













share|improve this question




share|improve this question








edited Nov 4 '17 at 20:50









Time4Tea

866119




866119










asked Nov 4 '17 at 18:23









Oliver Williams

3841513




3841513











  • post one of your exemplary filenames with a numeric timestamp
    – RomanPerekhrest
    Nov 4 '17 at 19:24










  • I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
    – Hauke Laging
    Nov 4 '17 at 19:45










  • Don't reinvent the wheel, use datapacker
    – Ipor Sircer
    Nov 4 '17 at 19:45
















  • post one of your exemplary filenames with a numeric timestamp
    – RomanPerekhrest
    Nov 4 '17 at 19:24










  • I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
    – Hauke Laging
    Nov 4 '17 at 19:45










  • Don't reinvent the wheel, use datapacker
    – Ipor Sircer
    Nov 4 '17 at 19:45















post one of your exemplary filenames with a numeric timestamp
– RomanPerekhrest
Nov 4 '17 at 19:24




post one of your exemplary filenames with a numeric timestamp
– RomanPerekhrest
Nov 4 '17 at 19:24












I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
– Hauke Laging
Nov 4 '17 at 19:45




I doubt that a simple approach is possible. There is implicit cumulated size measuring with (a) the size of a volume / filesystem, (b) with quota, and (c) with ulimits. But none of those works with mv i.e. existing files only with newly created (or changed) ones.
– Hauke Laging
Nov 4 '17 at 19:45












Don't reinvent the wheel, use datapacker
– Ipor Sircer
Nov 4 '17 at 19:45




Don't reinvent the wheel, use datapacker
– Ipor Sircer
Nov 4 '17 at 19:45










2 Answers
2






active

oldest

votes

















up vote
1
down vote













max_k=1000
sum=0
target_dir_path="/target/dir"
find . -mindepth 1 -maxdepth 1 -type f -printf '%k %p' |
while IFS= read -r -d $'' line; do
size="$line%% *"
path="$line#* "
sum=$((sum+size))
if [ "$sum" -le "$max_k" ]; then
printf "%s" "$path"
else
break
fi
done | xargs -0 echo mv -t "target_dir_path"





share|improve this answer



























    up vote
    -1
    down vote













    You can create special LV (or partition) of desired size and mount it under your 'a' directory.



    If you want to leave some spare space, combine it with quota.



    There was similar question:



    https://stackoverflow.com/questions/8148715/how-to-set-limit-on-directory-size-in-linux






    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f402538%2fhow-do-i-add-files-to-a-folder-until-a-certain-size-limit-is-reached%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      max_k=1000
      sum=0
      target_dir_path="/target/dir"
      find . -mindepth 1 -maxdepth 1 -type f -printf '%k %p' |
      while IFS= read -r -d $'' line; do
      size="$line%% *"
      path="$line#* "
      sum=$((sum+size))
      if [ "$sum" -le "$max_k" ]; then
      printf "%s" "$path"
      else
      break
      fi
      done | xargs -0 echo mv -t "target_dir_path"





      share|improve this answer
























        up vote
        1
        down vote













        max_k=1000
        sum=0
        target_dir_path="/target/dir"
        find . -mindepth 1 -maxdepth 1 -type f -printf '%k %p' |
        while IFS= read -r -d $'' line; do
        size="$line%% *"
        path="$line#* "
        sum=$((sum+size))
        if [ "$sum" -le "$max_k" ]; then
        printf "%s" "$path"
        else
        break
        fi
        done | xargs -0 echo mv -t "target_dir_path"





        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          max_k=1000
          sum=0
          target_dir_path="/target/dir"
          find . -mindepth 1 -maxdepth 1 -type f -printf '%k %p' |
          while IFS= read -r -d $'' line; do
          size="$line%% *"
          path="$line#* "
          sum=$((sum+size))
          if [ "$sum" -le "$max_k" ]; then
          printf "%s" "$path"
          else
          break
          fi
          done | xargs -0 echo mv -t "target_dir_path"





          share|improve this answer












          max_k=1000
          sum=0
          target_dir_path="/target/dir"
          find . -mindepth 1 -maxdepth 1 -type f -printf '%k %p' |
          while IFS= read -r -d $'' line; do
          size="$line%% *"
          path="$line#* "
          sum=$((sum+size))
          if [ "$sum" -le "$max_k" ]; then
          printf "%s" "$path"
          else
          break
          fi
          done | xargs -0 echo mv -t "target_dir_path"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 4 '17 at 20:18









          Hauke Laging

          53.6k1282130




          53.6k1282130






















              up vote
              -1
              down vote













              You can create special LV (or partition) of desired size and mount it under your 'a' directory.



              If you want to leave some spare space, combine it with quota.



              There was similar question:



              https://stackoverflow.com/questions/8148715/how-to-set-limit-on-directory-size-in-linux






              share|improve this answer
























                up vote
                -1
                down vote













                You can create special LV (or partition) of desired size and mount it under your 'a' directory.



                If you want to leave some spare space, combine it with quota.



                There was similar question:



                https://stackoverflow.com/questions/8148715/how-to-set-limit-on-directory-size-in-linux






                share|improve this answer






















                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  You can create special LV (or partition) of desired size and mount it under your 'a' directory.



                  If you want to leave some spare space, combine it with quota.



                  There was similar question:



                  https://stackoverflow.com/questions/8148715/how-to-set-limit-on-directory-size-in-linux






                  share|improve this answer












                  You can create special LV (or partition) of desired size and mount it under your 'a' directory.



                  If you want to leave some spare space, combine it with quota.



                  There was similar question:



                  https://stackoverflow.com/questions/8148715/how-to-set-limit-on-directory-size-in-linux







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 4 '17 at 19:44









                  Jaroslav Kucera

                  4,3754621




                  4,3754621



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402538%2fhow-do-i-add-files-to-a-folder-until-a-certain-size-limit-is-reached%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay