Copy files and replace token in the filename?

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











up vote
2
down vote

favorite












I've been doing Project Euler for a long time. I have a project structure that makes it really easy to create a new folder for the next question and I have a couple of boilerplate golang files to get started with.



ProjectEuler/001/001.go
ProjectEuler/001/001_test.go
ProjectEuler/002/002.go
ProjectEuler/002/002_test.go
...
ProjectEuler/new_question_stub/xxx.go
ProjectEuler/new_question_stub/xxx_test.go


When I create a new question's folder, I run these commands:



ProjectEuler $ mkdir 003
ProjectEuler $ cd 003
ProjectEuler/003 $ cp ../new_question_stub/xxx.go 003.go
ProjectEuler/003 $ cp ../new_question_stub/xxx_test.go 003_test.go


I feel there should be some way to say "copy every file out of a specific directory to the current directory, but replace token xxx with 003." It doesn't have to be one command to copy and rename in one stroke. Copying them all is easy enough but how would I rename them all after they're copied?



Edit: my use case is a pretty trivial example of only two files but I'm looking for a way to do it for an arbitrary number of files.







share|improve this question


























    up vote
    2
    down vote

    favorite












    I've been doing Project Euler for a long time. I have a project structure that makes it really easy to create a new folder for the next question and I have a couple of boilerplate golang files to get started with.



    ProjectEuler/001/001.go
    ProjectEuler/001/001_test.go
    ProjectEuler/002/002.go
    ProjectEuler/002/002_test.go
    ...
    ProjectEuler/new_question_stub/xxx.go
    ProjectEuler/new_question_stub/xxx_test.go


    When I create a new question's folder, I run these commands:



    ProjectEuler $ mkdir 003
    ProjectEuler $ cd 003
    ProjectEuler/003 $ cp ../new_question_stub/xxx.go 003.go
    ProjectEuler/003 $ cp ../new_question_stub/xxx_test.go 003_test.go


    I feel there should be some way to say "copy every file out of a specific directory to the current directory, but replace token xxx with 003." It doesn't have to be one command to copy and rename in one stroke. Copying them all is easy enough but how would I rename them all after they're copied?



    Edit: my use case is a pretty trivial example of only two files but I'm looking for a way to do it for an arbitrary number of files.







    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I've been doing Project Euler for a long time. I have a project structure that makes it really easy to create a new folder for the next question and I have a couple of boilerplate golang files to get started with.



      ProjectEuler/001/001.go
      ProjectEuler/001/001_test.go
      ProjectEuler/002/002.go
      ProjectEuler/002/002_test.go
      ...
      ProjectEuler/new_question_stub/xxx.go
      ProjectEuler/new_question_stub/xxx_test.go


      When I create a new question's folder, I run these commands:



      ProjectEuler $ mkdir 003
      ProjectEuler $ cd 003
      ProjectEuler/003 $ cp ../new_question_stub/xxx.go 003.go
      ProjectEuler/003 $ cp ../new_question_stub/xxx_test.go 003_test.go


      I feel there should be some way to say "copy every file out of a specific directory to the current directory, but replace token xxx with 003." It doesn't have to be one command to copy and rename in one stroke. Copying them all is easy enough but how would I rename them all after they're copied?



      Edit: my use case is a pretty trivial example of only two files but I'm looking for a way to do it for an arbitrary number of files.







      share|improve this question














      I've been doing Project Euler for a long time. I have a project structure that makes it really easy to create a new folder for the next question and I have a couple of boilerplate golang files to get started with.



      ProjectEuler/001/001.go
      ProjectEuler/001/001_test.go
      ProjectEuler/002/002.go
      ProjectEuler/002/002_test.go
      ...
      ProjectEuler/new_question_stub/xxx.go
      ProjectEuler/new_question_stub/xxx_test.go


      When I create a new question's folder, I run these commands:



      ProjectEuler $ mkdir 003
      ProjectEuler $ cd 003
      ProjectEuler/003 $ cp ../new_question_stub/xxx.go 003.go
      ProjectEuler/003 $ cp ../new_question_stub/xxx_test.go 003_test.go


      I feel there should be some way to say "copy every file out of a specific directory to the current directory, but replace token xxx with 003." It doesn't have to be one command to copy and rename in one stroke. Copying them all is easy enough but how would I rename them all after they're copied?



      Edit: my use case is a pretty trivial example of only two files but I'm looking for a way to do it for an arbitrary number of files.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 at 15:15

























      asked Apr 3 at 15:06









      Corey Ogburn

      152110




      152110




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Okay with using a for loop? Like



          for file in ../new_question_stub/*; do cp "$file" "003$file#*xxx"; done


          Maybe you want to define a function for this






          share|improve this answer



























            up vote
            -1
            down vote













            You can first copy all xxx_* files and then use perl rename to rename them:



            rename "s,^xxx,"$(basename $(pwd))"," *





            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%2f435300%2fcopy-files-and-replace-token-in-the-filename%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Okay with using a for loop? Like



              for file in ../new_question_stub/*; do cp "$file" "003$file#*xxx"; done


              Maybe you want to define a function for this






              share|improve this answer
























                up vote
                0
                down vote













                Okay with using a for loop? Like



                for file in ../new_question_stub/*; do cp "$file" "003$file#*xxx"; done


                Maybe you want to define a function for this






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Okay with using a for loop? Like



                  for file in ../new_question_stub/*; do cp "$file" "003$file#*xxx"; done


                  Maybe you want to define a function for this






                  share|improve this answer












                  Okay with using a for loop? Like



                  for file in ../new_question_stub/*; do cp "$file" "003$file#*xxx"; done


                  Maybe you want to define a function for this







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 3 at 15:23









                  Philippos

                  5,90211545




                  5,90211545






















                      up vote
                      -1
                      down vote













                      You can first copy all xxx_* files and then use perl rename to rename them:



                      rename "s,^xxx,"$(basename $(pwd))"," *





                      share|improve this answer


























                        up vote
                        -1
                        down vote













                        You can first copy all xxx_* files and then use perl rename to rename them:



                        rename "s,^xxx,"$(basename $(pwd))"," *





                        share|improve this answer
























                          up vote
                          -1
                          down vote










                          up vote
                          -1
                          down vote









                          You can first copy all xxx_* files and then use perl rename to rename them:



                          rename "s,^xxx,"$(basename $(pwd))"," *





                          share|improve this answer














                          You can first copy all xxx_* files and then use perl rename to rename them:



                          rename "s,^xxx,"$(basename $(pwd))"," *






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 3 at 15:22

























                          answered Apr 3 at 15:11









                          Arkadiusz Drabczyk

                          7,18521532




                          7,18521532






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435300%2fcopy-files-and-replace-token-in-the-filename%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)