Copy files from one directory into an existing directory










189















In bash I need to do this:



  1. take all files in a directory


  2. copy them into an existing directory


How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don't want that, I need the files in t1 to go directly inside t2. How do I do this?










share|improve this question



















  • 7





    Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 10:38












  • Take a look at this stackoverflow.com/questions/23698183/…

    – theGiallo
    Apr 22 '17 at 15:53











  • I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

    – David A. Gray
    Nov 1 '18 at 16:44















189















In bash I need to do this:



  1. take all files in a directory


  2. copy them into an existing directory


How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don't want that, I need the files in t1 to go directly inside t2. How do I do this?










share|improve this question



















  • 7





    Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 10:38












  • Take a look at this stackoverflow.com/questions/23698183/…

    – theGiallo
    Apr 22 '17 at 15:53











  • I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

    – David A. Gray
    Nov 1 '18 at 16:44













189












189








189


41






In bash I need to do this:



  1. take all files in a directory


  2. copy them into an existing directory


How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don't want that, I need the files in t1 to go directly inside t2. How do I do this?










share|improve this question
















In bash I need to do this:



  1. take all files in a directory


  2. copy them into an existing directory


How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don't want that, I need the files in t1 to go directly inside t2. How do I do this?







bash file directory copy cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 16:28









Vadim Kotov

4,61063447




4,61063447










asked Sep 4 '10 at 20:12









David ChangDavid Chang

946273




946273







  • 7





    Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 10:38












  • Take a look at this stackoverflow.com/questions/23698183/…

    – theGiallo
    Apr 22 '17 at 15:53











  • I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

    – David A. Gray
    Nov 1 '18 at 16:44












  • 7





    Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 10:38












  • Take a look at this stackoverflow.com/questions/23698183/…

    – theGiallo
    Apr 22 '17 at 15:53











  • I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

    – David A. Gray
    Nov 1 '18 at 16:44







7




7





Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 9 '14 at 10:38






Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 9 '14 at 10:38














Take a look at this stackoverflow.com/questions/23698183/…

– theGiallo
Apr 22 '17 at 15:53





Take a look at this stackoverflow.com/questions/23698183/…

– theGiallo
Apr 22 '17 at 15:53













I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

– David A. Gray
Nov 1 '18 at 16:44





I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.

– David A. Gray
Nov 1 '18 at 16:44












6 Answers
6






active

oldest

votes


















309














What you want is:



cp -R t1/. t2/


The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.






share|improve this answer




















  • 2





    I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

    – Martin von Wittich
    Sep 16 '13 at 11:18






  • 2





    Amazing. Where is this documented? Is it POSIX or extension?

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 11:14







  • 7





    @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

    – striking
    Mar 12 '16 at 18:32






  • 1





    cp -R t1/. t2/ worked for me

    – Martin Gottweis
    May 24 '16 at 6:57


















30














If you want to copy something from one directory into the current directory, do this:



