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?](http://superuser.com/q/666431/87805) at SU – kenorb May 27 '15 at 18:14
-
3Related: [Copy files from one directory into an existing directory](http://stackoverflow.com/q/3643848/55075) at SO – kenorb May 27 '15 at 21:16
-
1I wish this could be migrated to a SE where it's more on topic. – jrh May 07 '18 at 02:02
-
11Bash programming is [on-topic](https://meta.stackoverflow.com/a/283161/5376789). – xskxzr Jun 29 '18 at 04:43
-
1Related: [Copy directory contents using 'cp' command](https://superuser.com/questions/26586/copy-directory-contents-using-cp-command) at SU – vhs Dec 19 '19 at 11:34
-
Related: [How can I copy the contents of a folder to another folder in a different directory?](https://askubuntu.com/questions/86822/how-can-i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo) at AU – vhs Dec 19 '19 at 11:38
3 Answers
The option you're looking for is -R
.
cp -R path_to_source path_to_destination/
- If
destination
doesn't exist, it will be created. -R
meanscopy directories recursively
. You can also use-r
since it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-a
flag along with trailing/.
in the source (as per@muni764
's /@Anton Krug
's comment):
cp -a path_to_source/. path_to_destination/

- 6,000
- 2
- 35
- 25

- 24,084
- 1
- 14
- 6
-
7i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory. – holms Mar 29 '17 at 00:17
-
14I believe the '/' on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here's [a reference](https://unix.stackexchange.com/questions/870/the-slash-after-a-directory-name-on-shell-commands) with more info. – OllieBrown Jul 19 '18 at 17:02
-
34I would say if you don't want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the **/.** – Anton Krug Jul 26 '18 at 16:16
-
This also seems to work correctly if the directories to be copied are symbolic links, which was my use case. – Gdalya Sep 12 '18 at 16:08
-
Lately that does not seem to be the case. Regardless of including the trailing `/`, ARM OS based on Debian still copy the directory, but not its contents. – Dragas Nov 16 '18 at 09:24
-
14Note the importance of "Slash dot" on your source in `cp -r src/. dest` I know it is mentioned but I still seem to miss it every time. – bbeecher Mar 07 '19 at 02:11
-
1
-
@Chupo_cro you right, `-a` would be completely enough and would do exactly the same. Just sometimes I like to be more explicit. In the comment above the emphasis was on the `/.` part. – Anton Krug Jul 06 '21 at 23:22
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).

- 37,233
- 13
- 109
- 109
-
7To clarify further, the `-r` option in this will copy directories recursively. – TVann Jan 21 '16 at 20:16
-
6Did the OP not specify that he *was* using the `cp -r` command, but that it wasn't working properly? – MD XF Oct 06 '16 at 22:27
-
4The reason I included the info about manpages is these flags can be distribution dependent. However, for the most part, it appears `-r`, `--recursive`, and `-R` are equivalent. It will also give common pitfalls, etc. which is nice. – Alex W Dec 16 '16 at 16:22
-
8If you want to portably copy the DIRECTORY and not just it's contents, leave off the trailing dash in the source folder ie. `cp -r dir1 ~/Pictures/` – krethika Dec 15 '17 at 18:06
-
1A slash on the end of the src is a BSDism. The GNU Linux equivilent is the -T flag. – xpusostomos Aug 05 '20 at 10:44
Use:
$ cp -R SRCFOLDER DESTFOLDER/

- 30,738
- 21
- 105
- 131

- 2,581
- 1
- 13
- 22
-
4this is the way I done it and `demo1_copy` did not exist already ```$ ls demo1 demo3 README.md $ cp -R demo1/ demo1_copy/``` – HattrickNZ Oct 29 '15 at 02:21