Batch rename files to a sequential numbering












2















I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case.



I have a bunch of files that have (what appears to be) a "timestamp-id":



abc_128390.png
abc_138493.png
abc_159084.png
...


that I'd like to exchange for a counter:



abc_001.png
abc_002.png
abc_003.png
...


My (plenty) naïve approach would be something like:



mv abc_*.png abc_{001..123}.png



Also, I could not figure out a way to make it work with a for-loop.



FWIW, unfortunately rename is not available on this particular system.



Any advice would be greatly appreciated!










share|improve this question

























  • FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

    – ilkkachu
    Jan 31 '18 at 12:36


















2















I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case.



I have a bunch of files that have (what appears to be) a "timestamp-id":



abc_128390.png
abc_138493.png
abc_159084.png
...


that I'd like to exchange for a counter:



abc_001.png
abc_002.png
abc_003.png
...


My (plenty) naïve approach would be something like:



mv abc_*.png abc_{001..123}.png



Also, I could not figure out a way to make it work with a for-loop.



FWIW, unfortunately rename is not available on this particular system.



Any advice would be greatly appreciated!










share|improve this question

























  • FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

    – ilkkachu
    Jan 31 '18 at 12:36
















2












2








2








I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case.



I have a bunch of files that have (what appears to be) a "timestamp-id":



abc_128390.png
abc_138493.png
abc_159084.png
...


that I'd like to exchange for a counter:



abc_001.png
abc_002.png
abc_003.png
...


My (plenty) naïve approach would be something like:



mv abc_*.png abc_{001..123}.png



Also, I could not figure out a way to make it work with a for-loop.



FWIW, unfortunately rename is not available on this particular system.



Any advice would be greatly appreciated!










share|improve this question
















I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case.



I have a bunch of files that have (what appears to be) a "timestamp-id":



abc_128390.png
abc_138493.png
abc_159084.png
...


that I'd like to exchange for a counter:



abc_001.png
abc_002.png
abc_003.png
...


My (plenty) naïve approach would be something like:



mv abc_*.png abc_{001..123}.png



Also, I could not figure out a way to make it work with a for-loop.



FWIW, unfortunately rename is not available on this particular system.



Any advice would be greatly appreciated!







bash shell-script shell rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 31 '18 at 12:35









ilkkachu

58.6k891165




58.6k891165










asked Jan 31 '18 at 12:12









NicApicellaNicApicella

3914




3914













  • FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

    – ilkkachu
    Jan 31 '18 at 12:36





















  • FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

    – ilkkachu
    Jan 31 '18 at 12:36



















FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

– ilkkachu
Jan 31 '18 at 12:36







FWIW: abc_*.png abc_{001..123}.png expands to the existing file names, and then the generated names in sequence, and mv has no way to determine what the distinction between them is. (Try e.g. echo abc_*.png abc_{001..123}.png)

– ilkkachu
Jan 31 '18 at 12:36












5 Answers
5






active

oldest

votes


















5














I can't think of a solution that handles incrementing the counter in a more clever way, but this should work:



i=0
for fi in abc_??????.png; do
mv "$fi" abc_$i.png
i=$((i+1))
done


It should be safe to use abc_*.png because it is expanded before the first mv is ever executed, but it can be useful to be very specific in that you only want files with a six-character timestamp at the end.






share|improve this answer



















  • 3





    Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

    – ilkkachu
    Jan 31 '18 at 12:19













  • I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

    – NicApicella
    Jan 31 '18 at 12:23



















3














With zsh:



