How to combine folders in macOS using the hidden Merge options and the Ditto command

Posted:
in macOS edited October 2020
If you ever need to combine two identically-named folders together, but they contain a mix of old, new, and duplicate files, it's likely you will want to be selective in how they are merged together. AppleInsider offers a basic guide to using the Merge option in Finder to keep only the files you want intact.




Merging is a useful process for turning two folders into one, but it isn't required often. One common scenario is if two or more similar projects need to be combined into one, like if a developer needs to put together different versions of an application together, when they were previously held in separate similar folders.

In this and other cases, if there are identically named files or folders held within each of the source folders, it is generally preferred for the newer versions of files to be retained via the merge. In the developer example, it could mean only more recently developed application files could be kept while older files are removed completely.

There are a few ways to perform the merge, but it does depend on the amount of files involved, one way is usually better.

Moving Files

To merge two identically-named folders, with each containing files using unique names, simply bring the two up in separate Finder windows and decide which folder you want to keep the merged files inside. Select and drag the files you want to move from the source folder to the destination folder.




If there are files with identical names, macOS will bring up a warning message stating that one already exists in the location, and to Keep Both, Stop, or Replace. Stop will stop the process, Replace will replace the version in the destination folder with the file being dragged in, and Keep Both will rename one of the incoming files to add the suffix "Copy," allowing it to be placed in the folder alongside its possible duplicate.

A fourth option called Skip is also available, and replaces the Keep Both button when the Option Key is pressed. Selecting Skip will prevent the currently-identified file from copying to the destination, instead keeping it in the original folder and preserving its counterpart.

Without pressing Option (above), with Option (below)
Without pressing Option (above), with Option (below)


The selection of Replace, Keep Both, and Skip can be used on an individual file basis, but can also be applied to all files being moved by selecting the Apply To All checkbox.

Moving Folders

If there are too many files and folders within the folders you want to merge, or you just want to copy everything instead of selecting specific files, it may be a better option to transfer the folder completely.

For this, decide which folder you want to keep the files inside, hold the Option key down, and drag the other identically-named folder on top.




Normally, macOS will warn that a folder with the same name exists in the location you are dragging it to, with options to Stop the process or Replace everything in the destination with the dragged folder's files. By holding Option, the notification adds a third Merge button, as well as advising it will preserve all newer versions of files, and how many files will change.

Without pressing Option (above), with Option (below)
Without pressing Option (above), with Option (below)


Select Merge.

Subfolders, Terminal, and Ditto

If there are subfolders within the two folders being merged, Finder will also perform the function to them as well. However, Finder will only analyze the top level folders for newer or older versions of files and offer to merge if at least one file is different, and not take into account the contents of subfolders.

Rather than searching for an app to merge, an option is to use the Ditto command within Terminal.

Load up a Terminal window, which can be found in the Other section of Launchpad, or by going to the Applications folder and then Utilities. It can also be brought up using Spotlight, by pressing Command-Space, typing "Terminal", and pressing Return.

Once in the Terminal Window, type the Ditto command followed by a space, the path of the source folder you want to merge files from, another space, then the path of the destination folder. Instead of typing the folder paths, they can also be entered by dragging and dropping each folder into the Terminal window.

Note that you will need to make the entire command use a single line, as in ditto sourcefolder destinationfolder, and not to press return between each folder path.




Once entered, press Return to set Ditto running.

Comments

  • Reply 1 of 8
    ednlednl Posts: 61member
    I didn’t know about ditto. The man page doesn’t explain how the merge works; only by file modification time? I use “rsync -auv folder1/ folder2/“ for that, but rsync has lots of other options for tuning the merging criteria.
  • Reply 2 of 8
    viclauyycviclauyyc Posts: 849member
    Is there an app for that?
  • Reply 3 of 8
    maltzmaltz Posts: 453member
    Actually, the man page does describe how ditto merges, and it contradicts the article:
    ditto overwrites existing files, symbolic links, and devices in the destination when these are copied from a source.  The resulting files, links, and devices will have the same mode, access time, modification time, owner, and group as the source items from which they are copied.

    When I tested it, newer versions of files in the destination folder are, in fact, overwritten by older versions in the source folder.  An "rsync -auvEH" (or "rsync -auvAXH" if you're using the non-macified version) command probably comes the closest to mimicking the Finder's behavior, but it might still miss a few obscure things like HFS compression and quarantine info that the ditto command preserves, according to its man page.

    The way the Finder does it one way for files in the root folder, but differently for subfolders is supremely unintuitive, imo, with serious risk of accidental data loss.  No wonder it's hidden.

    edited March 2018 marqueymarc
  • Reply 4 of 8
    Extremely useful, thanks!
  • Reply 5 of 8
    wonkothesanewonkothesane Posts: 1,717member
    Again I found there’s still pretty basic stuff I didn’t know about. Even after today - 1987 years of Mac. 
    I have to admit though, after fading out Easter eggs I slowly also stopped using option everywhere just to see what happens ;)
    steveauSpamSandwich
  • Reply 6 of 8
    urashidurashid Posts: 127member
    I wonder why they didn't make the "Option" dialog box the default for drag operation. Normally, Option changes the functionality (for example, from copy to move) but in this case it is simply a superset of the basic dialog box. And adding the Merge button (with explanation) hardly complicates it.
  • Reply 7 of 8
    marqueymarcmarqueymarc Posts: 1unconfirmed, member
    maltz said:
    Actually, the man page does describe how ditto merges, and it contradicts the article:
    ditto overwrites existing files, symbolic links, and devices in the destination when these are copied from a source.  The resulting files, links, and devices will have the same mode, access time, modification time, owner, and group as the source items from which they are copied.

    When I tested it, newer versions of files in the destination folder are, in fact, overwritten by older versions in the source folder.  An "rsync -auvEH" (or "rsync -auvAXH" if you're using the non-macified version) command probably comes the closest to mimicking the Finder's behavior, but it might still miss a few obscure things like HFS compression and quarantine info that the ditto command preserves, according to its man page.

    The way the Finder does it one way for files in the root folder, but differently for subfolders is supremely unintuitive, imo, with serious risk of accidental data loss.  No wonder it's hidden.

    I can confirm that in the merge the source file if present takes precedence, unfortunately.  NOT newest takes precedence.  
  • Reply 8 of 8
    SpamSandwichSpamSandwich Posts: 33,407member
    There were so many times in the past I could’ve used that Merge option. Infuriating it wasn’t readily apparent previously.
Sign In or Register to comment.