Change all filenames in directory to numerals

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 files in a directory where filenames are like



01 ABC DEF.m4a
02 DEF ABC.m4a
etc...


I want to convert these to



1.m4a
2.m4a
etc...


How can I do this using the command line?










share|improve this question









New contributor




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























    up vote
    2
    down vote

    favorite












    I have files in a directory where filenames are like



    01 ABC DEF.m4a
    02 DEF ABC.m4a
    etc...


    I want to convert these to



    1.m4a
    2.m4a
    etc...


    How can I do this using the command line?










    share|improve this question









    New contributor




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





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have files in a directory where filenames are like



      01 ABC DEF.m4a
      02 DEF ABC.m4a
      etc...


      I want to convert these to



      1.m4a
      2.m4a
      etc...


      How can I do this using the command line?










      share|improve this question









      New contributor




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











      I have files in a directory where filenames are like



      01 ABC DEF.m4a
      02 DEF ABC.m4a
      etc...


      I want to convert these to



      1.m4a
      2.m4a
      etc...


      How can I do this using the command line?







      files






      share|improve this question









      New contributor




      Perseus14 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




      Perseus14 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 Oct 4 at 6:57









      roaima

      40.9k547111




      40.9k547111






      New contributor




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









      asked Oct 4 at 6:53









      Perseus14

      132




      132




      New contributor




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





      New contributor





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






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




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          If you have the perl version of rename (sometimes called prename) you can use this



          rename -n 's/^0*(d+).*(.m4a)z/$1$2/s' [0-9]*.m4a


          When you're happy it's going to do what you want, remove the -n flag.



          This uses a Regular Expression match:




          • ^ - require start-of-subject


          • 0* - match zero or more "0"


          • (d+) - match and remember one or more decimal digits


          • .* - match everything until...


          • (.m4a) - match and remember literal ".m4a"


          • z - require end-of-subject¹


          • s flag - make sure . matches any byte (including newline, also valid in file names)

          and then uses $1 and $2 to reference the value of the bracketed expressions.




          ¹ Not strictly necessary here as the file names given by the shell as argument all end in .m4a and the previous .* is greedy. $ would also work here, but in rename which works on file names (which can be any sequence of non-0 bytes including newline), z is preferable as $ matches at the end of the subject like z but also before an eventual last newline character.






          share|improve this answer





























            up vote
            1
            down vote













            How for would



            for FN in *.m4a; do mv -i "$FN" "$FN%% *.m4a"; done


            get you?






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



              );






              Perseus14 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%2f473150%2fchange-all-filenames-in-directory-to-numerals%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
              2
              down vote



              accepted










              If you have the perl version of rename (sometimes called prename) you can use this



              rename -n 's/^0*(d+).*(.m4a)z/$1$2/s' [0-9]*.m4a


              When you're happy it's going to do what you want, remove the -n flag.



              This uses a Regular Expression match:




              • ^ - require start-of-subject


              • 0* - match zero or more "0"


              • (d+) - match and remember one or more decimal digits


              • .* - match everything until...


              • (.m4a) - match and remember literal ".m4a"


              • z - require end-of-subject¹


              • s flag - make sure . matches any byte (including newline, also valid in file names)

              and then uses $1 and $2 to reference the value of the bracketed expressions.




              ¹ Not strictly necessary here as the file names given by the shell as argument all end in .m4a and the previous .* is greedy. $ would also work here, but in rename which works on file names (which can be any sequence of non-0 bytes including newline), z is preferable as $ matches at the end of the subject like z but also before an eventual last newline character.






              share|improve this answer


























                up vote
                2
                down vote



                accepted










                If you have the perl version of rename (sometimes called prename) you can use this



                rename -n 's/^0*(d+).*(.m4a)z/$1$2/s' [0-9]*.m4a


                When you're happy it's going to do what you want, remove the -n flag.



                This uses a Regular Expression match:




                • ^ - require start-of-subject


                • 0* - match zero or more "0"


                • (d+) - match and remember one or more decimal digits


                • .* - match everything until...


                • (.m4a) - match and remember literal ".m4a"


                • z - require end-of-subject¹


                • s flag - make sure . matches any byte (including newline, also valid in file names)

                and then uses $1 and $2 to reference the value of the bracketed expressions.




                ¹ Not strictly necessary here as the file names given by the shell as argument all end in .m4a and the previous .* is greedy. $ would also work here, but in rename which works on file names (which can be any sequence of non-0 bytes including newline), z is preferable as $ matches at the end of the subject like z but also before an eventual last newline character.






                share|improve this answer
























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  If you have the perl version of rename (sometimes called prename) you can use this



                  rename -n 's/^0*(d+).*(.m4a)z/$1$2/s' [0-9]*.m4a


                  When you're happy it's going to do what you want, remove the -n flag.



                  This uses a Regular Expression match:




                  • ^ - require start-of-subject


                  • 0* - match zero or more "0"


                  • (d+) - match and remember one or more decimal digits


                  • .* - match everything until...


                  • (.m4a) - match and remember literal ".m4a"


                  • z - require end-of-subject¹


                  • s flag - make sure . matches any byte (including newline, also valid in file names)

                  and then uses $1 and $2 to reference the value of the bracketed expressions.




                  ¹ Not strictly necessary here as the file names given by the shell as argument all end in .m4a and the previous .* is greedy. $ would also work here, but in rename which works on file names (which can be any sequence of non-0 bytes including newline), z is preferable as $ matches at the end of the subject like z but also before an eventual last newline character.






                  share|improve this answer














                  If you have the perl version of rename (sometimes called prename) you can use this



                  rename -n 's/^0*(d+).*(.m4a)z/$1$2/s' [0-9]*.m4a


                  When you're happy it's going to do what you want, remove the -n flag.



                  This uses a Regular Expression match:




                  • ^ - require start-of-subject


                  • 0* - match zero or more "0"


                  • (d+) - match and remember one or more decimal digits


                  • .* - match everything until...


                  • (.m4a) - match and remember literal ".m4a"


                  • z - require end-of-subject¹


                  • s flag - make sure . matches any byte (including newline, also valid in file names)

                  and then uses $1 and $2 to reference the value of the bracketed expressions.




                  ¹ Not strictly necessary here as the file names given by the shell as argument all end in .m4a and the previous .* is greedy. $ would also work here, but in rename which works on file names (which can be any sequence of non-0 bytes including newline), z is preferable as $ matches at the end of the subject like z but also before an eventual last newline character.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 4 at 13:50









                  Stéphane Chazelas

                  287k53531868




                  287k53531868










                  answered Oct 4 at 7:00









                  roaima

                  40.9k547111




                  40.9k547111






















                      up vote
                      1
                      down vote













                      How for would



                      for FN in *.m4a; do mv -i "$FN" "$FN%% *.m4a"; done


                      get you?






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        How for would



                        for FN in *.m4a; do mv -i "$FN" "$FN%% *.m4a"; done


                        get you?






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          How for would



                          for FN in *.m4a; do mv -i "$FN" "$FN%% *.m4a"; done


                          get you?






                          share|improve this answer












                          How for would



                          for FN in *.m4a; do mv -i "$FN" "$FN%% *.m4a"; done


                          get you?







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 4 at 11:10









                          RudiC

                          1,84219




                          1,84219




















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









                               

                              draft saved


                              draft discarded


















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












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











                              Perseus14 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%2f473150%2fchange-all-filenames-in-directory-to-numerals%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