How to combine folders in macOS using the hidden Merge options and the Ditto command
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.
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)
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.
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)
Select Merge.
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.
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)
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)
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
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 have to admit though, after fading out Easter eggs I slowly also stopped using option everywhere just to see what happens