Linux: remove duplicate strings from for and cut












0















So I have a file structure that looks like this:




  • video


    • abc.avi

    • party_2014.h264

    • test.avi

    • xxx.mkv




I need to create a directory for each extension and move the corresponding file there so it looks like this




  • video


    • video-avi


      • abc.avi

      • test.avi



    • video-h264


      • party_2014.h264



    • video-mkv


      • xxx.mkv






so far I have this code:



for i in $(find video -name "*.*" -print)
do cut -d'.' -f2 <<< "$i" | uniq -c;
done


this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?










share|improve this question



























    0















    So I have a file structure that looks like this:




    • video


      • abc.avi

      • party_2014.h264

      • test.avi

      • xxx.mkv




    I need to create a directory for each extension and move the corresponding file there so it looks like this




    • video


      • video-avi


        • abc.avi

        • test.avi



      • video-h264


        • party_2014.h264



      • video-mkv


        • xxx.mkv






    so far I have this code:



    for i in $(find video -name "*.*" -print)
    do cut -d'.' -f2 <<< "$i" | uniq -c;
    done


    this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?










    share|improve this question

























      0












      0








      0








      So I have a file structure that looks like this:




      • video


        • abc.avi

        • party_2014.h264

        • test.avi

        • xxx.mkv




      I need to create a directory for each extension and move the corresponding file there so it looks like this




      • video


        • video-avi


          • abc.avi

          • test.avi



        • video-h264


          • party_2014.h264



        • video-mkv


          • xxx.mkv






      so far I have this code:



      for i in $(find video -name "*.*" -print)
      do cut -d'.' -f2 <<< "$i" | uniq -c;
      done


      this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?










      share|improve this question














      So I have a file structure that looks like this:




      • video


        • abc.avi

        • party_2014.h264

        • test.avi

        • xxx.mkv




      I need to create a directory for each extension and move the corresponding file there so it looks like this




      • video


        • video-avi


          • abc.avi

          • test.avi



        • video-h264


          • party_2014.h264



        • video-mkv


          • xxx.mkv






      so far I have this code:



      for i in $(find video -name "*.*" -print)
      do cut -d'.' -f2 <<< "$i" | uniq -c;
      done


      this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?







      linux bash for-loop find






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 19:02









      Lada1208Lada1208

      274




      274
























          1 Answer
          1






          active

          oldest

          votes


















          3














          This simple script might work for you:



          #!/usr/bin/env bash    

          shopt -s nullglob

          dir=video
          for file in "$dir"/*.*; do
          out=$dir/video-${file##*.}

          [[ -d $out ]] || mkdir -- "$out" || continue
          mv -- "$file" "$out"
          done


          You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}.






          share|improve this answer


























          • Yep this worked, thanks

            – Lada1208
            Jan 2 at 19:18






          • 1





            Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

            – David C. Rankin
            Jan 2 at 19:24






          • 1





            Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

            – David C. Rankin
            Jan 2 at 19:25








          • 2





            @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

            – PesaThe
            Jan 2 at 19:27






          • 1





            @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

            – PesaThe
            Jan 2 at 19:33














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f54011790%2flinux-remove-duplicate-strings-from-for-and-cut%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          This simple script might work for you:



          #!/usr/bin/env bash    

          shopt -s nullglob

          dir=video
          for file in "$dir"/*.*; do
          out=$dir/video-${file##*.}

          [[ -d $out ]] || mkdir -- "$out" || continue
          mv -- "$file" "$out"
          done


          You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}.






          share|improve this answer


























          • Yep this worked, thanks

            – Lada1208
            Jan 2 at 19:18






          • 1





            Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

            – David C. Rankin
            Jan 2 at 19:24






          • 1





            Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

            – David C. Rankin
            Jan 2 at 19:25








          • 2





            @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

            – PesaThe
            Jan 2 at 19:27






          • 1





            @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

            – PesaThe
            Jan 2 at 19:33


















          3














          This simple script might work for you:



          #!/usr/bin/env bash    

          shopt -s nullglob

          dir=video
          for file in "$dir"/*.*; do
          out=$dir/video-${file##*.}

          [[ -d $out ]] || mkdir -- "$out" || continue
          mv -- "$file" "$out"
          done


          You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}.






          share|improve this answer


























          • Yep this worked, thanks

            – Lada1208
            Jan 2 at 19:18






          • 1





            Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

            – David C. Rankin
            Jan 2 at 19:24






          • 1





            Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

            – David C. Rankin
            Jan 2 at 19:25








          • 2





            @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

            – PesaThe
            Jan 2 at 19:27






          • 1





            @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

            – PesaThe
            Jan 2 at 19:33
















          3












          3








          3







          This simple script might work for you:



          #!/usr/bin/env bash    

          shopt -s nullglob

          dir=video
          for file in "$dir"/*.*; do
          out=$dir/video-${file##*.}

          [[ -d $out ]] || mkdir -- "$out" || continue
          mv -- "$file" "$out"
          done


          You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}.






          share|improve this answer















          This simple script might work for you:



          #!/usr/bin/env bash    

          shopt -s nullglob

          dir=video
          for file in "$dir"/*.*; do
          out=$dir/video-${file##*.}

          [[ -d $out ]] || mkdir -- "$out" || continue
          mv -- "$file" "$out"
          done


          You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 19:39

























          answered Jan 2 at 19:15









          PesaThePesaThe

          5,8651529




          5,8651529













          • Yep this worked, thanks

            – Lada1208
            Jan 2 at 19:18






          • 1





            Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

            – David C. Rankin
            Jan 2 at 19:24






          • 1





            Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

            – David C. Rankin
            Jan 2 at 19:25








          • 2





            @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

            – PesaThe
            Jan 2 at 19:27






          • 1





            @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

            – PesaThe
            Jan 2 at 19:33





















          • Yep this worked, thanks

            – Lada1208
            Jan 2 at 19:18






          • 1





            Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

            – David C. Rankin
            Jan 2 at 19:24






          • 1





            Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

            – David C. Rankin
            Jan 2 at 19:25








          • 2





            @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

            – PesaThe
            Jan 2 at 19:27






          • 1





            @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

            – PesaThe
            Jan 2 at 19:33



















          Yep this worked, thanks

          – Lada1208
          Jan 2 at 19:18





          Yep this worked, thanks

          – Lada1208
          Jan 2 at 19:18




          1




          1





          Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

          – David C. Rankin
          Jan 2 at 19:24





          Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See man bash under Parameter Expansion.

          – David C. Rankin
          Jan 2 at 19:24




          1




          1





          Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

          – David C. Rankin
          Jan 2 at 19:25







          Tweak -- to protect against whitespace out="$dir/video-${file##*.}" (you cover it everywhere else)

          – David C. Rankin
          Jan 2 at 19:25






          2




          2





          @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

          – PesaThe
          Jan 2 at 19:27





          @DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)

          – PesaThe
          Jan 2 at 19:27




          1




          1





          @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

          – PesaThe
          Jan 2 at 19:33







          @DavidC.Rankin True, it's a force of habit though :) Just like not quoting $out inside [[...]].

          – PesaThe
          Jan 2 at 19:33






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f54011790%2flinux-remove-duplicate-strings-from-for-and-cut%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith