23

I'm trying to copy a large directory from one drive to another. I mistakenly logged out before it was finished so only about 80% of the files copied over. Is there away to copy the remaining files without starting from scratch?

35

I would try,

rsync -a /from/file /dest/file

you can use other options like --append, -P (--partial --progress). See man rsync for more info.

Or if you are using cp then use cp -u.

from man cp:

-u, --update
     copy only when the SOURCE file is newer than the destination file or when the destination file is missing.
  • 1
    I'll have to give it to Rahul cus he just beat you to it, but thanks and everyone gets an upvote! – barro32 Aug 5 '16 at 11:30
  • 3
    If the first copy ended abruptly, one of the files may be copied only partially, and cp -u might not care, since it only checks timestamps, not the size... So you'd need to touch the source files first if you have to use it. (Or just use rsync.) – ilkkachu Aug 6 '16 at 20:11
  • @ilkkachu that's right. I will update it soon. – Rahul Aug 8 '16 at 5:58
  • Does cp ever leave a single file only partially transfered? If it does, then -u seems like it would leave you with a directory that was only partially working. – Trevor Aug 4 '18 at 4:36
15

For partial file transfers, cp might not be what you need.

I suggest using rsync instead. Even when transfer fails, you can simply re-run the command at a later time, and it'll copy only the missing files.

Example:

$ rsync -aPEmivvz from/ to/

(will copy from/ into to/ directory)

Since rsync does syncing and checking after each transfer, ensuring files have been copied correctly is trivial.

The swich set -aPEmivvz is my standard go-to selection, whenever copying files over networks or external drives, including plug-in devices, like SD-Cards, etc.

These are the switches I use almost always:

-a: "archive", includes -rlptgo (recursive, symlinks as symlinks, permissions, times, group, owner)

-P: "partial progress" shows nice progress bar for each file

-E: "executability" preserve whether executable or not

-m: "noempty" prunes empty dirs

-i: "summary" print change summary for all updates

-vv: "more verbose"

-z: "compression"

6

What you probably want rather than cp in this case is rsync.

Just recursively copying one directory structure to a new location:

rsync -av /path/to/source /path/to/destination 
4

You should not use cp for this. As others have stated it's not the right tool for the job. The correct tool is going to depend on your goals.

If you just want a straight copy:

rsync /source /destination with some level of flags. See the other answers for some really good examples.

If you want to have bi-directional updates

unison is the way to go. Rsync doesn't do very well at conflict resolution with bidirectional syncs. Specially deletes. So you you want bi-directional copies fire up unison

If you want backups and the files are text based, then you can try using git. Git can be run locally, there's no need for a "server". In fact, git servers are only file sharing methods. You could totally have a git repo on the backup drive and "push" to it. Again this only really works if the files are text based and your trying to get a one way backup.

Again if your going for a backup (and not just one time) then look at deja-dupe, BackIntime, or similar options. They take snapshots of the data at a point in time. They all run rsync under the hood.

4

You can also copy all files again, ask for confirmation for each overwrite, then automatically answer no to the question:

yes n | cp -i /source /dest

(you have probably used more options, like -r, so just add them like cp -pri /source /dest ...)

3

Use the command rsync with the following options:

rsync --progress --partial --append /source_file /destination_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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