typeset -A count
incr='++count[$1/$2]'
(zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')


Remove the -n when happy.



Example:



$ ls
a1b.png abc_128390.png abc_159084.png x12y.png
a2b.png abc_138493.png a.png x2y.png
$ typeset -A count
$ incr='++count[$1/$2]'
$ (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')
mv -- a1b.png a001b.png
mv -- a2b.png a002b.png
mv -- abc_128390.png abc_001.png
mv -- abc_138493.png abc_002.png
mv -- abc_159084.png abc_003.png
mv -- x2y.png x001y.png
mv -- x12y.png x002y.png





share|improve this answer

































    1














    With rename utility as part of Perl packages, you would do:



    rename -n 'our $i; s/_.*/sprintf("_%03d.png", $i++)/e' *.png


    Note: -n is for dry run, remove it to rename apply on files.






    share|improve this answer































      0














      Avoid overwriting existing files:



      i=1
      for fi in abc_??????.png; do
      a="abc_$(printf '%04d' "$i").png"
      if [[ -e $a ]]; then
      echo "file $a exist, not moving $fi"
      else
      mv "$fi" "$a"
      fi
      i=$((i+1))
      done





      share|improve this answer































        0














        you can use cut to cut out parts of the filename, for example, if you want to rename files like



        "1 first.jpg"
        "2 second.jpg"
        ...


        to



        "10 first.jpg"
        "20 second.jpg"
        ...


        you can use



        for i in *jpg; do
        mv -iv "$i" "$(echo "$i"|cut -d -f1)0 $(echo "$i"|cut -d -f2-99)";
        done





        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',
          autoActivateHeartbeat: false,
          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%2f420927%2fbatch-rename-files-to-a-sequential-numbering%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          I can't think of a solution that handles incrementing the counter in a more clever way, but this should work:



          i=0
          for fi in abc_??????.png; do
          mv "$fi" abc_$i.png
          i=$((i+1))
          done


          It should be safe to use abc_*.png because it is expanded before the first mv is ever executed, but it can be useful to be very specific in that you only want files with a six-character timestamp at the end.






          share|improve this answer



















          • 3





            Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

            – ilkkachu
            Jan 31 '18 at 12:19













          • I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

            – NicApicella
            Jan 31 '18 at 12:23
















          5














          I can't think of a solution that handles incrementing the counter in a more clever way, but this should work:



          i=0
          for fi in abc_??????.png; do
          mv "$fi" abc_$i.png
          i=$((i+1))
          done


          It should be safe to use abc_*.png because it is expanded before the first mv is ever executed, but it can be useful to be very specific in that you only want files with a six-character timestamp at the end.






          share|improve this answer



















          • 3





            Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

            – ilkkachu
            Jan 31 '18 at 12:19













          • I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

            – NicApicella
            Jan 31 '18 at 12:23














          5












          5








          5







          I can't think of a solution that handles incrementing the counter in a more clever way, but this should work:



          i=0
          for fi in abc_??????.png; do
          mv "$fi" abc_$i.png
          i=$((i+1))
          done


          It should be safe to use abc_*.png because it is expanded before the first mv is ever executed, but it can be useful to be very specific in that you only want files with a six-character timestamp at the end.






          share|improve this answer













          I can't think of a solution that handles incrementing the counter in a more clever way, but this should work:



          i=0
          for fi in abc_??????.png; do
          mv "$fi" abc_$i.png
          i=$((i+1))
          done


          It should be safe to use abc_*.png because it is expanded before the first mv is ever executed, but it can be useful to be very specific in that you only want files with a six-character timestamp at the end.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 31 '18 at 12:16









          Ulrich SchwarzUlrich Schwarz

          9,84312946




          9,84312946








          • 3





            Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

            – ilkkachu
            Jan 31 '18 at 12:19













          • I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

            – NicApicella
            Jan 31 '18 at 12:23














          • 3





            Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

            – ilkkachu
            Jan 31 '18 at 12:19













          • I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

            – NicApicella
            Jan 31 '18 at 12:23








          3




          3





          Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

          – ilkkachu
          Jan 31 '18 at 12:19







          Change the destination file name to "$(printf "abc_%03d.png" "$i")" to get the zero-padding, too

          – ilkkachu
          Jan 31 '18 at 12:19















          I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

          – NicApicella
          Jan 31 '18 at 12:23





          I had been very close to this solution at one point… The zero-padding is the cherry on top! Thanks1

          – NicApicella
          Jan 31 '18 at 12:23













          3














          With zsh:



          typeset -A count
          incr='++count[$1/$2]'
          (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')


          Remove the -n when happy.



          Example:



          $ ls
          a1b.png abc_128390.png abc_159084.png x12y.png
          a2b.png abc_138493.png a.png x2y.png
          $ typeset -A count
          $ incr='++count[$1/$2]'
          $ (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')
          mv -- a1b.png a001b.png
          mv -- a2b.png a002b.png
          mv -- abc_128390.png abc_001.png
          mv -- abc_138493.png abc_002.png
          mv -- abc_159084.png abc_003.png
          mv -- x2y.png x001y.png
          mv -- x12y.png x002y.png





          share|improve this answer






























            3














            With zsh:



            typeset -A count
            incr='++count[$1/$2]'
            (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')


            Remove the -n when happy.



            Example:



            $ ls
            a1b.png abc_128390.png abc_159084.png x12y.png
            a2b.png abc_138493.png a.png x2y.png
            $ typeset -A count
            $ incr='++count[$1/$2]'
            $ (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')
            mv -- a1b.png a001b.png
            mv -- a2b.png a002b.png
            mv -- abc_128390.png abc_001.png
            mv -- abc_138493.png abc_002.png
            mv -- abc_159084.png abc_003.png
            mv -- x2y.png x001y.png
            mv -- x12y.png x002y.png





            share|improve this answer




























              3












              3








              3







              With zsh:



              typeset -A count
              incr='++count[$1/$2]'
              (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')


              Remove the -n when happy.



              Example:



              $ ls
              a1b.png abc_128390.png abc_159084.png x12y.png
              a2b.png abc_138493.png a.png x2y.png
              $ typeset -A count
              $ incr='++count[$1/$2]'
              $ (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')
              mv -- a1b.png a001b.png
              mv -- a2b.png a002b.png
              mv -- abc_128390.png abc_001.png
              mv -- abc_138493.png abc_002.png
              mv -- abc_159084.png abc_003.png
              mv -- x2y.png x001y.png
              mv -- x12y.png x002y.png





              share|improve this answer















              With zsh:



              typeset -A count
              incr='++count[$1/$2]'
              (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')


              Remove the -n when happy.



              Example:



              $ ls
              a1b.png abc_128390.png abc_159084.png x12y.png
              a2b.png abc_138493.png a.png x2y.png
              $ typeset -A count
              $ incr='++count[$1/$2]'
              $ (zmv -n '([^0-9]##)<->(*)(#qn)' '$1${(l:3::0:)$((incr))}$2')
              mv -- a1b.png a001b.png
              mv -- a2b.png a002b.png
              mv -- abc_128390.png abc_001.png
              mv -- abc_138493.png abc_002.png
              mv -- abc_159084.png abc_003.png
              mv -- x2y.png x001y.png
              mv -- x12y.png x002y.png






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 1 at 10:44

























              answered Jan 31 '18 at 13:48









              Stéphane ChazelasStéphane Chazelas

              305k57574929




              305k57574929























                  1














                  With rename utility as part of Perl packages, you would do:



                  rename -n 'our $i; s/_.*/sprintf("_%03d.png", $i++)/e' *.png


                  Note: -n is for dry run, remove it to rename apply on files.






                  share|improve this answer




























                    1














                    With rename utility as part of Perl packages, you would do:



                    rename -n 'our $i; s/_.*/sprintf("_%03d.png", $i++)/e' *.png


                    Note: -n is for dry run, remove it to rename apply on files.






                    share|improve this answer


























                      1












                      1








                      1







                      With rename utility as part of Perl packages, you would do:



                      rename -n 'our $i; s/_.*/sprintf("_%03d.png", $i++)/e' *.png


                      Note: -n is for dry run, remove it to rename apply on files.






                      share|improve this answer













                      With rename utility as part of Perl packages, you would do:



                      rename -n 'our $i; s/_.*/sprintf("_%03d.png", $i++)/e' *.png


                      Note: -n is for dry run, remove it to rename apply on files.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 31 '18 at 18:20









                      αғsнιηαғsнιη

                      16.8k102865




                      16.8k102865























                          0














                          Avoid overwriting existing files:



                          i=1
                          for fi in abc_??????.png; do
                          a="abc_$(printf '%04d' "$i").png"
                          if [[ -e $a ]]; then
                          echo "file $a exist, not moving $fi"
                          else
                          mv "$fi" "$a"
                          fi
                          i=$((i+1))
                          done





                          share|improve this answer




























                            0














                            Avoid overwriting existing files:



                            i=1
                            for fi in abc_??????.png; do
                            a="abc_$(printf '%04d' "$i").png"
                            if [[ -e $a ]]; then
                            echo "file $a exist, not moving $fi"
                            else
                            mv "$fi" "$a"
                            fi
                            i=$((i+1))
                            done





                            share|improve this answer


























                              0












                              0








                              0







                              Avoid overwriting existing files:



                              i=1
                              for fi in abc_??????.png; do
                              a="abc_$(printf '%04d' "$i").png"
                              if [[ -e $a ]]; then
                              echo "file $a exist, not moving $fi"
                              else
                              mv "$fi" "$a"
                              fi
                              i=$((i+1))
                              done





                              share|improve this answer













                              Avoid overwriting existing files:



                              i=1
                              for fi in abc_??????.png; do
                              a="abc_$(printf '%04d' "$i").png"
                              if [[ -e $a ]]; then
                              echo "file $a exist, not moving $fi"
                              else
                              mv "$fi" "$a"
                              fi
                              i=$((i+1))
                              done






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 31 '18 at 17:22









                              IsaacIsaac

                              11.9k11752




                              11.9k11752























                                  0














                                  you can use cut to cut out parts of the filename, for example, if you want to rename files like



                                  "1 first.jpg"
                                  "2 second.jpg"
                                  ...


                                  to



                                  "10 first.jpg"
                                  "20 second.jpg"
                                  ...


                                  you can use



                                  for i in *jpg; do
                                  mv -iv "$i" "$(echo "$i"|cut -d -f1)0 $(echo "$i"|cut -d -f2-99)";
                                  done





                                  share|improve this answer




























                                    0














                                    you can use cut to cut out parts of the filename, for example, if you want to rename files like



                                    "1 first.jpg"
                                    "2 second.jpg"
                                    ...


                                    to



                                    "10 first.jpg"
                                    "20 second.jpg"
                                    ...


                                    you can use



                                    for i in *jpg; do
                                    mv -iv "$i" "$(echo "$i"|cut -d -f1)0 $(echo "$i"|cut -d -f2-99)";
                                    done





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      you can use cut to cut out parts of the filename, for example, if you want to rename files like



                                      "1 first.jpg"
                                      "2 second.jpg"
                                      ...


                                      to



                                      "10 first.jpg"
                                      "20 second.jpg"
                                      ...


                                      you can use



                                      for i in *jpg; do
                                      mv -iv "$i" "$(echo "$i"|cut -d -f1)0 $(echo "$i"|cut -d -f2-99)";
                                      done





                                      share|improve this answer













                                      you can use cut to cut out parts of the filename, for example, if you want to rename files like



                                      "1 first.jpg"
                                      "2 second.jpg"
                                      ...


                                      to



                                      "10 first.jpg"
                                      "20 second.jpg"
                                      ...


                                      you can use



                                      for i in *jpg; do
                                      mv -iv "$i" "$(echo "$i"|cut -d -f1)0 $(echo "$i"|cut -d -f2-99)";
                                      done






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 14 '18 at 18:43









                                      rubo77rubo77

                                      7,6622573134




                                      7,6622573134






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420927%2fbatch-rename-files-to-a-sequential-numbering%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 make a Squid Proxy server?

                                          Is this a new Fibonacci Identity?

                                          19世紀