bash - remove first 3 characters and last 1 character from all mp3 files in all subdirectory

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











up vote
0
down vote

favorite












in base directory i have folders like this:



1
2
3
4
5
10
110


so in each of them i have files like



 0010011.mp3 0010031.mp3 0010051.mp3 0010071.mp3 0010021.mp3 0010041.mp3 0010061.mp3


so i want to remove first 3 characters and last 1 character so files will look like this



 001.mp3 003.mp3 005.mp3 007.mp3 002.mp3 004.mp3 006.mp3


i tried this



for file in ??????*; do echo mv $file `echo $file | cut -c4-`; done


also this is not working in subdirectories it is just if files are in base directory







share|improve this question











migrated from serverfault.com May 3 at 5:54


This question came from our site for system and network administrators.


















    up vote
    0
    down vote

    favorite












    in base directory i have folders like this:



    1
    2
    3
    4
    5
    10
    110


    so in each of them i have files like



     0010011.mp3 0010031.mp3 0010051.mp3 0010071.mp3 0010021.mp3 0010041.mp3 0010061.mp3


    so i want to remove first 3 characters and last 1 character so files will look like this



     001.mp3 003.mp3 005.mp3 007.mp3 002.mp3 004.mp3 006.mp3


    i tried this



    for file in ??????*; do echo mv $file `echo $file | cut -c4-`; done


    also this is not working in subdirectories it is just if files are in base directory







    share|improve this question











    migrated from serverfault.com May 3 at 5:54


    This question came from our site for system and network administrators.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      in base directory i have folders like this:



      1
      2
      3
      4
      5
      10
      110


      so in each of them i have files like



       0010011.mp3 0010031.mp3 0010051.mp3 0010071.mp3 0010021.mp3 0010041.mp3 0010061.mp3


      so i want to remove first 3 characters and last 1 character so files will look like this



       001.mp3 003.mp3 005.mp3 007.mp3 002.mp3 004.mp3 006.mp3


      i tried this



      for file in ??????*; do echo mv $file `echo $file | cut -c4-`; done


      also this is not working in subdirectories it is just if files are in base directory







      share|improve this question











      in base directory i have folders like this:



      1
      2
      3
      4
      5
      10
      110


      so in each of them i have files like



       0010011.mp3 0010031.mp3 0010051.mp3 0010071.mp3 0010021.mp3 0010041.mp3 0010061.mp3


      so i want to remove first 3 characters and last 1 character so files will look like this



       001.mp3 003.mp3 005.mp3 007.mp3 002.mp3 004.mp3 006.mp3


      i tried this



      for file in ??????*; do echo mv $file `echo $file | cut -c4-`; done


      also this is not working in subdirectories it is just if files are in base directory









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 27 at 15:06









      arlind

      82




      82




      migrated from serverfault.com May 3 at 5:54


      This question came from our site for system and network administrators.






      migrated from serverfault.com May 3 at 5:54


      This question came from our site for system and network administrators.






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote














          I would not advise just collecting all files that match *.mp3
          and then trimming the file names. You MAY want to run this same
          script AGAIN later (like next week) and it should NOT try to
          rename the same files a second time, making them even shorter
          and have naming conflicts.



          Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3




           KEEP_DIR=$PWD 
          cd /your/music/base_dir # sub-dirs holding .mp3

          RCOUNT=0 ; SKIP=0 ; FCC=0


          for FFF in */001???1.mp3
          do
          LOC="$(dirname $FFF)"
          BAS="$(basename $FFF)"
          TRIM1="$BAS#1.mp3" #could use $BAS:4:3
          NEWF="$LOC/$TRIM1%001.mp3"
          if [ -e "$NEWF" ] ; then
          echo "#-- File $NEWF already exists, not renaming $FFF"
          SKIP=$((SKIP+1))
          else
          mv $FFF $NEWF
          MCOUNT=$((MCOUNT+1))
          fi
          FCC=$((FCC+1))
          done

          echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
          cd $KEEP_DIR

          #--[eof]





          share|improve this answer





















          • Don't forget to double quote "$variables" when you use them
            – roaima
            May 3 at 6:53


















          up vote
          0
          down vote













          If you're got rename (sometimes known as prename) you can do this in one command:



          rename -n 's!(.*/)...(.*).(.mp3)!$1$2$3!' */?????*.mp3


          Alternatively you can loop through the files:



          for f in */?????*.mp3
          do
          echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"
          done


          In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.






          share|improve this answer























          • output of second command is mv -v *.mp3 p3.mp3
            – arlind
            Apr 27 at 17:10










          • i need to run command in parent directory and it should work for all subdirectories
            – arlind
            Apr 27 at 17:25










          • @arpa answer updated to run from the root of your folders.
            – roaima
            Apr 27 at 17:58










          • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
            – arlind
            Apr 27 at 20:32

















          up vote
          0
          down vote













          You may use sed to create one mv command line for each mp3 with a 7 characters long basename:



          $ find . -name "???????.mp3" | sed 's/(.*)(...)(...)(.)(.mp3)/mv 12345 135/'


          After reviewing the output you can redirect it into a file and execute.






          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%2f441458%2fbash-remove-first-3-characters-and-last-1-character-from-all-mp3-files-in-all%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote














            I would not advise just collecting all files that match *.mp3
            and then trimming the file names. You MAY want to run this same
            script AGAIN later (like next week) and it should NOT try to
            rename the same files a second time, making them even shorter
            and have naming conflicts.



            Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3




             KEEP_DIR=$PWD 
            cd /your/music/base_dir # sub-dirs holding .mp3

            RCOUNT=0 ; SKIP=0 ; FCC=0


            for FFF in */001???1.mp3
            do
            LOC="$(dirname $FFF)"
            BAS="$(basename $FFF)"
            TRIM1="$BAS#1.mp3" #could use $BAS:4:3
            NEWF="$LOC/$TRIM1%001.mp3"
            if [ -e "$NEWF" ] ; then
            echo "#-- File $NEWF already exists, not renaming $FFF"
            SKIP=$((SKIP+1))
            else
            mv $FFF $NEWF
            MCOUNT=$((MCOUNT+1))
            fi
            FCC=$((FCC+1))
            done

            echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
            cd $KEEP_DIR

            #--[eof]





            share|improve this answer





















            • Don't forget to double quote "$variables" when you use them
              – roaima
              May 3 at 6:53















            up vote
            0
            down vote














            I would not advise just collecting all files that match *.mp3
            and then trimming the file names. You MAY want to run this same
            script AGAIN later (like next week) and it should NOT try to
            rename the same files a second time, making them even shorter
            and have naming conflicts.



            Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3




             KEEP_DIR=$PWD 
            cd /your/music/base_dir # sub-dirs holding .mp3

            RCOUNT=0 ; SKIP=0 ; FCC=0


            for FFF in */001???1.mp3
            do
            LOC="$(dirname $FFF)"
            BAS="$(basename $FFF)"
            TRIM1="$BAS#1.mp3" #could use $BAS:4:3
            NEWF="$LOC/$TRIM1%001.mp3"
            if [ -e "$NEWF" ] ; then
            echo "#-- File $NEWF already exists, not renaming $FFF"
            SKIP=$((SKIP+1))
            else
            mv $FFF $NEWF
            MCOUNT=$((MCOUNT+1))
            fi
            FCC=$((FCC+1))
            done

            echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
            cd $KEEP_DIR

            #--[eof]





            share|improve this answer





















            • Don't forget to double quote "$variables" when you use them
              – roaima
              May 3 at 6:53













            up vote
            0
            down vote










            up vote
            0
            down vote










            I would not advise just collecting all files that match *.mp3
            and then trimming the file names. You MAY want to run this same
            script AGAIN later (like next week) and it should NOT try to
            rename the same files a second time, making them even shorter
            and have naming conflicts.



            Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3




             KEEP_DIR=$PWD 
            cd /your/music/base_dir # sub-dirs holding .mp3

            RCOUNT=0 ; SKIP=0 ; FCC=0


            for FFF in */001???1.mp3
            do
            LOC="$(dirname $FFF)"
            BAS="$(basename $FFF)"
            TRIM1="$BAS#1.mp3" #could use $BAS:4:3
            NEWF="$LOC/$TRIM1%001.mp3"
            if [ -e "$NEWF" ] ; then
            echo "#-- File $NEWF already exists, not renaming $FFF"
            SKIP=$((SKIP+1))
            else
            mv $FFF $NEWF
            MCOUNT=$((MCOUNT+1))
            fi
            FCC=$((FCC+1))
            done

            echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
            cd $KEEP_DIR

            #--[eof]





            share|improve this answer














            I would not advise just collecting all files that match *.mp3
            and then trimming the file names. You MAY want to run this same
            script AGAIN later (like next week) and it should NOT try to
            rename the same files a second time, making them even shorter
            and have naming conflicts.



            Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3




             KEEP_DIR=$PWD 
            cd /your/music/base_dir # sub-dirs holding .mp3

            RCOUNT=0 ; SKIP=0 ; FCC=0


            for FFF in */001???1.mp3
            do
            LOC="$(dirname $FFF)"
            BAS="$(basename $FFF)"
            TRIM1="$BAS#1.mp3" #could use $BAS:4:3
            NEWF="$LOC/$TRIM1%001.mp3"
            if [ -e "$NEWF" ] ; then
            echo "#-- File $NEWF already exists, not renaming $FFF"
            SKIP=$((SKIP+1))
            else
            mv $FFF $NEWF
            MCOUNT=$((MCOUNT+1))
            fi
            FCC=$((FCC+1))
            done

            echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
            cd $KEEP_DIR

            #--[eof]






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered May 3 at 2:25







            Joseph in Atlanta


















            • Don't forget to double quote "$variables" when you use them
              – roaima
              May 3 at 6:53

















            • Don't forget to double quote "$variables" when you use them
              – roaima
              May 3 at 6:53
















            Don't forget to double quote "$variables" when you use them
            – roaima
            May 3 at 6:53





            Don't forget to double quote "$variables" when you use them
            – roaima
            May 3 at 6:53













            up vote
            0
            down vote













            If you're got rename (sometimes known as prename) you can do this in one command:



            rename -n 's!(.*/)...(.*).(.mp3)!$1$2$3!' */?????*.mp3


            Alternatively you can loop through the files:



            for f in */?????*.mp3
            do
            echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"
            done


            In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.






            share|improve this answer























            • output of second command is mv -v *.mp3 p3.mp3
              – arlind
              Apr 27 at 17:10










            • i need to run command in parent directory and it should work for all subdirectories
              – arlind
              Apr 27 at 17:25










            • @arpa answer updated to run from the root of your folders.
              – roaima
              Apr 27 at 17:58










            • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
              – arlind
              Apr 27 at 20:32














            up vote
            0
            down vote













            If you're got rename (sometimes known as prename) you can do this in one command:



            rename -n 's!(.*/)...(.*).(.mp3)!$1$2$3!' */?????*.mp3


            Alternatively you can loop through the files:



            for f in */?????*.mp3
            do
            echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"
            done


            In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.






            share|improve this answer























            • output of second command is mv -v *.mp3 p3.mp3
              – arlind
              Apr 27 at 17:10










            • i need to run command in parent directory and it should work for all subdirectories
              – arlind
              Apr 27 at 17:25










            • @arpa answer updated to run from the root of your folders.
              – roaima
              Apr 27 at 17:58










            • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
              – arlind
              Apr 27 at 20:32












            up vote
            0
            down vote










            up vote
            0
            down vote









            If you're got rename (sometimes known as prename) you can do this in one command:



            rename -n 's!(.*/)...(.*).(.mp3)!$1$2$3!' */?????*.mp3


            Alternatively you can loop through the files:



            for f in */?????*.mp3
            do
            echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"
            done


            In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.






            share|improve this answer















            If you're got rename (sometimes known as prename) you can do this in one command:



            rename -n 's!(.*/)...(.*).(.mp3)!$1$2$3!' */?????*.mp3


            Alternatively you can loop through the files:



            for f in */?????*.mp3
            do
            echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"
            done


            In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited May 3 at 6:57


























            answered Apr 27 at 16:01









            roaima

            39.4k545106




            39.4k545106











            • output of second command is mv -v *.mp3 p3.mp3
              – arlind
              Apr 27 at 17:10










            • i need to run command in parent directory and it should work for all subdirectories
              – arlind
              Apr 27 at 17:25










            • @arpa answer updated to run from the root of your folders.
              – roaima
              Apr 27 at 17:58










            • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
              – arlind
              Apr 27 at 20:32
















            • output of second command is mv -v *.mp3 p3.mp3
              – arlind
              Apr 27 at 17:10










            • i need to run command in parent directory and it should work for all subdirectories
              – arlind
              Apr 27 at 17:25










            • @arpa answer updated to run from the root of your folders.
              – roaima
              Apr 27 at 17:58










            • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
              – arlind
              Apr 27 at 20:32















            output of second command is mv -v *.mp3 p3.mp3
            – arlind
            Apr 27 at 17:10




            output of second command is mv -v *.mp3 p3.mp3
            – arlind
            Apr 27 at 17:10












            i need to run command in parent directory and it should work for all subdirectories
            – arlind
            Apr 27 at 17:25




            i need to run command in parent directory and it should work for all subdirectories
            – arlind
            Apr 27 at 17:25












            @arpa answer updated to run from the root of your folders.
            – roaima
            Apr 27 at 17:58




            @arpa answer updated to run from the root of your folders.
            – roaima
            Apr 27 at 17:58












            for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
            – arlind
            Apr 27 at 20:32




            for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
            – arlind
            Apr 27 at 20:32










            up vote
            0
            down vote













            You may use sed to create one mv command line for each mp3 with a 7 characters long basename:



            $ find . -name "???????.mp3" | sed 's/(.*)(...)(...)(.)(.mp3)/mv 12345 135/'


            After reviewing the output you can redirect it into a file and execute.






            share|improve this answer

























              up vote
              0
              down vote













              You may use sed to create one mv command line for each mp3 with a 7 characters long basename:



              $ find . -name "???????.mp3" | sed 's/(.*)(...)(...)(.)(.mp3)/mv 12345 135/'


              After reviewing the output you can redirect it into a file and execute.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You may use sed to create one mv command line for each mp3 with a 7 characters long basename:



                $ find . -name "???????.mp3" | sed 's/(.*)(...)(...)(.)(.mp3)/mv 12345 135/'


                After reviewing the output you can redirect it into a file and execute.






                share|improve this answer













                You may use sed to create one mv command line for each mp3 with a 7 characters long basename:



                $ find . -name "???????.mp3" | sed 's/(.*)(...)(...)(.)(.mp3)/mv 12345 135/'


                After reviewing the output you can redirect it into a file and execute.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 3 at 7:56









                rudimeier

                5,1111331




                5,1111331






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441458%2fbash-remove-first-3-characters-and-last-1-character-from-all-mp3-files-in-all%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?