Rename matching files based on a serial number











up vote
1
down vote

favorite












Assuming I have a bunch of files that are mac screenshots:



Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png


And I want to use mv to label them to:



Screen_0.png
Screen_1.png
Screen_2.png


The partial command I come up with:



find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;


How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.










share|improve this question




























    up vote
    1
    down vote

    favorite












    Assuming I have a bunch of files that are mac screenshots:



    Screen Shot 2018-11-09 at 12.37.37 PM.png
    Screen Shot 2018-11-10 at 4.53.02 PM.png
    Screen Shot 2018-11-10 at 9.19.19 PM.png


    And I want to use mv to label them to:



    Screen_0.png
    Screen_1.png
    Screen_2.png


    The partial command I come up with:



    find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;


    How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Assuming I have a bunch of files that are mac screenshots:



      Screen Shot 2018-11-09 at 12.37.37 PM.png
      Screen Shot 2018-11-10 at 4.53.02 PM.png
      Screen Shot 2018-11-10 at 9.19.19 PM.png


      And I want to use mv to label them to:



      Screen_0.png
      Screen_1.png
      Screen_2.png


      The partial command I come up with:



      find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;


      How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.










      share|improve this question















      Assuming I have a bunch of files that are mac screenshots:



      Screen Shot 2018-11-09 at 12.37.37 PM.png
      Screen Shot 2018-11-10 at 4.53.02 PM.png
      Screen Shot 2018-11-10 at 9.19.19 PM.png


      And I want to use mv to label them to:



      Screen_0.png
      Screen_1.png
      Screen_2.png


      The partial command I come up with:



      find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;


      How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.







      bash find rename






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 0:10









      codeforester

      17.3k83864




      17.3k83864










      asked Nov 11 at 22:23









      Rocky Li

      2,7531315




      2,7531315
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I don't think it is possible to pass a sequence number xargs in the way you want. Use a simple loop instead:



          #!/bin/bash
          for file in Screen*.png; do
          [[ -f $file ]] || continue # skip if not a regular file
          mv "$file" "Screen_$((count++)).png"
          done





          share|improve this answer























          • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
            – Rocky Li
            Nov 12 at 0:15






          • 1




            Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
            – codeforester
            Nov 12 at 0:17


















          up vote
          1
          down vote













          If you use an array, no arithmetics is needed, just use the index of each element:



          #!/bin/bash
          files=('Screen Shot'*.png)
          for i in "${!files[@]}" ; do
          mv "${files[i]}" Screen_$i.png
          done





          share|improve this answer





















          • Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
            – Rocky Li
            Nov 12 at 0:28


















          up vote
          1
          down vote













          With Perl one liner also, you could do it easily.



          > ls -1 Screen*
          Screen Shot 2018-11-09 at 12.37.37 PM.png
          Screen Shot 2018-11-10 at 4.53.02 PM.png
          Screen Shot 2018-11-10 at 9.19.19 PM.png
          > perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
          > ls -1 Screen*
          Screen_0.png
          Screen_1.png
          Screen_2.png
          >





          share|improve this answer




























            up vote
            1
            down vote













            You can just use rename, a.k.a. Perl rename:



            rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png


            Sample Output



            'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
            'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
            'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'


            If you like the look of the output, run again without --dry-run.





            If you are on macOS, you can install Perl rename with homebrew:



            brew install rename





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53253838%2frename-matching-files-based-on-a-serial-number%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              I don't think it is possible to pass a sequence number xargs in the way you want. Use a simple loop instead:



              #!/bin/bash
              for file in Screen*.png; do
              [[ -f $file ]] || continue # skip if not a regular file
              mv "$file" "Screen_$((count++)).png"
              done





              share|improve this answer























              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 at 0:15






              • 1




                Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
                – codeforester
                Nov 12 at 0:17















              up vote
              2
              down vote



              accepted










              I don't think it is possible to pass a sequence number xargs in the way you want. Use a simple loop instead:



              #!/bin/bash
              for file in Screen*.png; do
              [[ -f $file ]] || continue # skip if not a regular file
              mv "$file" "Screen_$((count++)).png"
              done





              share|improve this answer























              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 at 0:15






              • 1




                Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
                – codeforester
                Nov 12 at 0:17













              up vote
              2
              down vote



              accepted







              up vote
              2
              down vote



              accepted






              I don't think it is possible to pass a sequence number xargs in the way you want. Use a simple loop instead:



              #!/bin/bash
              for file in Screen*.png; do
              [[ -f $file ]] || continue # skip if not a regular file
              mv "$file" "Screen_$((count++)).png"
              done





              share|improve this answer














              I don't think it is possible to pass a sequence number xargs in the way you want. Use a simple loop instead:



              #!/bin/bash
              for file in Screen*.png; do
              [[ -f $file ]] || continue # skip if not a regular file
              mv "$file" "Screen_$((count++)).png"
              done






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 11 at 22:42

























              answered Nov 11 at 22:36









              codeforester

              17.3k83864




              17.3k83864












              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 at 0:15






              • 1




                Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
                – codeforester
                Nov 12 at 0:17


















              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 at 0:15






              • 1




                Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
                – codeforester
                Nov 12 at 0:17
















              Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
              – Rocky Li
              Nov 12 at 0:15




              Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
              – Rocky Li
              Nov 12 at 0:15




              1




              1




              Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
              – codeforester
              Nov 12 at 0:17




              Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use $((++count)).
              – codeforester
              Nov 12 at 0:17












              up vote
              1
              down vote













              If you use an array, no arithmetics is needed, just use the index of each element:



              #!/bin/bash
              files=('Screen Shot'*.png)
              for i in "${!files[@]}" ; do
              mv "${files[i]}" Screen_$i.png
              done





              share|improve this answer





















              • Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
                – Rocky Li
                Nov 12 at 0:28















              up vote
              1
              down vote













              If you use an array, no arithmetics is needed, just use the index of each element:



              #!/bin/bash
              files=('Screen Shot'*.png)
              for i in "${!files[@]}" ; do
              mv "${files[i]}" Screen_$i.png
              done





              share|improve this answer





















              • Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
                – Rocky Li
                Nov 12 at 0:28













              up vote
              1
              down vote










              up vote
              1
              down vote









              If you use an array, no arithmetics is needed, just use the index of each element:



              #!/bin/bash
              files=('Screen Shot'*.png)
              for i in "${!files[@]}" ; do
              mv "${files[i]}" Screen_$i.png
              done





              share|improve this answer












              If you use an array, no arithmetics is needed, just use the index of each element:



              #!/bin/bash
              files=('Screen Shot'*.png)
              for i in "${!files[@]}" ; do
              mv "${files[i]}" Screen_$i.png
              done






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 11 at 22:47









              choroba

              153k14139201




              153k14139201












              • Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
                – Rocky Li
                Nov 12 at 0:28


















              • Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
                – Rocky Li
                Nov 12 at 0:28
















              Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
              – Rocky Li
              Nov 12 at 0:28




              Thanks, I was able to condense your code into a single line: files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done that works in the terminal, much thanks!
              – Rocky Li
              Nov 12 at 0:28










              up vote
              1
              down vote













              With Perl one liner also, you could do it easily.



              > ls -1 Screen*
              Screen Shot 2018-11-09 at 12.37.37 PM.png
              Screen Shot 2018-11-10 at 4.53.02 PM.png
              Screen Shot 2018-11-10 at 9.19.19 PM.png
              > perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
              > ls -1 Screen*
              Screen_0.png
              Screen_1.png
              Screen_2.png
              >





              share|improve this answer

























                up vote
                1
                down vote













                With Perl one liner also, you could do it easily.



                > ls -1 Screen*
                Screen Shot 2018-11-09 at 12.37.37 PM.png
                Screen Shot 2018-11-10 at 4.53.02 PM.png
                Screen Shot 2018-11-10 at 9.19.19 PM.png
                > perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
                > ls -1 Screen*
                Screen_0.png
                Screen_1.png
                Screen_2.png
                >





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  With Perl one liner also, you could do it easily.



                  > ls -1 Screen*
                  Screen Shot 2018-11-09 at 12.37.37 PM.png
                  Screen Shot 2018-11-10 at 4.53.02 PM.png
                  Screen Shot 2018-11-10 at 9.19.19 PM.png
                  > perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
                  > ls -1 Screen*
                  Screen_0.png
                  Screen_1.png
                  Screen_2.png
                  >





                  share|improve this answer












                  With Perl one liner also, you could do it easily.



                  > ls -1 Screen*
                  Screen Shot 2018-11-09 at 12.37.37 PM.png
                  Screen Shot 2018-11-10 at 4.53.02 PM.png
                  Screen Shot 2018-11-10 at 9.19.19 PM.png
                  > perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
                  > ls -1 Screen*
                  Screen_0.png
                  Screen_1.png
                  Screen_2.png
                  >






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 10:33









                  stack0114106

                  1,7221416




                  1,7221416






















                      up vote
                      1
                      down vote













                      You can just use rename, a.k.a. Perl rename:



                      rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png


                      Sample Output



                      'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
                      'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
                      'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'


                      If you like the look of the output, run again without --dry-run.





                      If you are on macOS, you can install Perl rename with homebrew:



                      brew install rename





                      share|improve this answer



























                        up vote
                        1
                        down vote













                        You can just use rename, a.k.a. Perl rename:



                        rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png


                        Sample Output



                        'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
                        'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
                        'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'


                        If you like the look of the output, run again without --dry-run.





                        If you are on macOS, you can install Perl rename with homebrew:



                        brew install rename





                        share|improve this answer

























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can just use rename, a.k.a. Perl rename:



                          rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png


                          Sample Output



                          'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
                          'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
                          'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'


                          If you like the look of the output, run again without --dry-run.





                          If you are on macOS, you can install Perl rename with homebrew:



                          brew install rename





                          share|improve this answer














                          You can just use rename, a.k.a. Perl rename:



                          rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png


                          Sample Output



                          'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
                          'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
                          'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'


                          If you like the look of the output, run again without --dry-run.





                          If you are on macOS, you can install Perl rename with homebrew:



                          brew install rename






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 12 at 12:08

























                          answered Nov 12 at 12:00









                          Mark Setchell

                          84.9k672171




                          84.9k672171






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fstackoverflow.com%2fquestions%2f53253838%2frename-matching-files-based-on-a-serial-number%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

                              Bressuire

                              Vorschmack

                              Quarantine