How to copy-merge two directories?

  • I have two directories images and images2 with this structure in Linux:


    ... and other 4000 folders

    and the other is like:


    ... and other 4000 folders

    Each of these folders contain images and the directories' names under images and images2 are exactly the same, however their content is different. Then I want to know how I can copy-merge the images of /images2/ad into images/ad, the images of /images2/foo into images/foo and so on with all the 4000 folders..

    are the end files named the same in both directories?

    Nope... for example in images/ad are 1.jpg, 2.jpg and 3.jpg. But in images2/ad are 4.jpg and 5.jpg

    @AmirAliAkbari, I don't think that it is a duplicate - the other question basically is 'Does mv do merging?' (answer: no). This question is about how to merge 2 directory hierarchies.

  • This is a job for rsync. There's no benefit to doing this manually with a shell loop unless you want to move the file rather than copy them.

    rsync -a /path/to/source/ /path/to/destination

    In your case:

    rsync -a /images2/ /images/

    (Note trailing slash on images2, otherwise it would copy to /images/images2.)

    If images with the same name exist in both directories, the command above will overwrite /images/SOMEPATH/SOMEFILE with /images2/SOMEPATH/SOMEFILE. If you want to replace only older files, add the option -u. If you want to always keep the version in /images, add the option --ignore-existing.

    If you want to move the files from /images2, with rsync, you can pass the option --remove-source-files. Then rsync copies all the files in turn, and removes each file when it's done. This is a lot slower than moving if the source and destination directories are on the same filesystem.

    ..add -P if you'd like to see progress..

    I would add that there's no benefit to using a tricky shell loop here even if you *do* want to move them instead of copying them—in that case just use `rsync`, then `rm -r /images`.

    @Wildcard, well, that's not quite the same as moving. As Gilles points out, it's a lot slower than moving if they're on the same fs; and moreover it requires a lot more temporary spae.

    I'd also like to point out that **it's important to include the trailing slashes for each directory**. For example, if you simply ran `rsync -a images images2`, it will just copy images2 into images instead of merging them.

    FWIW, in the order of the directories in the command here is backward from the user's original question—as written, this answer will copy the contents of /images/ into /images2/, but the user's question had it going the other way around.

    @s3cur3 Oh. From 2 to (implicit) 1. That's unintuitive. Thanks, I fixed my answer.

    Is there a way to make rsync ASK you if you want to overwrite or not?

    @MaxCoplan No. Rsync isn't an interactive tool.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM