I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).
-
3Related: How can I copy a folder from the Linux command line? at SUkenorb– kenorb2015-05-27 18:14:24 +00:00Commented May 27, 2015 at 18:14
-
3Related: Copy files from one directory into an existing directory at SOkenorb– kenorb2015-05-27 21:16:27 +00:00Commented May 27, 2015 at 21:16
-
1Related: Copy directory contents using 'cp' command at SUvhs– vhs2019-12-19 11:34:28 +00:00Commented Dec 19, 2019 at 11:34
-
Related: How can I copy the contents of a folder to another folder in a different directory? at AUvhs– vhs2019-12-19 11:38:23 +00:00Commented Dec 19, 2019 at 11:38
3 Answers
The option you're looking for is -R.
cp -R path_to_source path_to_destination/
- If
destinationdoesn't exist, it will be created. -Rmeanscopy directories recursively. You can also use-rsince it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-aflag along with trailing/.in the source (as per@muni764's /@Anton Krug's comment):
cp -a path_to_source/. path_to_destination/
8 Comments
cp -r src/. dest I know it is mentioned but I still seem to miss it every time.-ra when -a already includes -r?You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you're copying is called dir1 and you want to copy it to your /home/Pictures folder:
cp -r dir1/ ~/Pictures/
Linux is case-sensitive and also needs the / after each directory to know that it isn't a file. ~ is a special character in the terminal that automatically evaluates to the current user's home directory. If you need to know what directory you are in, use the command pwd.
When you don't know how to use a Linux command, there is a manual page that you can refer to by typing:
man [insert command here]
at a terminal prompt.
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you've started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R, -r, and --recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r. It's important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
5 Comments
-r option in this will copy directories recursively.cp -r command, but that it wasn't working properly?-r, --recursive, and -R are equivalent. It will also give common pitfalls, etc. which is nice.cp -r dir1 ~/Pictures/Use:
$ cp -R SRCFOLDER DESTFOLDER/
1 Comment
demo1_copy did not exist already $ ls demo1 demo3 README.md $ cp -R demo1/ demo1_copy/