The following two commands, used in a Terminal, can accomplish what you've asked, the first creating a destination hierarchical directory structure containing the *.mp3 files and the second command deleting the *.mp3 files from the source hierarchical directory structure.
Note: Make sure you have proper backups before using the second command as a mistype can be fatal.
Open Terminal.
By default Terminal opens to one's Home Directory so I'll use it as the example starting point and the Music directory as the source hierarchical directory structure.
This first command will make a copy of the Music hierarchical directory structure containing only *.mp3 files in the destination directory. In other words after the first command is run there will be a destination directory containing that same hierarchical directory structure starting at Music within the destination directory that contains only *.mp3 files and only the directories from the source that contain *.mp3 files. If a given source directory does not contain a *.mp3 file then that directory is not created in the destination directory hierarchical structure.
Note: Full command line extends out of view without horizontal scrolling.
find ./Music -name "*.mp3" -type f -print0 | xargs -0 -I '{}' /usr/bin/rsync -avR "{}" "./mp3 files only/"
Note: Full command shown on two lines for clarity:
find ./Music -name "*.mp3" -type f -print0 | \
xargs -0 -I '{}' /usr/bin/rsync -avR "{}" "./mp3 files only/"
When the above command finishes there will be a directory in my Home Directory named "mp3 files only" and within that directory there will be a Music hierarchical directory structure containing only *.mp3 files. Note: If the destination directory does not exist it will automatically be created.
Now that the *.mp3 files have been copied from the source Music hierarchical directory structure to the "mp3 files only" directory the *.mp3 files can be removed from the source Music hierarchical directory structure with the following command.
find ./Music/ -name "*.mp3" -delete
That's it, the *.mp3 files along with their hierarchical directory structure have been copied to a new location and the *.mp3 files deleted from the source.
The rsync command line tool is able to merge two folders.
You'll need to open a terminal window. Take a look at the manual pages
man rsync
There may be a one liner but the following steps are non-destructive since you won't change your source folders.
- Create a new folder (dest)
- Sync source1_path to dest
- Sync source2_path to dest
Here the script - check the manual pages for additional options
rsync -rv source1_path/ dest
rsync -rv source2_path/ dest
The following commands also create copies for non-identical files which exist in both source directories (with a difference in size or timestamp for example)
rsync -rv --backup --suffix=_source1 source1_path/ dest
rsync -rv --backup --suffix=_source2 source2_path/ dest
Best Answer
Finder has supported merging folders since 10.7:
If the target folder has at least one file that is newer than the corresponding file in the source folder, Finder merges the folders by keeping the newer files, and the Merge button is shown as Keep Newer:
The dialog with the Merge or Keep Newer button is shown if you copy a single folder but not if you move a folder or copy multiple folders.
Another option is to use
rsync
:Options:
-a
: archive (-rlptgoD
or--resursive --links --perms --times --group --owner --devices --specials
)-E
: preserve extended attributes, resource forks, and ACLs-nv
: show what files would be copied (--dry-run --verbose
)--ignore-existing
: don't replace files that are older in the destination--delete
: delete files from the destination that don't have corresponding files in the source