cp dir1/* .


This assumes you're not trying to copy hidden files.






share|improve this answer


















  • 15





    Misses hidden files.

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 9 '14 at 11:11


















25














cp dir1/* dir2


Or if you have directories inside dir1 that you'd want to copy as well



cp -r dir1/* dir2





share|improve this answer


















  • 3





    Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

    – ssokolow
    Sep 4 '10 at 20:17






  • 2





    Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

    – Gordon Davisson
    Sep 5 '10 at 18:37







  • 1





    You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

    – RobW
    Apr 14 '12 at 18:17



















4














Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:



sudo cp -R t1/* t2/


Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:



sudo cp -R t1/* ./


Or you can navigate to the folder with files (t1) and do this:



sudo cp -R ./* t2/


Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).






share|improve this answer
































    2














    cp -R t1/ t2


    The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/* misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).






    share|improve this answer























    • Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

      – zpon
      Feb 6 '13 at 7:32


















    2














    Depending on some details you might need to do something like this:



    r=$(pwd)
    case "$TARG" in
    /*) p=$r;;
    *) p="";;
    esac
    cd "$SRC" && cp -r . "$p/$TARG"
    cd "$r"


    ... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.



    The extra fussing is to handle relative or absolute targets.



    (This doesn't rely on subtle semantics of the cp command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp and I don't know if they'll continue to be so in the future).






    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%2f3643848%2fcopy-files-from-one-directory-into-an-existing-directory%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      309














      What you want is:



      cp -R t1/. t2/


      The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.






      share|improve this answer




















      • 2





        I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

        – Martin von Wittich
        Sep 16 '13 at 11:18






      • 2





        Amazing. Where is this documented? Is it POSIX or extension?

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:14







      • 7





        @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

        – striking
        Mar 12 '16 at 18:32






      • 1





        cp -R t1/. t2/ worked for me

        – Martin Gottweis
        May 24 '16 at 6:57















      309














      What you want is:



      cp -R t1/. t2/


      The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.






      share|improve this answer




















      • 2





        I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

        – Martin von Wittich
        Sep 16 '13 at 11:18






      • 2





        Amazing. Where is this documented? Is it POSIX or extension?

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:14







      • 7





        @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

        – striking
        Mar 12 '16 at 18:32






      • 1





        cp -R t1/. t2/ worked for me

        – Martin Gottweis
        May 24 '16 at 6:57













      309












      309








      309







      What you want is:



      cp -R t1/. t2/


      The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.






      share|improve this answer















      What you want is:



      cp -R t1/. t2/


      The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 30 '16 at 12:29









      Doguhan Uluca

      3,53223047




      3,53223047










      answered Jan 10 '11 at 9:11









      NickNick

      5,34263762




      5,34263762







      • 2





        I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

        – Martin von Wittich
        Sep 16 '13 at 11:18






      • 2





        Amazing. Where is this documented? Is it POSIX or extension?

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:14







      • 7





        @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

        – striking
        Mar 12 '16 at 18:32






      • 1





        cp -R t1/. t2/ worked for me

        – Martin Gottweis
        May 24 '16 at 6:57












      • 2





        I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

        – Martin von Wittich
        Sep 16 '13 at 11:18






      • 2





        Amazing. Where is this documented? Is it POSIX or extension?

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:14







      • 7





        @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

        – striking
        Mar 12 '16 at 18:32






      • 1





        cp -R t1/. t2/ worked for me

        – Martin Gottweis
        May 24 '16 at 6:57







      2




      2





      I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

      – Martin von Wittich
      Sep 16 '13 at 11:18





      I really like this syntax, but for some reason it doesn't work with mv. Does anyone know why?

      – Martin von Wittich
      Sep 16 '13 at 11:18




      2




      2





      Amazing. Where is this documented? Is it POSIX or extension?

      – Ciro Santilli 新疆改造中心 六四事件 法轮功
      Jan 9 '14 at 11:14






      Amazing. Where is this documented? Is it POSIX or extension?

      – Ciro Santilli 新疆改造中心 六四事件 法轮功
      Jan 9 '14 at 11:14





      7




      7





      @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

      – striking
      Mar 12 '16 at 18:32





      @CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called ".", which is the same as the directory holding it, the copy behaves this way. Let's say t1 contains a file called "file". cp will perform the operation equivalent to "cp t1/./file t2/./". It is copying the folder ".", but copying files into t2's "." folder strips the "./" because "t2/./" is the same as "t2/". Technically, this means it's POSIX built in behavior... but probably not in the way you might have been expecting!

      – striking
      Mar 12 '16 at 18:32




      1




      1





      cp -R t1/. t2/ worked for me

      – Martin Gottweis
      May 24 '16 at 6:57





      cp -R t1/. t2/ worked for me

      – Martin Gottweis
      May 24 '16 at 6:57













      30














      If you want to copy something from one directory into the current directory, do this:



      cp dir1/* .


      This assumes you're not trying to copy hidden files.






      share|improve this answer


















      • 15





        Misses hidden files.

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:11















      30














      If you want to copy something from one directory into the current directory, do this:



      cp dir1/* .


      This assumes you're not trying to copy hidden files.






      share|improve this answer


















      • 15





        Misses hidden files.

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:11













      30












      30








      30







      If you want to copy something from one directory into the current directory, do this:



      cp dir1/* .


      This assumes you're not trying to copy hidden files.






      share|improve this answer













      If you want to copy something from one directory into the current directory, do this:



      cp dir1/* .


      This assumes you're not trying to copy hidden files.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 17 '12 at 8:55









      Karl GiesingKarl Giesing

      1,07331220




      1,07331220







      • 15





        Misses hidden files.

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:11












      • 15





        Misses hidden files.

        – Ciro Santilli 新疆改造中心 六四事件 法轮功
        Jan 9 '14 at 11:11







      15




      15





      Misses hidden files.

      – Ciro Santilli 新疆改造中心 六四事件 法轮功
      Jan 9 '14 at 11:11





      Misses hidden files.

      – Ciro Santilli 新疆改造中心 六四事件 法轮功
      Jan 9 '14 at 11:11











      25














      cp dir1/* dir2


      Or if you have directories inside dir1 that you'd want to copy as well



      cp -r dir1/* dir2





      share|improve this answer


















      • 3





        Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

        – ssokolow
        Sep 4 '10 at 20:17






      • 2





        Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

        – Gordon Davisson
        Sep 5 '10 at 18:37







      • 1





        You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

        – RobW
        Apr 14 '12 at 18:17
















      25














      cp dir1/* dir2


      Or if you have directories inside dir1 that you'd want to copy as well



      cp -r dir1/* dir2





      share|improve this answer


















      • 3





        Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

        – ssokolow
        Sep 4 '10 at 20:17






      • 2





        Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

        – Gordon Davisson
        Sep 5 '10 at 18:37







      • 1





        You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

        – RobW
        Apr 14 '12 at 18:17














      25












      25








      25







      cp dir1/* dir2


      Or if you have directories inside dir1 that you'd want to copy as well



      cp -r dir1/* dir2





      share|improve this answer













      cp dir1/* dir2


      Or if you have directories inside dir1 that you'd want to copy as well



      cp -r dir1/* dir2






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 4 '10 at 20:14









      Bertrand MarronBertrand Marron

      15.4k74381




      15.4k74381







      • 3





        Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

        – ssokolow
        Sep 4 '10 at 20:17






      • 2





        Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

        – Gordon Davisson
        Sep 5 '10 at 18:37







      • 1





        You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

        – RobW
        Apr 14 '12 at 18:17













      • 3





        Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

        – ssokolow
        Sep 4 '10 at 20:17






      • 2





        Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

        – Gordon Davisson
        Sep 5 '10 at 18:37







      • 1





        You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

        – RobW
        Apr 14 '12 at 18:17








      3




      3





      Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

      – ssokolow
      Sep 4 '10 at 20:17





      Depending on how your shell is configured, you'll probably need to use "dir1/* dir1/.*" instead of "dir1/*" if you want to also catch hidden files.

      – ssokolow
      Sep 4 '10 at 20:17




      2




      2





      Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

      – Gordon Davisson
      Sep 5 '10 at 18:37






      Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you're actually trying to copy). It also copies dir1/. which is fine, except that it's already (mostly) been copied, so you're doing the work twice.

      – Gordon Davisson
      Sep 5 '10 at 18:37





      1




      1





      You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

      – RobW
      Apr 14 '12 at 18:17






      You can get around the dir1/.*/hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as .. So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir], cp . [path to destination dir, with no trailing slash].

      – RobW
      Apr 14 '12 at 18:17












      4














      Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:



      sudo cp -R t1/* t2/


      Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:



      sudo cp -R t1/* ./


      Or you can navigate to the folder with files (t1) and do this:



      sudo cp -R ./* t2/


      Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).






      share|improve this answer





























        4














        Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:



        sudo cp -R t1/* t2/


        Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:



        sudo cp -R t1/* ./


        Or you can navigate to the folder with files (t1) and do this:



        sudo cp -R ./* t2/


        Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).






        share|improve this answer



























          4












          4








          4







          Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:



          sudo cp -R t1/* t2/


          Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:



          sudo cp -R t1/* ./


          Or you can navigate to the folder with files (t1) and do this:



          sudo cp -R ./* t2/


          Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).






          share|improve this answer















          Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:



          sudo cp -R t1/* t2/


          Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:



          sudo cp -R t1/* ./


          Or you can navigate to the folder with files (t1) and do this:



          sudo cp -R ./* t2/


          Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 2:45

























          answered Nov 15 '18 at 2:29









          KonkretKonkret

          937




          937





















              2














              cp -R t1/ t2


              The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/* misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).






              share|improve this answer























              • Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

                – zpon
                Feb 6 '13 at 7:32















              2














              cp -R t1/ t2


              The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/* misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).






              share|improve this answer























              • Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

                – zpon
                Feb 6 '13 at 7:32













              2












              2








              2







              cp -R t1/ t2


              The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/* misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).






              share|improve this answer













              cp -R t1/ t2


              The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/* misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 4 '10 at 21:14









              Gordon DavissonGordon Davisson

              69.3k97894




              69.3k97894












              • Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

                – zpon
                Feb 6 '13 at 7:32

















              • Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

                – zpon
                Feb 6 '13 at 7:32
















              Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

              – zpon
              Feb 6 '13 at 7:32





              Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)

              – zpon
              Feb 6 '13 at 7:32











              2














              Depending on some details you might need to do something like this:



              r=$(pwd)
              case "$TARG" in
              /*) p=$r;;
              *) p="";;
              esac
              cd "$SRC" && cp -r . "$p/$TARG"
              cd "$r"


              ... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.



              The extra fussing is to handle relative or absolute targets.



              (This doesn't rely on subtle semantics of the cp command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp and I don't know if they'll continue to be so in the future).






              share|improve this answer



























                2














                Depending on some details you might need to do something like this:



                r=$(pwd)
                case "$TARG" in
                /*) p=$r;;
                *) p="";;
                esac
                cd "$SRC" && cp -r . "$p/$TARG"
                cd "$r"


                ... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.



                The extra fussing is to handle relative or absolute targets.



                (This doesn't rely on subtle semantics of the cp command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp and I don't know if they'll continue to be so in the future).






                share|improve this answer

























                  2












                  2








                  2







                  Depending on some details you might need to do something like this:



                  r=$(pwd)
                  case "$TARG" in
                  /*) p=$r;;
                  *) p="";;
                  esac
                  cd "$SRC" && cp -r . "$p/$TARG"
                  cd "$r"


                  ... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.



                  The extra fussing is to handle relative or absolute targets.



                  (This doesn't rely on subtle semantics of the cp command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp and I don't know if they'll continue to be so in the future).






                  share|improve this answer













                  Depending on some details you might need to do something like this:



                  r=$(pwd)
                  case "$TARG" in
                  /*) p=$r;;
                  *) p="";;
                  esac
                  cd "$SRC" && cp -r . "$p/$TARG"
                  cd "$r"


                  ... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.



                  The extra fussing is to handle relative or absolute targets.



                  (This doesn't rely on subtle semantics of the cp command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp and I don't know if they'll continue to be so in the future).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 4 '10 at 21:41









                  Jim DennisJim Dennis

                  12.6k74287




                  12.6k74287



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3643848%2fcopy-files-from-one-directory-into-an-existing-directory%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin