Rename initial string in filename











up vote
0
down vote

favorite












I need to dynamically remove first part of name and copy to other locations.
My file names are like



Voice_XYZZ_1_1801.pdf
Voice_XYZZ_1_1801.ndx
Electon_MNOPQ_1_1801.pdf
Electon_MNOPQ_1_1801.ndx


at run time either Voice can generate or Electon or both.
I need to remove the initial part from file names and they should look like



XYZZ_1_1801.pdf
XYZZ_1_1801.ndx
MNOPQ_1_1801.pdf
MNOPQ_1_1801.ndx


Which command I can use to achieve this?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I need to dynamically remove first part of name and copy to other locations.
    My file names are like



    Voice_XYZZ_1_1801.pdf
    Voice_XYZZ_1_1801.ndx
    Electon_MNOPQ_1_1801.pdf
    Electon_MNOPQ_1_1801.ndx


    at run time either Voice can generate or Electon or both.
    I need to remove the initial part from file names and they should look like



    XYZZ_1_1801.pdf
    XYZZ_1_1801.ndx
    MNOPQ_1_1801.pdf
    MNOPQ_1_1801.ndx


    Which command I can use to achieve this?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to dynamically remove first part of name and copy to other locations.
      My file names are like



      Voice_XYZZ_1_1801.pdf
      Voice_XYZZ_1_1801.ndx
      Electon_MNOPQ_1_1801.pdf
      Electon_MNOPQ_1_1801.ndx


      at run time either Voice can generate or Electon or both.
      I need to remove the initial part from file names and they should look like



      XYZZ_1_1801.pdf
      XYZZ_1_1801.ndx
      MNOPQ_1_1801.pdf
      MNOPQ_1_1801.ndx


      Which command I can use to achieve this?










      share|improve this question















      I need to dynamically remove first part of name and copy to other locations.
      My file names are like



      Voice_XYZZ_1_1801.pdf
      Voice_XYZZ_1_1801.ndx
      Electon_MNOPQ_1_1801.pdf
      Electon_MNOPQ_1_1801.ndx


      at run time either Voice can generate or Electon or both.
      I need to remove the initial part from file names and they should look like



      XYZZ_1_1801.pdf
      XYZZ_1_1801.ndx
      MNOPQ_1_1801.pdf
      MNOPQ_1_1801.ndx


      Which command I can use to achieve this?







      shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      38.2k1475125




      38.2k1475125










      asked May 1 at 1:11









      SHa

      6




      6






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f using ${f#*_}



          Ex.



          $ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx

          $ for f in Voice_* Electon_*; do echo "${f#*_}"; done
          XYZZ_1_1801.ndx
          XYZZ_1_1801.pdf
          MNOPQ_1_1801.ndx
          MNOPQ_1_1801.pdf


          You can use this along with the mv command to rename files e.g.



          for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done


          Copying to another location may be done similarly with the cp command e.g.



          for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done





          share|improve this answer





















          • I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
            – SHa
            May 1 at 16:31












          • @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
            – steeldriver
            May 1 at 17:53










          • This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
            – SHa
            May 1 at 18:11












          • The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
            – SHa
            May 1 at 18:17


















          up vote
          0
          down vote













          This can be done very easily from the command-line by calling a Perl one-liner:



          ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'


          Here's an explanation for the code:





          • Send all the filenames that have '_' in their name to the next command



            ls _ |




          • For each of the strings being passed in, do the command



            perl -ne




          • Remove the newline from the filename (leftover from running 'ls' command)



            chomp($_);




          • Remove the first part of the word, searching until you find a '_'



            $_ =~ s/[^]*//;




          • Rename the file



            rename $orig,$_;








          share|improve this answer





















          • I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
            – SHa
            May 1 at 13:59










          • The perl command is not working in .ksh script. I tried multiple times, but name never changes.
            – SHa
            May 1 at 17:34










          • Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
            – mdiehl13
            May 2 at 15:22












          • which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
            – mdiehl13
            May 2 at 15:24













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


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441024%2frename-initial-string-in-filename%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f using ${f#*_}



          Ex.



          $ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx

          $ for f in Voice_* Electon_*; do echo "${f#*_}"; done
          XYZZ_1_1801.ndx
          XYZZ_1_1801.pdf
          MNOPQ_1_1801.ndx
          MNOPQ_1_1801.pdf


          You can use this along with the mv command to rename files e.g.



          for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done


          Copying to another location may be done similarly with the cp command e.g.



          for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done





          share|improve this answer





















          • I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
            – SHa
            May 1 at 16:31












          • @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
            – steeldriver
            May 1 at 17:53










          • This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
            – SHa
            May 1 at 18:11












          • The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
            – SHa
            May 1 at 18:17















          up vote
          0
          down vote













          Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f using ${f#*_}



          Ex.



          $ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx

          $ for f in Voice_* Electon_*; do echo "${f#*_}"; done
          XYZZ_1_1801.ndx
          XYZZ_1_1801.pdf
          MNOPQ_1_1801.ndx
          MNOPQ_1_1801.pdf


          You can use this along with the mv command to rename files e.g.



          for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done


          Copying to another location may be done similarly with the cp command e.g.



          for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done





          share|improve this answer





















          • I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
            – SHa
            May 1 at 16:31












          • @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
            – steeldriver
            May 1 at 17:53










          • This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
            – SHa
            May 1 at 18:11












          • The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
            – SHa
            May 1 at 18:17













          up vote
          0
          down vote










          up vote
          0
          down vote









          Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f using ${f#*_}



          Ex.



          $ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx

          $ for f in Voice_* Electon_*; do echo "${f#*_}"; done
          XYZZ_1_1801.ndx
          XYZZ_1_1801.pdf
          MNOPQ_1_1801.ndx
          MNOPQ_1_1801.pdf


          You can use this along with the mv command to rename files e.g.



          for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done


          Copying to another location may be done similarly with the cp command e.g.



          for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done





          share|improve this answer












          Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f using ${f#*_}



          Ex.



          $ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx

          $ for f in Voice_* Electon_*; do echo "${f#*_}"; done
          XYZZ_1_1801.ndx
          XYZZ_1_1801.pdf
          MNOPQ_1_1801.ndx
          MNOPQ_1_1801.pdf


          You can use this along with the mv command to rename files e.g.



          for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done


          Copying to another location may be done similarly with the cp command e.g.



          for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 1 at 2:06









          steeldriver

          33.6k34982




          33.6k34982












          • I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
            – SHa
            May 1 at 16:31












          • @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
            – steeldriver
            May 1 at 17:53










          • This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
            – SHa
            May 1 at 18:11












          • The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
            – SHa
            May 1 at 18:17


















          • I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
            – SHa
            May 1 at 16:31












          • @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
            – steeldriver
            May 1 at 17:53










          • This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
            – SHa
            May 1 at 18:11












          • The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
            – SHa
            May 1 at 18:17
















          I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
          – SHa
          May 1 at 16:31






          I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
          – SHa
          May 1 at 16:31














          @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
          – steeldriver
          May 1 at 17:53




          @SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
          – steeldriver
          May 1 at 17:53












          This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
          – SHa
          May 1 at 18:11






          This is what I have done in script. for filename in find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf' do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
          – SHa
          May 1 at 18:11














          The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
          – SHa
          May 1 at 18:17




          The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
          – SHa
          May 1 at 18:17












          up vote
          0
          down vote













          This can be done very easily from the command-line by calling a Perl one-liner:



          ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'


          Here's an explanation for the code:





          • Send all the filenames that have '_' in their name to the next command



            ls _ |




          • For each of the strings being passed in, do the command



            perl -ne




          • Remove the newline from the filename (leftover from running 'ls' command)



            chomp($_);




          • Remove the first part of the word, searching until you find a '_'



            $_ =~ s/[^]*//;




          • Rename the file



            rename $orig,$_;








          share|improve this answer





















          • I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
            – SHa
            May 1 at 13:59










          • The perl command is not working in .ksh script. I tried multiple times, but name never changes.
            – SHa
            May 1 at 17:34










          • Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
            – mdiehl13
            May 2 at 15:22












          • which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
            – mdiehl13
            May 2 at 15:24

















          up vote
          0
          down vote













          This can be done very easily from the command-line by calling a Perl one-liner:



          ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'


          Here's an explanation for the code:





          • Send all the filenames that have '_' in their name to the next command



            ls _ |




          • For each of the strings being passed in, do the command



            perl -ne




          • Remove the newline from the filename (leftover from running 'ls' command)



            chomp($_);




          • Remove the first part of the word, searching until you find a '_'



            $_ =~ s/[^]*//;




          • Rename the file



            rename $orig,$_;








          share|improve this answer





















          • I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
            – SHa
            May 1 at 13:59










          • The perl command is not working in .ksh script. I tried multiple times, but name never changes.
            – SHa
            May 1 at 17:34










          • Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
            – mdiehl13
            May 2 at 15:22












          • which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
            – mdiehl13
            May 2 at 15:24















          up vote
          0
          down vote










          up vote
          0
          down vote









          This can be done very easily from the command-line by calling a Perl one-liner:



          ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'


          Here's an explanation for the code:





          • Send all the filenames that have '_' in their name to the next command



            ls _ |




          • For each of the strings being passed in, do the command



            perl -ne




          • Remove the newline from the filename (leftover from running 'ls' command)



            chomp($_);




          • Remove the first part of the word, searching until you find a '_'



            $_ =~ s/[^]*//;




          • Rename the file



            rename $orig,$_;








          share|improve this answer












          This can be done very easily from the command-line by calling a Perl one-liner:



          ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'


          Here's an explanation for the code:





          • Send all the filenames that have '_' in their name to the next command



            ls _ |




          • For each of the strings being passed in, do the command



            perl -ne




          • Remove the newline from the filename (leftover from running 'ls' command)



            chomp($_);




          • Remove the first part of the word, searching until you find a '_'



            $_ =~ s/[^]*//;




          • Rename the file



            rename $orig,$_;









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 1 at 4:37









          mdiehl13

          1132




          1132












          • I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
            – SHa
            May 1 at 13:59










          • The perl command is not working in .ksh script. I tried multiple times, but name never changes.
            – SHa
            May 1 at 17:34










          • Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
            – mdiehl13
            May 2 at 15:22












          • which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
            – mdiehl13
            May 2 at 15:24




















          • I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
            – SHa
            May 1 at 13:59










          • The perl command is not working in .ksh script. I tried multiple times, but name never changes.
            – SHa
            May 1 at 17:34










          • Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
            – mdiehl13
            May 2 at 15:22












          • which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
            – mdiehl13
            May 2 at 15:24


















          I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
          – SHa
          May 1 at 13:59




          I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
          – SHa
          May 1 at 13:59












          The perl command is not working in .ksh script. I tried multiple times, but name never changes.
          – SHa
          May 1 at 17:34




          The perl command is not working in .ksh script. I tried multiple times, but name never changes.
          – SHa
          May 1 at 17:34












          Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
          – mdiehl13
          May 2 at 15:22






          Hmm, I ran KSH and the command worked. Maybe add print($_); right before chomp($_) and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
          – mdiehl13
          May 2 at 15:22














          which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
          – mdiehl13
          May 2 at 15:24






          which Perl are you using? perl -v Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
          – mdiehl13
          May 2 at 15:24




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441024%2frename-initial-string-in-filename%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

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt