Why won't my cp command copy whole directories? [duplicate]
I've tested the cp
command, and it will only copy individual files. For example, I typed cp
followed by the exact filename, then typed the directory and the file got copied. However, when I typed the directory and the destination directory, nothing on the computer changed. I thought cp
was supposed to copy contents in the directories.
I've tried this multiple times.
Solution 1:
When you use cp without any arguments, it defaults to copying files as below
cp sourcefile destinationLocation
#will copy sourcefile to the specified destinationLocation
But if you want to copy a directory, you need to specify the recursive argument like this
cp -R dir1 dir2 #copies dir1 to dir2
cp -R dir dir2 dir3 #copies dir1 & dir2 to dir3
Ideally you can specify as many files as you want to a single destination all separated by spaces. This here below however copies a dir along with its permissions
sudo cp -rp /home/me /media/backup/me
-p same as --preserve=mode,ownership,timestamps
Alternatively you can use rsync
sudo rsync -a /home/me/ /media/backup/me/
-a, --archive
Note that -a does not preserve hardlinks, because finding multiply-linked
files is expensive. You must separately specify -H.
And Please don't forget the trailing slashes when copying using rsync. See the man page for each command on options of using them available on your terminal by typing man cp or man rsync
Solution 2:
Use
cp -R
It specifies recursive copy i.e. all sub-directories and files will be copied.
Use man cp
to find more switches for cp.