Mike
Mike

Reputation: 2464

Merge two directories with large content on slow storage efficiently

I have two directory structures on a USB drive that have various files that are the same, and each have files that the other one doesn't.

What I want to do is to move over directory structure B to A. It is ok that B's content is gone after the merge. Directories in A must not be erased, because otherwise I lose A's content. The mv command won't work I think, because it will complain that it can't move a directory because a destination directory in the same place is not empty. mv B/* A/ won't work either because some sub directory will also not be empty. cp -a B/* A/ is bad (even with -u), because it will take way too long, because the files are on a USB drive, and there possibly too many of them, making the drive run out of capacity. rsync has the same problem, because it doesn't appear to have a move/rename feature, and it can only move files by copying them.

So either, I'm going to have to write a script that will recursively run through B, and create missing directories and move missing files to A. But I'm hoping that there is a command or option or utility that I don't know about.

Upvotes: 1

Views: 209

Answers (1)

twalberg
twalberg

Reputation: 62399

I believe cpio has the capabilities you are wanting. This command:

cd B
find . -type f -print0 | cpio -0dumpl A/.

Will find all files in B, pass them to cpio with null termination to properly handle odd file names, create necessary directories (cpio -d), preserve ownership, permissions and timestamps (-m), and use linking to create the destination files where possible (-l) unconditionally (-u).

Upvotes: 2

Related Questions