You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
An alternate is rsync:
rsync -a source/ destination
The advantages of rsync are:
- After the initial sync, it will then copy only the files that have changed.
- You can use it over a network, convenient for files in $HOME, especially config files.
bash - Copy files from one directory into an existing directory - Stack Overflow
cp - How do I create a copy of a directory in Unix/Linux? - Stack Overflow
Linux cp command to copy a folder to current directory - Stack Overflow
cp - Copy only regular files from one directory to another - Unix & Linux Stack Exchange
Videos
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.
cp dir1/* dir2
Or if you have directories inside dir1 that you'd want to copy as well
cp -r dir1/* dir2
cp -r /path/to/someDirA/folderTwo /path/to/someDirB/
The -r option to cp tells it to recurse on directories, copying its contents.
You can use cp to copy files and directories:
cp -r /path/to/someDirA/folderTwo /path/to/someDirB
The -r option is needed when copying directories.
Alternatively, you can use rsync:
rsync -a /path/to/someDirA/folderTwo /path/to/someDirB
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/
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).
Just omit the -T parameter, as that's what prevents the command from working properly:
cp -r /home/hope/subfolder .
The -T parameter treats the target argument as a file, so no copying will be performed at all if that is actually a directory.
A friendly reminder: virtually all Unix commands have a --help command line argument that is worth trying out in case of a trouble :)
For me the main barrier was the /home part. I needed to copy files from a folder in my home that started with the letter 'a' to my current folder, which was not home. So I used:
cp home/tmp/a* ./
the first line worked for me. While I was trying commands like:
cp ~/home/tmp/a* ./
but this didn't work.
cp dir1/* dir2
cp will not copy directories unless explicitly told to do so (with --recursive for example, see man cp).
Note 1: cp will most likely exit with a non-zero status, but the files will have been copied anyway. This may be an issue when chaining commands based on exit codes:&&, ||, if cp -r dir1/* dir2; then ..., etc. (Thanks to contrebis for their comment on that issue)
Note 2: cp expects the last parameter to be a single file name or directory. There really should be no wildcard * after the name of the target directory. dir2\* will be expanded by the shell just like dir1\*. Unexpected things will happen:
- If
dir2is empty and depending on your shell and settings:- you may just get an error message, which is the best case scenario.
dir2/*will be taken literally (looking for a file/directory named*), which will probably lead to an error, too, unless*actually exists.dir2/*it will just be removed from the command entirely, leavingcp dir1/*. Which, depending on the expansion ofdir1/*, may even destroy data:- If
dir1/*matches only one file or directory, you will get an error fromcp. - If
dir1/*matches exactly two files, one will be overwritten by the other (Bad). - If
dir/*matches multiple files and the last match is a, you will get an error message. - If the last match of
dir/*is a directory all other matches will be moved into it.
- If
- If
dir2is not empty, it again depends:- If the last match of
dir2/*is a directory,dir1/*and the other matches ofdir2/*will be moved into. - If the last match of
dir2/*is a file, you probably will get an error message, unlessdir1/*matches only one file.
- If the last match of
It's the shell that expands wildcards, not the commands. So cp dir1/* dir2/* first expands the two wildcards, then calls cp on the result. This is not at all what you apparently expect: depending on how many files there are already in dir2, dir2/* may expand to one or more argument. The command cp doesn't know which of its arguments came from expanding the first pattern and which ones came from expanding the second pattern. It expects its last argument to be the name of the destination directory. Thus, to copy all the files from the directory dir1 into the directory dir2, the last argument must be the directory dir2:
cp dir1/* dir2
Since * matches all files, cp attempts to copy all files. This includes directories: directories are files too. It skips directories, but reports an error. It copies the content of special files such as named pipes (something had better be writing to them, or cp will block), etc.
To copy only regular files, you need to restrict the matching. In zsh, you can use the glob qualifier . for that:
cp dir1/*(.) dir2
Other shells don't have this. You can use the find command to filter on file types. Assuming that you're running non-embedded Linux or Cygwin:
find dir1 -maxdepth 1 -type f -exec cp -t dir2 {} +
On Linux, FreeBSD and OSX:
find dir1 -maxdepth 1 -type f | xargs -I {} cp {} dir2
please let me know about the command that I should use to copy a file from a sub-directory which is under a parent directory under home directory to the current directory under the same parent directory ?