Copy files from one directory into an existing directory

BashFileDirectoryCopyCp

Bash Problem Overview


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 Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

cp dir1/* dir2

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

cp -r dir1/* dir2

Solution 3 - Bash

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.

Solution 4 - Bash

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).

Solution 5 - Bash

For inside some directory, this will be use full as it copy all contents from "folder1" to new directory "folder2" inside some directory.

$(pwd) will get path for current directory.

Notice the dot (.) after folder1 to get all contents inside folder1

cp -r $(pwd)/folder1/. $(pwd)/folder2

Solution 6 - Bash

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).

Solution 7 - Bash

Nov, 2021 Update:

This code with Flag "-R" copies perfectly all the contents of "folder1" to existing "folder2":

cp -R folder1/. folder2

Flag "-R" copies symbolic links as well but Flag "-r" skips symbolic links so Flag "-R" is better than Flag "-r".

  • The latest GNU Grep 3.7:
-R, --dereference-recursive

For each directory operand, read and process all files in that directory, 
recursively, following all symbolic links.
-r, --recursive

For each directory operand, read and process all files in that directory, 
recursively. Follow symbolic links on the command line, but skip symlinks 
that are encountered recursively. Note that if no file operand is given, 
grep searches the working directory. This is the same as the 
‘--directories=recurse’ option.

Solution 8 - Bash

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).

Solution 9 - Bash

the correct option should be -T. used with -r to copy recursively.

$ cp -r -T t1 t2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid ChangView Question on Stackoverflow
Solution 1 - BashNickView Answer on Stackoverflow
Solution 2 - BashBertrand MarronView Answer on Stackoverflow
Solution 3 - BashKarl GiesingView Answer on Stackoverflow
Solution 4 - BashKonkretView Answer on Stackoverflow
Solution 5 - BashBidyashish KumarView Answer on Stackoverflow
Solution 6 - BashGordon DavissonView Answer on Stackoverflow
Solution 7 - BashKai - Kazuya ItoView Answer on Stackoverflow
Solution 8 - BashJim DennisView Answer on Stackoverflow
Solution 9 - BashyuanjianpengView Answer on Stackoverflow