Merging directories (folders) on Mac OS X
Every now and then I find myself in a situation where I have a folder (I’ll call it
source) of files and nested folders, possibly many levels deep, that I want to copy into another folder (which I’ll call
target already contains some of the files and folders I’m copying, and it also has files and folders that are not present in
target’s parent folder in the Mac OS X Finder will replace everything in
target with the contents of
source. This is not always what I want, and in my opinion it’s one of the biggest flaws of the Mac OS X Finder. Not just Mac OS X actually – back in the pre-Mac OS X days there was a utility called Speed Doubler that patched the Finder to add a smart replace option when copying files.
It’s possible to manually open each folder and their subfolders and copy just the files, but it can be very tedious. There are also third party software options that let you merge files when copying, and if you have Apple’s Developer Tools installed there is the FileMerge utility.
However, you can open a Terminal window and copy the files from the command line, which saves you from installing extra software. Since I keep looking up the syntax every time I need to do this I decided to document it here for future reference.
One command line utility that can copy directories without replacing everything in them is cp:
cp -pRv source/ target
pRv options do the following:
ppreserves timestamps, flags, modes, and ownerships of files
Rcopies the entire subtree
vmakes cp output the name of each file that is copied
/ after the name of the source directory is important since it tells cp to copy the contents of the directory and not the directory itself.
You can also use rsync:
rsync -av source/ target
av options do this:
atells rsync to copy recursively and preserve file attributes
vmakes rsync print information to the terminal window about what was copied
Just as with the cp command, the trailing slash after the source directory is important to make sure only the contents of the directory are copied.
Hoping for Finder integration
It would be great if Apple could make it possible to use the Finder to copy folders like this. It could be a secret option somewhere or invoked when you hold certain modifier keys when copying. It doesn’t matter as long as it’s possible.
Most people probably don’t need this feature every day, but when you do need it it can save lots of time. Having the feature built into the Finder would also reduce the risk of people accidentally deleting files because they don’t realise copying folders replaces everything inside them.
- Previous post: Only use ems for the total width of em-based layouts
- Next post: Flexible height vertical centering with CSS, beyond IE7