cutting a directory name with multiple instances of the same delimiter (Shell)

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











up vote
0
down vote

favorite












I have directories I want to rename,
for example: 10-00017_S9_L001_plasmidspades_careful



I want to rename them to just the 10-00017_S9_L001 part



I've tried to use the cut command echo "$filename" | cut -d'_' -f3, however that leaves me with just the L001 part of the filename.



Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.



Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?










share|improve this question









New contributor




A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    0
    down vote

    favorite












    I have directories I want to rename,
    for example: 10-00017_S9_L001_plasmidspades_careful



    I want to rename them to just the 10-00017_S9_L001 part



    I've tried to use the cut command echo "$filename" | cut -d'_' -f3, however that leaves me with just the L001 part of the filename.



    Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.



    Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?










    share|improve this question









    New contributor




    A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have directories I want to rename,
      for example: 10-00017_S9_L001_plasmidspades_careful



      I want to rename them to just the 10-00017_S9_L001 part



      I've tried to use the cut command echo "$filename" | cut -d'_' -f3, however that leaves me with just the L001 part of the filename.



      Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.



      Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?










      share|improve this question









      New contributor




      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have directories I want to rename,
      for example: 10-00017_S9_L001_plasmidspades_careful



      I want to rename them to just the 10-00017_S9_L001 part



      I've tried to use the cut command echo "$filename" | cut -d'_' -f3, however that leaves me with just the L001 part of the filename.



      Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.



      Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?







      cut






      share|improve this question









      New contributor




      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      38.2k1475123




      38.2k1475123






      New contributor




      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      A. Murray

      61




      61




      New contributor




      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      A. Murray is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          The best solution is to use rename (Debian) or prename (RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:



          rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *


          or a more lenient:



          rename -n 's/(.+_.+_.+_.+)_.+/$1/' *


          With -n it just shows what it would do, remove (or better, replace by -v) for actual execution.



          Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.






          share|improve this answer






















          • In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
            – ctrl-alt-delor
            2 days ago

















          up vote
          0
          down vote













          A rather low tech solution:



          • Use ls or find to create a list of directory in a file

          • Use an editor you are familiar with to replace each line (that just contains oldname by mv oldname newname)


          • Execute the script:



            . list_of_moves


            (note leading dot-space)







          share|improve this answer



























            up vote
            0
            down vote













            Put all directories in one file and execute below command to get it renamed



            awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh


            Tested for above mentioned dirtectoy and it worked fine



            echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh





            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: 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
              );



              );






              A. Murray is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482153%2fcutting-a-directory-name-with-multiple-instances-of-the-same-delimiter-shell%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              The best solution is to use rename (Debian) or prename (RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:



              rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *


              or a more lenient:



              rename -n 's/(.+_.+_.+_.+)_.+/$1/' *


              With -n it just shows what it would do, remove (or better, replace by -v) for actual execution.



              Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.






              share|improve this answer






















              • In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
                – ctrl-alt-delor
                2 days ago














              up vote
              1
              down vote













              The best solution is to use rename (Debian) or prename (RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:



              rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *


              or a more lenient:



              rename -n 's/(.+_.+_.+_.+)_.+/$1/' *


              With -n it just shows what it would do, remove (or better, replace by -v) for actual execution.



              Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.






              share|improve this answer






















              • In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
                – ctrl-alt-delor
                2 days ago












              up vote
              1
              down vote










              up vote
              1
              down vote









              The best solution is to use rename (Debian) or prename (RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:



              rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *


              or a more lenient:



              rename -n 's/(.+_.+_.+_.+)_.+/$1/' *


              With -n it just shows what it would do, remove (or better, replace by -v) for actual execution.



              Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.






              share|improve this answer














              The best solution is to use rename (Debian) or prename (RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:



              rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *


              or a more lenient:



              rename -n 's/(.+_.+_.+_.+)_.+/$1/' *


              With -n it just shows what it would do, remove (or better, replace by -v) for actual execution.



              Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered 2 days ago









              xenoid

              2,4491723




              2,4491723











              • In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
                – ctrl-alt-delor
                2 days ago
















              • In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
                – ctrl-alt-delor
                2 days ago















              In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
              – ctrl-alt-delor
              2 days ago




              In is Larry Wall's rename. Some systems have a different rename. rename is sed for file-names.
              – ctrl-alt-delor
              2 days ago












              up vote
              0
              down vote













              A rather low tech solution:



              • Use ls or find to create a list of directory in a file

              • Use an editor you are familiar with to replace each line (that just contains oldname by mv oldname newname)


              • Execute the script:



                . list_of_moves


                (note leading dot-space)







              share|improve this answer
























                up vote
                0
                down vote













                A rather low tech solution:



                • Use ls or find to create a list of directory in a file

                • Use an editor you are familiar with to replace each line (that just contains oldname by mv oldname newname)


                • Execute the script:



                  . list_of_moves


                  (note leading dot-space)







                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  A rather low tech solution:



                  • Use ls or find to create a list of directory in a file

                  • Use an editor you are familiar with to replace each line (that just contains oldname by mv oldname newname)


                  • Execute the script:



                    . list_of_moves


                    (note leading dot-space)







                  share|improve this answer












                  A rather low tech solution:



                  • Use ls or find to create a list of directory in a file

                  • Use an editor you are familiar with to replace each line (that just contains oldname by mv oldname newname)


                  • Execute the script:



                    . list_of_moves


                    (note leading dot-space)








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  xenoid

                  2,4491723




                  2,4491723




















                      up vote
                      0
                      down vote













                      Put all directories in one file and execute below command to get it renamed



                      awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh


                      Tested for above mentioned dirtectoy and it worked fine



                      echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Put all directories in one file and execute below command to get it renamed



                        awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh


                        Tested for above mentioned dirtectoy and it worked fine



                        echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Put all directories in one file and execute below command to get it renamed



                          awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh


                          Tested for above mentioned dirtectoy and it worked fine



                          echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh





                          share|improve this answer












                          Put all directories in one file and execute below command to get it renamed



                          awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh


                          Tested for above mentioned dirtectoy and it worked fine



                          echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          Praveen Kumar BS

                          1,055138




                          1,055138




















                              A. Murray is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              A. Murray is a new contributor. Be nice, and check out our Code of Conduct.












                              A. Murray is a new contributor. Be nice, and check out our Code of Conduct.











                              A. Murray is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482153%2fcutting-a-directory-name-with-multiple-instances-of-the-same-delimiter-shell%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 check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay