3

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.

  • 3
    I can't believe I can't find a duplicate for this: to copy a folder, you have to use the -R option: cp -R /source/path /target/path – kos Sep 26 '15 at 2:19
9

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

3

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.

  • -R, -r, --recursive are the same you know – noraj Nov 1 '18 at 17:05
0

cp does not, by default, copy directories. From man 1posix cp:

2. If source_file is of type directory, the following steps  shall  be
   taken:

    a. If  neither the -R or -r options were specified, cp shall write
       a diagnostic message to standard error, do  nothing  more  with
       source_file, and go on to any remaining files.

You should have got a message like so:

cp: omitting directory '...'

For example:

$ cp Documents Pictures
cp: omitting directory ‘Documents’

As can be seen from the manpage, use -r or -R with cp:

cp -R Documents Pictures

Not the answer you're looking for? Browse other questions tagged or ask your own question.