Copy files with match prefix AND suffix with shell script

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












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




























    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


























      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
















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 22 at 2:50









      Rui F Ribeiro

      40k1479135




      40k1479135










      asked Jan 22 at 0:11









      user2842390user2842390

      33




      33




















          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
            Jan 22 at 0:18












          • 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
            Jan 22 at 0:21












          • @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
            Jan 22 at 0:25











          • @NasirRiley he edited the question.

            – user1794469
            Jan 22 at 0:27






          • 1





            @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
            Jan 22 at 0:30


















          6














          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
            Jan 22 at 1:36


















          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
            Jan 22 at 1:36










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%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
            Jan 22 at 0:18












          • 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
            Jan 22 at 0:21












          • @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
            Jan 22 at 0:25











          • @NasirRiley he edited the question.

            – user1794469
            Jan 22 at 0:27






          • 1





            @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
            Jan 22 at 0:30















          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
            Jan 22 at 0:18












          • 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
            Jan 22 at 0:21












          • @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
            Jan 22 at 0:25











          • @NasirRiley he edited the question.

            – user1794469
            Jan 22 at 0:27






          • 1





            @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
            Jan 22 at 0:30













          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 Jan 22 at 0:19

























          answered Jan 22 at 0:16









          user1794469user1794469

          1,5801822




          1,5801822












          • 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
            Jan 22 at 0:18












          • 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
            Jan 22 at 0:21












          • @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
            Jan 22 at 0:25











          • @NasirRiley he edited the question.

            – user1794469
            Jan 22 at 0:27






          • 1





            @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
            Jan 22 at 0:30

















          • 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
            Jan 22 at 0:18












          • 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
            Jan 22 at 0:21












          • @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
            Jan 22 at 0:25











          • @NasirRiley he edited the question.

            – user1794469
            Jan 22 at 0:27






          • 1





            @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
            Jan 22 at 0:30
















          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
          Jan 22 at 0:18






          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
          Jan 22 at 0:18














          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
          Jan 22 at 0:21






          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
          Jan 22 at 0:21














          @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
          Jan 22 at 0:25





          @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
          Jan 22 at 0:25













          @NasirRiley he edited the question.

          – user1794469
          Jan 22 at 0:27





          @NasirRiley he edited the question.

          – user1794469
          Jan 22 at 0:27




          1




          1





          @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
          Jan 22 at 0:30





          @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
          Jan 22 at 0:30













          6














          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
            Jan 22 at 1:36















          6














          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
            Jan 22 at 1:36













          6












          6








          6







          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 Jan 22 at 0:31

























          answered Jan 22 at 0:21









          KusalanandaKusalananda

          129k16242399




          129k16242399












          • Thank you very much!

            – user2842390
            Jan 22 at 1:36

















          • Thank you very much!

            – user2842390
            Jan 22 at 1:36
















          Thank you very much!

          – user2842390
          Jan 22 at 1:36





          Thank you very much!

          – user2842390
          Jan 22 at 1:36











          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
            Jan 22 at 1:36















          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
            Jan 22 at 1:36













          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 Jan 22 at 0:27

























          answered Jan 22 at 0:22









          Nasir RileyNasir Riley

          2,654249




          2,654249












          • Thank you very much!

            – user2842390
            Jan 22 at 1:36

















          • Thank you very much!

            – user2842390
            Jan 22 at 1:36
















          Thank you very much!

          – user2842390
          Jan 22 at 1:36





          Thank you very much!

          – user2842390
          Jan 22 at 1:36

















          draft saved

          draft discarded
















































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


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

          But avoid


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

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

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




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?