Rename matching files based on a serial number










1














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




























    1














    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


























      1












      1








      1







      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 '18 at 0:10









      codeforester

      17.4k83864




      17.4k83864










      asked Nov 11 '18 at 22:23









      Rocky Li

      2,8201316




      2,8201316






















          4 Answers
          4






          active

          oldest

          votes


















          2














          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 '18 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 '18 at 0:17


















          1














          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 '18 at 0:28


















          1














          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




























            1














            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',
              autoActivateHeartbeat: false,
              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









              2














              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 '18 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 '18 at 0:17















              2














              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 '18 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 '18 at 0:17













              2












              2








              2






              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 '18 at 22:42

























              answered Nov 11 '18 at 22:36









              codeforester

              17.4k83864




              17.4k83864











              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 '18 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 '18 at 0:17
















              • Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
                – Rocky Li
                Nov 12 '18 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 '18 at 0:17















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




              Does $((count++)) initiate the variable count as well? Looks like it. I never knew that's possible.
              – Rocky Li
              Nov 12 '18 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 '18 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 '18 at 0:17













              1














              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 '18 at 0:28















              1














              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 '18 at 0:28













              1












              1








              1






              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 '18 at 22:47









              choroba

              154k14140202




              154k14140202











              • 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 '18 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 '18 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 '18 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 '18 at 0:28











              1














              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

























                1














                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























                  1












                  1








                  1






                  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 '18 at 10:33









                  stack0114106

                  2,1811417




                  2,1811417





















                      1














                      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



























                        1














                        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

























                          1












                          1








                          1






                          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 '18 at 12:08

























                          answered Nov 12 '18 at 12:00









                          Mark Setchell

                          86.1k673172




                          86.1k673172



























                              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

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo