Copy files with match prefix AND suffix with shell script












0















I have a directory with files such as



aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png


Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.










share|improve this question









New contributor




user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I have a directory with files such as



    aaaXXXbbb.png
    aaaYYYccc.png
    xxxAAAyyy.png
    yyyAAAxxx.png


    Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.










    share|improve this question









    New contributor




    user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I have a directory with files such as



      aaaXXXbbb.png
      aaaYYYccc.png
      xxxAAAyyy.png
      yyyAAAxxx.png


      Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.










      share|improve this question









      New contributor




      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have a directory with files such as



      aaaXXXbbb.png
      aaaYYYccc.png
      xxxAAAyyy.png
      yyyAAAxxx.png


      Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.







      shell cp






      share|improve this question









      New contributor




      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      Rui F Ribeiro

      39.6k1479132




      39.6k1479132






      New contributor




      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      user2842390user2842390

      33




      33




      New contributor




      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user2842390 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          0














          You can do this with a one liner:



          grep '^aaa.*.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/


          grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'



          If you are in the directory of the files you can just cp them with:



          cp aaa*.png destination_dir





          share|improve this answer


























          • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

            – user2842390
            3 hours ago













          • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

            – user1794469
            3 hours ago













          • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

            – Nasir Riley
            3 hours ago











          • @NasirRiley he edited the question.

            – user1794469
            3 hours ago











          • @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

            – Nasir Riley
            3 hours ago



















          4














          cp aaa*.png /some/destdir


          This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.



          This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.



          In that case, use something like the following loop:



          for name in aaa*.png; do
          cp "$name" /some/destdir
          done


          This would copy the files one by one.



          A more efficient method for many thousands of files would be (using GNU cp with its -t option):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +


          Or (without GNU cp):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +


          This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.



          More on find using -exec: Understanding the -exec option of `find`






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago



















          0














          With find:



          find . -maxdepth 1 -type f -name aaa*.png -exec cp {} destination ;


          . indicates the current directory.



          -maxdepth 1 tells it to only look in the current directory



          type -f tells it to look for files



          -name aaa*.png indicates files beginning with aaa and ending in .png



          -exec cp {} destination ; copies the files into the directory called destination.



          My environment requires me to escape the * with a . Yours may not require this so you may be able to just use aaa*.png






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago











          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
          });


          }
          });






          user2842390 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495878%2fcopy-files-with-match-prefix-and-suffix-with-shell-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can do this with a one liner:



          grep '^aaa.*.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/


          grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'



          If you are in the directory of the files you can just cp them with:



          cp aaa*.png destination_dir





          share|improve this answer


























          • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

            – user2842390
            3 hours ago













          • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

            – user1794469
            3 hours ago













          • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

            – Nasir Riley
            3 hours ago











          • @NasirRiley he edited the question.

            – user1794469
            3 hours ago











          • @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

            – Nasir Riley
            3 hours ago
















          0














          You can do this with a one liner:



          grep '^aaa.*.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/


          grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'



          If you are in the directory of the files you can just cp them with:



          cp aaa*.png destination_dir





          share|improve this answer


























          • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

            – user2842390
            3 hours ago













          • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

            – user1794469
            3 hours ago













          • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

            – Nasir Riley
            3 hours ago











          • @NasirRiley he edited the question.

            – user1794469
            3 hours ago











          • @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

            – Nasir Riley
            3 hours ago














          0












          0








          0







          You can do this with a one liner:



          grep '^aaa.*.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/


          grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'



          If you are in the directory of the files you can just cp them with:



          cp aaa*.png destination_dir





          share|improve this answer















          You can do this with a one liner:



          grep '^aaa.*.png$' list.txt | xargs -I '{}' cp '{}' destination_dir/


          grep is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp which moves them to 'destination_dir'



          If you are in the directory of the files you can just cp them with:



          cp aaa*.png destination_dir






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          user1794469user1794469

          1,5721822




          1,5721822













          • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

            – user2842390
            3 hours ago













          • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

            – user1794469
            3 hours ago













          • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

            – Nasir Riley
            3 hours ago











          • @NasirRiley he edited the question.

            – user1794469
            3 hours ago











          • @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

            – Nasir Riley
            3 hours ago



















          • Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

            – user2842390
            3 hours ago













          • That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

            – user1794469
            3 hours ago













          • @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

            – Nasir Riley
            3 hours ago











          • @NasirRiley he edited the question.

            – user1794469
            3 hours ago











          • @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

            – Nasir Riley
            3 hours ago

















          Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

          – user2842390
          3 hours ago







          Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?

          – user2842390
          3 hours ago















          That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

          – user1794469
          3 hours ago







          That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with cp

          – user1794469
          3 hours ago















          @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

          – Nasir Riley
          3 hours ago





          @user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.

          – Nasir Riley
          3 hours ago













          @NasirRiley he edited the question.

          – user1794469
          3 hours ago





          @NasirRiley he edited the question.

          – user1794469
          3 hours ago













          @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

          – Nasir Riley
          3 hours ago





          @user1794469 And it still states that he is looking to copy files. The only way for grep to apply would be the put the names into a text file before using grep which isn't necessary when find is already available.

          – Nasir Riley
          3 hours ago













          4














          cp aaa*.png /some/destdir


          This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.



          This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.



          In that case, use something like the following loop:



          for name in aaa*.png; do
          cp "$name" /some/destdir
          done


          This would copy the files one by one.



          A more efficient method for many thousands of files would be (using GNU cp with its -t option):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +


          Or (without GNU cp):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +


          This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.



          More on find using -exec: Understanding the -exec option of `find`






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago
















          4














          cp aaa*.png /some/destdir


          This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.



          This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.



          In that case, use something like the following loop:



          for name in aaa*.png; do
          cp "$name" /some/destdir
          done


          This would copy the files one by one.



          A more efficient method for many thousands of files would be (using GNU cp with its -t option):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +


          Or (without GNU cp):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +


          This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.



          More on find using -exec: Understanding the -exec option of `find`






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago














          4












          4








          4







          cp aaa*.png /some/destdir


          This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.



          This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.



          In that case, use something like the following loop:



          for name in aaa*.png; do
          cp "$name" /some/destdir
          done


          This would copy the files one by one.



          A more efficient method for many thousands of files would be (using GNU cp with its -t option):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +


          Or (without GNU cp):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +


          This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.



          More on find using -exec: Understanding the -exec option of `find`






          share|improve this answer















          cp aaa*.png /some/destdir


          This would match all filenames starting with the string aaa and ending in the string .png and copy them all to the directory /some/destdir. The * would match any number of any characters in the middle of the name.



          This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.



          In that case, use something like the following loop:



          for name in aaa*.png; do
          cp "$name" /some/destdir
          done


          This would copy the files one by one.



          A more efficient method for many thousands of files would be (using GNU cp with its -t option):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +


          Or (without GNU cp):



          find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +


          This last find command would find all regular files (-type f) under the current directory (only, due to -maxdepth 1) whose names matches the pattern aaa*.png, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.



          More on find using -exec: Understanding the -exec option of `find`







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          KusalanandaKusalananda

          125k16236389




          125k16236389













          • Thank you very much!

            – user2842390
            2 hours ago



















          • Thank you very much!

            – user2842390
            2 hours ago

















          Thank you very much!

          – user2842390
          2 hours ago





          Thank you very much!

          – user2842390
          2 hours ago











          0














          With find:



          find . -maxdepth 1 -type f -name aaa*.png -exec cp {} destination ;


          . indicates the current directory.



          -maxdepth 1 tells it to only look in the current directory



          type -f tells it to look for files



          -name aaa*.png indicates files beginning with aaa and ending in .png



          -exec cp {} destination ; copies the files into the directory called destination.



          My environment requires me to escape the * with a . Yours may not require this so you may be able to just use aaa*.png






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago
















          0














          With find:



          find . -maxdepth 1 -type f -name aaa*.png -exec cp {} destination ;


          . indicates the current directory.



          -maxdepth 1 tells it to only look in the current directory



          type -f tells it to look for files



          -name aaa*.png indicates files beginning with aaa and ending in .png



          -exec cp {} destination ; copies the files into the directory called destination.



          My environment requires me to escape the * with a . Yours may not require this so you may be able to just use aaa*.png






          share|improve this answer


























          • Thank you very much!

            – user2842390
            2 hours ago














          0












          0








          0







          With find:



          find . -maxdepth 1 -type f -name aaa*.png -exec cp {} destination ;


          . indicates the current directory.



          -maxdepth 1 tells it to only look in the current directory



          type -f tells it to look for files



          -name aaa*.png indicates files beginning with aaa and ending in .png



          -exec cp {} destination ; copies the files into the directory called destination.



          My environment requires me to escape the * with a . Yours may not require this so you may be able to just use aaa*.png






          share|improve this answer















          With find:



          find . -maxdepth 1 -type f -name aaa*.png -exec cp {} destination ;


          . indicates the current directory.



          -maxdepth 1 tells it to only look in the current directory



          type -f tells it to look for files



          -name aaa*.png indicates files beginning with aaa and ending in .png



          -exec cp {} destination ; copies the files into the directory called destination.



          My environment requires me to escape the * with a . Yours may not require this so you may be able to just use aaa*.png







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          Nasir RileyNasir Riley

          2,471249




          2,471249













          • Thank you very much!

            – user2842390
            2 hours ago



















          • Thank you very much!

            – user2842390
            2 hours ago

















          Thank you very much!

          – user2842390
          2 hours ago





          Thank you very much!

          – user2842390
          2 hours ago










          user2842390 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user2842390 is a new contributor. Be nice, and check out our Code of Conduct.













          user2842390 is a new contributor. Be nice, and check out our Code of Conduct.












          user2842390 is a new contributor. Be nice, and check out our Code of Conduct.
















          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%2f495878%2fcopy-files-with-match-prefix-and-suffix-with-shell-script%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

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio