Change file name while copying based on source path












0















I have a large number of files in directories of the format */*/*/*/*.txt and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt, I'd like to copy it to dest/ so that its path after copying is dest/A_B_C_D_E.txt. Is this possible?










share|improve this question



























    0















    I have a large number of files in directories of the format */*/*/*/*.txt and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt, I'd like to copy it to dest/ so that its path after copying is dest/A_B_C_D_E.txt. Is this possible?










    share|improve this question

























      0












      0








      0








      I have a large number of files in directories of the format */*/*/*/*.txt and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt, I'd like to copy it to dest/ so that its path after copying is dest/A_B_C_D_E.txt. Is this possible?










      share|improve this question














      I have a large number of files in directories of the format */*/*/*/*.txt and I would like to copy them into a different place while replacing the forward-slashes in the path with underscores. For example, if a file is located at A/B/C/D/E.txt, I'd like to copy it to dest/ so that its path after copying is dest/A_B_C_D_E.txt. Is this possible?







      files rename






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 16 at 6:51









      David ScottDavid Scott

      132




      132






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use script like this:



          for i in `find . -type f -name "*.txt"`
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done


          If the number of files if very big you can try with while instead of for



          while read i
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done < (find . -type f -name "*.txt")


          P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers






          share|improve this answer





















          • 3





            This will break on a bunch of filenames.

            – jordanm
            Jan 16 at 6:58











          • @jordanm, can you please clarify?

            – Romeo Ninov
            Jan 16 at 6:59






          • 2





            @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

            – Kusalananda
            Jan 16 at 7:15








          • 2





            @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

            – Kusalananda
            Jan 16 at 7:24






          • 1





            I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

            – David Scott
            Jan 17 at 2:20













          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%2f494743%2fchange-file-name-while-copying-based-on-source-path%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use script like this:



          for i in `find . -type f -name "*.txt"`
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done


          If the number of files if very big you can try with while instead of for



          while read i
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done < (find . -type f -name "*.txt")


          P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers






          share|improve this answer





















          • 3





            This will break on a bunch of filenames.

            – jordanm
            Jan 16 at 6:58











          • @jordanm, can you please clarify?

            – Romeo Ninov
            Jan 16 at 6:59






          • 2





            @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

            – Kusalananda
            Jan 16 at 7:15








          • 2





            @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

            – Kusalananda
            Jan 16 at 7:24






          • 1





            I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

            – David Scott
            Jan 17 at 2:20


















          0














          You can use script like this:



          for i in `find . -type f -name "*.txt"`
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done


          If the number of files if very big you can try with while instead of for



          while read i
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done < (find . -type f -name "*.txt")


          P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers






          share|improve this answer





















          • 3





            This will break on a bunch of filenames.

            – jordanm
            Jan 16 at 6:58











          • @jordanm, can you please clarify?

            – Romeo Ninov
            Jan 16 at 6:59






          • 2





            @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

            – Kusalananda
            Jan 16 at 7:15








          • 2





            @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

            – Kusalananda
            Jan 16 at 7:24






          • 1





            I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

            – David Scott
            Jan 17 at 2:20
















          0












          0








          0







          You can use script like this:



          for i in `find . -type f -name "*.txt"`
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done


          If the number of files if very big you can try with while instead of for



          while read i
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done < (find . -type f -name "*.txt")


          P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers






          share|improve this answer















          You can use script like this:



          for i in `find . -type f -name "*.txt"`
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done


          If the number of files if very big you can try with while instead of for



          while read i
          do
          newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)
          mv "$i" "dest/$newfile"
          done < (find . -type f -name "*.txt")


          P.S. Be warned about the filenames/directories with nonstandard symbols in filenames. For reference check this question and answers







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 17 at 5:23

























          answered Jan 16 at 6:57









          Romeo NinovRomeo Ninov

          5,77331928




          5,77331928








          • 3





            This will break on a bunch of filenames.

            – jordanm
            Jan 16 at 6:58











          • @jordanm, can you please clarify?

            – Romeo Ninov
            Jan 16 at 6:59






          • 2





            @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

            – Kusalananda
            Jan 16 at 7:15








          • 2





            @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

            – Kusalananda
            Jan 16 at 7:24






          • 1





            I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

            – David Scott
            Jan 17 at 2:20
















          • 3





            This will break on a bunch of filenames.

            – jordanm
            Jan 16 at 6:58











          • @jordanm, can you please clarify?

            – Romeo Ninov
            Jan 16 at 6:59






          • 2





            @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

            – Kusalananda
            Jan 16 at 7:15








          • 2





            @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

            – Kusalananda
            Jan 16 at 7:24






          • 1





            I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

            – David Scott
            Jan 17 at 2:20










          3




          3





          This will break on a bunch of filenames.

          – jordanm
          Jan 16 at 6:58





          This will break on a bunch of filenames.

          – jordanm
          Jan 16 at 6:58













          @jordanm, can you please clarify?

          – Romeo Ninov
          Jan 16 at 6:59





          @jordanm, can you please clarify?

          – Romeo Ninov
          Jan 16 at 6:59




          2




          2





          @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

          – Kusalananda
          Jan 16 at 7:15







          @RomeoNinov Anything with spaces, for example, but also files with globbing patterns. See e.g. unix.stackexchange.com/questions/321697

          – Kusalananda
          Jan 16 at 7:15






          2




          2





          @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

          – Kusalananda
          Jan 16 at 7:24





          @RomeoNinov Note that an answer is never an answer to a single user and their specific setup. An answer may be read years from now and applied to a setup where files may well have spaces and strange characters in their filenames.

          – Kusalananda
          Jan 16 at 7:24




          1




          1





          I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

          – David Scott
          Jan 17 at 2:20







          I was able to fix the leading ._ problem by adding to the newfile line: newfile=$(echo $i|sed -s 's@/@_@g'|cut -c -3)

          – David Scott
          Jan 17 at 2:20




















          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%2f494743%2fchange-file-name-while-copying-based-on-source-path%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 reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?