13
7
I have two copies of a folder
src/
dest/
I want to merge them, doing the following:
If a file is only in src
, I want it to be moved to dest
If a file is only in dest
, I want it ignored IE left alone.
If a file is in both and has identical contents (IE same size and date), delete from src
If a file is in both and does not have identical contents, leave behind in src
so I can manually merge them.
Only a very small number of files (between 0% and 5% of total files) should be in this last category, but I don't know how to separate the the in both and the same from in both, but different.
I've tried to figure out how to do this with rsync
but to no avail so far.
correction: that command will not remove files from src if an identical copy already exists in dest – Tok – 2010-11-23T18:52:48.927
Yeah :(. That's the part that I'm finding hard to figure out. – David Oneill – 2010-11-23T19:07:46.450
2Well, the good news is that you can solve it independently without much hassle:
for file in \
find src/ -type f`; do diff $file `echo $file | sed 's/src/dest/'` && rm $file || echo $file; done(you can skip the
|| echo $file` if you like, it is included for completeness) – Tok – 2010-11-23T19:16:27.760Nifty: that's what I needed. Edit that into your answer, and I'll accept it! – David Oneill – 2010-11-24T00:33:42.517
@Tok: Your command will choke on file names that contain special characters (whitespace,
\?*[
, initial-
). You need to use double quotes around variable substitutions, pass--
to utilities before file names, usefind … -exec …
instead of parsing the output offind
. With anrm
command in the mix, this is a recipe for disaster. – Gilles 'SO- stop being evil' – 2010-11-24T01:06:09.193@Tok: No, passing
-i
not only won't help getting the right files deleted (obviously), but it won't even help avoiding getting the wrong files deleted. Try (on Linux or Cygwin)touch 'foo -f bar'; rm -i $(echo foo*)
. – Gilles 'SO- stop being evil' – 2010-11-24T19:27:09.577--prune-empty-dirs
might also be a good option, in my case I have x = millions of files, y = x * 1.33 directories. – Alix Axel – 2013-06-04T12:47:32.657