Moving random files using shuf and mv - Argument list too long

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 directory containing nearly 250K files, which are lots of files, and I want to move x random files to another directory.



I searched and I got the solution of using the shuf and mv commands from here and here, so basically I am using this command



$ shuf -n 5533 -e trainB/* | xargs -i mv testB/


But I'm receiving this error:



bash: /usr/bin/shuf: Argument list too long


I believe because of the large number of files, so accordingly, the argument list is too long, is there another way to do this?



I'm running on SLES12 SP2.










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a directory containing nearly 250K files, which are lots of files, and I want to move x random files to another directory.



    I searched and I got the solution of using the shuf and mv commands from here and here, so basically I am using this command



    $ shuf -n 5533 -e trainB/* | xargs -i mv testB/


    But I'm receiving this error:



    bash: /usr/bin/shuf: Argument list too long


    I believe because of the large number of files, so accordingly, the argument list is too long, is there another way to do this?



    I'm running on SLES12 SP2.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a directory containing nearly 250K files, which are lots of files, and I want to move x random files to another directory.



      I searched and I got the solution of using the shuf and mv commands from here and here, so basically I am using this command



      $ shuf -n 5533 -e trainB/* | xargs -i mv testB/


      But I'm receiving this error:



      bash: /usr/bin/shuf: Argument list too long


      I believe because of the large number of files, so accordingly, the argument list is too long, is there another way to do this?



      I'm running on SLES12 SP2.










      share|improve this question















      I have a directory containing nearly 250K files, which are lots of files, and I want to move x random files to another directory.



      I searched and I got the solution of using the shuf and mv commands from here and here, so basically I am using this command



      $ shuf -n 5533 -e trainB/* | xargs -i mv testB/


      But I'm receiving this error:



      bash: /usr/bin/shuf: Argument list too long


      I believe because of the large number of files, so accordingly, the argument list is too long, is there another way to do this?



      I'm running on SLES12 SP2.







      bash mv arguments shuf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 at 16:23









      slm♦

      239k65494665




      239k65494665










      asked Sep 3 at 16:07









      Mostafa Hussein

      615




      615




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Since you’re using SLES, you can use GNU extensions to make this safer:



          find trainB -mindepth 1 -maxdepth 1 ! -name '.*' -print0 |
          shuf -n 5533 -z |
          xargs -r0 mv -t testB


          This uses find to process file lists via pipes instead of command-line arguments, then shuffles them, limiting the output, and finally moves them to testB. The -print0, -z, and -0 options ensure nul terminators are used instead of newlines.



          Instead of find, you can use:



          printf '%s' trainB/*


          printf being built-in in bash, it is not affected by that arg list too long limitation of the execve() system call. That's potentially less efficient though as the shell needs to build the whole list and sort it while find displays the file paths unsorted as they come.






          share|improve this answer






















          • anyway, shuf is a GNU-only utility.
            – Stéphane Chazelas
            Sep 3 at 16:35










          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%2f466593%2fmoving-random-files-using-shuf-and-mv-argument-list-too-long%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Since you’re using SLES, you can use GNU extensions to make this safer:



          find trainB -mindepth 1 -maxdepth 1 ! -name '.*' -print0 |
          shuf -n 5533 -z |
          xargs -r0 mv -t testB


          This uses find to process file lists via pipes instead of command-line arguments, then shuffles them, limiting the output, and finally moves them to testB. The -print0, -z, and -0 options ensure nul terminators are used instead of newlines.



          Instead of find, you can use:



          printf '%s' trainB/*


          printf being built-in in bash, it is not affected by that arg list too long limitation of the execve() system call. That's potentially less efficient though as the shell needs to build the whole list and sort it while find displays the file paths unsorted as they come.






          share|improve this answer






















          • anyway, shuf is a GNU-only utility.
            – Stéphane Chazelas
            Sep 3 at 16:35














          up vote
          2
          down vote



          accepted










          Since you’re using SLES, you can use GNU extensions to make this safer:



          find trainB -mindepth 1 -maxdepth 1 ! -name '.*' -print0 |
          shuf -n 5533 -z |
          xargs -r0 mv -t testB


          This uses find to process file lists via pipes instead of command-line arguments, then shuffles them, limiting the output, and finally moves them to testB. The -print0, -z, and -0 options ensure nul terminators are used instead of newlines.



          Instead of find, you can use:



          printf '%s' trainB/*


          printf being built-in in bash, it is not affected by that arg list too long limitation of the execve() system call. That's potentially less efficient though as the shell needs to build the whole list and sort it while find displays the file paths unsorted as they come.






          share|improve this answer






















          • anyway, shuf is a GNU-only utility.
            – Stéphane Chazelas
            Sep 3 at 16:35












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Since you’re using SLES, you can use GNU extensions to make this safer:



          find trainB -mindepth 1 -maxdepth 1 ! -name '.*' -print0 |
          shuf -n 5533 -z |
          xargs -r0 mv -t testB


          This uses find to process file lists via pipes instead of command-line arguments, then shuffles them, limiting the output, and finally moves them to testB. The -print0, -z, and -0 options ensure nul terminators are used instead of newlines.



          Instead of find, you can use:



          printf '%s' trainB/*


          printf being built-in in bash, it is not affected by that arg list too long limitation of the execve() system call. That's potentially less efficient though as the shell needs to build the whole list and sort it while find displays the file paths unsorted as they come.






          share|improve this answer














          Since you’re using SLES, you can use GNU extensions to make this safer:



          find trainB -mindepth 1 -maxdepth 1 ! -name '.*' -print0 |
          shuf -n 5533 -z |
          xargs -r0 mv -t testB


          This uses find to process file lists via pipes instead of command-line arguments, then shuffles them, limiting the output, and finally moves them to testB. The -print0, -z, and -0 options ensure nul terminators are used instead of newlines.



          Instead of find, you can use:



          printf '%s' trainB/*


          printf being built-in in bash, it is not affected by that arg list too long limitation of the execve() system call. That's potentially less efficient though as the shell needs to build the whole list and sort it while find displays the file paths unsorted as they come.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 3 at 16:37









          Stéphane Chazelas

          286k53527866




          286k53527866










          answered Sep 3 at 16:10









          Stephen Kitt

          147k22321389




          147k22321389











          • anyway, shuf is a GNU-only utility.
            – Stéphane Chazelas
            Sep 3 at 16:35
















          • anyway, shuf is a GNU-only utility.
            – Stéphane Chazelas
            Sep 3 at 16:35















          anyway, shuf is a GNU-only utility.
          – Stéphane Chazelas
          Sep 3 at 16:35




          anyway, shuf is a GNU-only utility.
          – Stéphane Chazelas
          Sep 3 at 16:35

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466593%2fmoving-random-files-using-shuf-and-mv-argument-list-too-long%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