1

This is not a problem as much as my curiosity of the different cp -r behavior. This is on the latest Ubuntu. I have an outdated copy in a directory with several subdirectories. I'd like to update the outdated copy by simply overwriting the directory with the newer one. New files are added and same file are simply overwritten.

So at first I did:

$cp -r <path>/curr_dir <path>/destination_dir 

New files in curr_dir are added but same_name files are not updated, hmm?

But, if I:

$cd <path>/curr_dir
$cp -r * <path>/destination_dir/

Then the newer files are added and the outdated files are overwritten.

Why this difference? I checked my .bashrc and my .profile and I don't see any special aliasing of the cp command.

Surely I'm missing something. Any comments, thoughts?

Thanks

5
  • 1
    The former command would copy the directory curr_dir into destination_dir/curr_dir.
    – Fred Foo
    Dec 22, 2012 at 18:00
  • edit: right, so I'm guessing when you copy a directory it doesn't update the existing files?
    – janjust
    Dec 22, 2012 at 18:00
  • The two commands are simply very different. If you have a file curr_dir/a, then the former will copy that to destination_dir/curr_dir/a, the latter to destination_dir/a.
    – Fred Foo
    Dec 22, 2012 at 18:03
  • Would you agree that it will suffice to explain this difference with cp dir will copy the directory including any files and subdirectories vs. cp dir/ will copy files including any files in sub-directories. (Hope that makes sense)
    – janjust
    Dec 22, 2012 at 18:15
  • That's not correct. The difference is between cp -r dir otherdir and cp -r dir/* otherdir. The former copies the directory, the latter the files and subdirectories in the directory. dir vs. dir/ makes no difference.
    – Fred Foo
    Dec 22, 2012 at 18:36

1 Answer 1

2

Using cp in your case is not really what you want: it won't delete files which are not there anymore, for one. What you want is rsync:

rsync -a --delete /source/directory/ /destination/directory/

Note the ending slashes, they do matter.

Add the -v option to see progress, and if you want to test, add the --dry-run option as well.

2
  • Thanks, but I wanted to keep the existing files which were deleted in the active directory. I guess I was just confused (still am) over why copying the directory vs. directory/ into a new one doesn't replace the files that already exist. But if I copy from within the directory they are replaced including files in the subdirectories.
    – janjust
    Dec 22, 2012 at 18:12
  • Just skip the --delete option then ;)
    – fge
    Dec 22, 2012 at 18:21

You must log in to answer this question.