How can I strip out a numeric prefix from multiple filenames?

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











up vote
1
down vote

favorite
1












I am new to Unix. I have >2000 files which are named like



123-FILENAME_TEXT_M101K_20150929.CSV


where 123 can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123- should be removed from the example name)



I have tried mv **-FILENAME* FILENAME*.
rename is not an available function



Any advice would be greatly appreciated.







share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    I am new to Unix. I have >2000 files which are named like



    123-FILENAME_TEXT_M101K_20150929.CSV


    where 123 can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123- should be removed from the example name)



    I have tried mv **-FILENAME* FILENAME*.
    rename is not an available function



    Any advice would be greatly appreciated.







    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I am new to Unix. I have >2000 files which are named like



      123-FILENAME_TEXT_M101K_20150929.CSV


      where 123 can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123- should be removed from the example name)



      I have tried mv **-FILENAME* FILENAME*.
      rename is not an available function



      Any advice would be greatly appreciated.







      share|improve this question














      I am new to Unix. I have >2000 files which are named like



      123-FILENAME_TEXT_M101K_20150929.CSV


      where 123 can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123- should be removed from the example name)



      I have tried mv **-FILENAME* FILENAME*.
      rename is not an available function



      Any advice would be greatly appreciated.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 31 at 14:35









      ilkkachu

      49.8k674137




      49.8k674137










      asked Jan 31 at 14:22









      Kim

      84




      84




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You could try something like this, it will remove everything before the first -



          for file in *-*.CSV
          do
          newName="$file#*-"
          mv -- "$file" "$newName"
          done





          share|improve this answer


















          • 1




            The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
            – ilkkachu
            Jan 31 at 14:42











          • Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
            – mmoisse
            Jan 31 at 14:45






          • 1




            Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
            – Stéphane Chazelas
            Jan 31 at 15:12











          • Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
            – Kim
            Jan 31 at 15:41










          • Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
            – Kim
            Jan 31 at 15:56

















          up vote
          0
          down vote













          I have tried by using combination of sed,awk and find . Tested and working fine



          find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| 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: 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%2f420972%2fhow-can-i-strip-out-a-numeric-prefix-from-multiple-filenames%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
            4
            down vote



            accepted










            You could try something like this, it will remove everything before the first -



            for file in *-*.CSV
            do
            newName="$file#*-"
            mv -- "$file" "$newName"
            done





            share|improve this answer


















            • 1




              The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
              – ilkkachu
              Jan 31 at 14:42











            • Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
              – mmoisse
              Jan 31 at 14:45






            • 1




              Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
              – Stéphane Chazelas
              Jan 31 at 15:12











            • Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
              – Kim
              Jan 31 at 15:41










            • Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
              – Kim
              Jan 31 at 15:56














            up vote
            4
            down vote



            accepted










            You could try something like this, it will remove everything before the first -



            for file in *-*.CSV
            do
            newName="$file#*-"
            mv -- "$file" "$newName"
            done





            share|improve this answer


















            • 1




              The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
              – ilkkachu
              Jan 31 at 14:42











            • Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
              – mmoisse
              Jan 31 at 14:45






            • 1




              Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
              – Stéphane Chazelas
              Jan 31 at 15:12











            • Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
              – Kim
              Jan 31 at 15:41










            • Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
              – Kim
              Jan 31 at 15:56












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            You could try something like this, it will remove everything before the first -



            for file in *-*.CSV
            do
            newName="$file#*-"
            mv -- "$file" "$newName"
            done





            share|improve this answer














            You could try something like this, it will remove everything before the first -



            for file in *-*.CSV
            do
            newName="$file#*-"
            mv -- "$file" "$newName"
            done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 31 at 14:51









            Jeff Schaller

            31.4k846105




            31.4k846105










            answered Jan 31 at 14:32









            mmoisse

            584




            584







            • 1




              The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
              – ilkkachu
              Jan 31 at 14:42











            • Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
              – mmoisse
              Jan 31 at 14:45






            • 1




              Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
              – Stéphane Chazelas
              Jan 31 at 15:12











            • Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
              – Kim
              Jan 31 at 15:41










            • Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
              – Kim
              Jan 31 at 15:56












            • 1




              The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
              – ilkkachu
              Jan 31 at 14:42











            • Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
              – mmoisse
              Jan 31 at 14:45






            • 1




              Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
              – Stéphane Chazelas
              Jan 31 at 15:12











            • Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
              – Kim
              Jan 31 at 15:41










            • Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
              – Kim
              Jan 31 at 15:56







            1




            1




            The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
            – ilkkachu
            Jan 31 at 14:42





            The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted $* there in Bash; It's had a bunch of bugs with that)
            – ilkkachu
            Jan 31 at 14:42













            Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
            – mmoisse
            Jan 31 at 14:45




            Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
            – mmoisse
            Jan 31 at 14:45




            1




            1




            Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
            – Stéphane Chazelas
            Jan 31 at 15:12





            Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's var=$*, it's the absence of quotes that cause problems.
            – Stéphane Chazelas
            Jan 31 at 15:12













            Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
            – Kim
            Jan 31 at 15:41




            Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
            – Kim
            Jan 31 at 15:41












            Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
            – Kim
            Jan 31 at 15:56




            Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
            – Kim
            Jan 31 at 15:56












            up vote
            0
            down vote













            I have tried by using combination of sed,awk and find . Tested and working fine



            find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh





            share|improve this answer
























              up vote
              0
              down vote













              I have tried by using combination of sed,awk and find . Tested and working fine



              find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                I have tried by using combination of sed,awk and find . Tested and working fine



                find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh





                share|improve this answer












                I have tried by using combination of sed,awk and find . Tested and working fine



                find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 31 at 19:18









                Praveen Kumar BS

                1,010128




                1,010128






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420972%2fhow-can-i-strip-out-a-numeric-prefix-from-multiple-filenames%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?