107

I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.

I use

cp -r /origin /destination

sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.

I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.

Braiam
  • 4,777
Open the way
  • 9,221

9 Answers9

233

Just use cp -n <source> <dest>.

From man page:

-n, --no-clobber

do NOT overwrite an existing file (overrides a previous -i option)

Balmipour
  • 2,519
68

cp -R -u -p /source /destination

The -u (or --update) flag does just this:

From the man page for cp:

-u, --update

copy only when the SOURCE file is newer than the destination file or when the destination file is missing

ifconfig
  • 697
user31894
  • 2,937
30

rsync -aq /src /dest

Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.

6

POSIX solution

Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.

Still, we can reproduce yes n with a while loop. This leads to the following POSIX solution:

while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
6

Use cp -rn <sourcedirname>/. <destdirname>

The r switch makes the copy recursive over the directories.

The n switch (long version no-clobber) ensures existing files are never over-written.

The '/.' after the sourcedirname ensures that it does not become a subdirectory under the destdirname instead of all contents of the former being copied to the latter.

3

Look up the "-u" option for the cp command.

Pointy
  • 887
2

All above answers are correct but if you are doing this recursively then

you should do:

 cp -rn SOURCE_PATH DESTINATION_PATH
grepit
  • 253
1

You should be copying as root to maintain permissions/ownership

# cp -au

Also look at rsync

pixelbeat
  • 1,380
0

If your source location is a remote file system, and you don't have shell access to it (and thus can't use rsync), then this might help:

cd /local/destination/path
echo "reget /remote/source/path" | sftp -r sftp://username@domainOrIpAddress

From this answer.

Note that reget is the same as get -a. It checks for the presence of half-copied files and finishes copying them.

joe
  • 101
  • 2