I have a need for this hack every now and then, and I *always* forget it for some reason, so I’m putting it here for safe keeping. I’ll start a “hacks” category, too, so I can locate these quickly, and so you can too 🙂 So, here’s the hack:
[jonesy@cranford testing]$ ls bar foo [jonesy@cranford testing]$ ls foo 1 2 3 [jonesy@cranford testing]$ ls bar 1 2 3 4 5 6 [jonesy@cranford testing]$ yes n | cp -i bar/* foo 2>/dev/null [jonesy@cranford testing]$ ls foo 1 2 3 4 5 6 [jonesy@cranford testing]$ ls bar 1 2 3 4 5 6
I’m piping a constant “no” to the “cp -i” command, which asks you if you want to overwrite (or not) any files that already exist in the target directory. You don’t have to send STDERR to /dev/null — there’s just some messy output if you don’t. The end result is that the copy command will only copy files into the destination directory if they don’t already exist. It’ll skip files that exist in both directories (Well, technically the *copy* command won’t, but the operation will when you do it this way).
Of course, I could just forcibly overwrite the directory contents, but I don’t know if the files that exist in both directories are identical. Or I could move one sideways and the other into place, but the same issue exists.