Copying files from multiple folders into one folder, Mac OS X
:: Thursday, July 9th, 2009 @ 11:42:40 am
:: Tags: Computers
Yesterday, I found myself needing to copy over 40,000 files spread across hundreds of directories into a single target directory. On Mac OS X, I used Spotlight to find all the files in the parent directory where the source files were located. Spotlight reported “more than 10,000 files”, and only let me select 10,000 at a time. Initiating a mouse drag gave me a SPOD for about 15 seconds, but eventually let me continue the drag for copying / moving. After trying this a few times, Mac OS X stopped allowing me to cut, copy or paste in the Finder, and I quickly realized that this problem spanned every application currently open. I rebooted.

lots of files = headache for the finder
I’m no shell expert, but in this instance it is really the way to go. Here’s what I did:
find [source dir] -type f -iname [pattern] -exec cp {} [destination dir] \;
So if I were currently in my source directory and wanted to copy all Photoshop files to a the “/Volumes/echeng/data” directory, the command would look like this:
find . -type f -iname *psd -exec cp {} /Volumes/echeng/data \;
Now let’s say I had, hypothetically, that I had previously attempted to copy by dragging thousands of files around with varying degrees of success. I might want to ignore files that had already been copied. Adding the “-n” flag to the copy command ignores existing files.
find . -type f -iname *psd -exec cp -n {} /Volumes/echeng/data \;
Seems to work!
Note that if your destination directory is not a directory, this will not work. If you specify “mv” instead of “cp”, and your destination directory is not a directory, the command will actually have the effect of deleting all of your source files. Do some tests before you run this on your real data!
Yay UNIX!!
If it’s important to you, you can also preserve the modified and created date of the files by using
cp -pinstead ofcp. =)Glad that you’re tagging your computer entries…gonna be sifting through a lot of them, for sure, as my girlfriend got a MacBook Pro and I have to prep it / trick it out for her. Yes, it’s funny that I’m going to have to learn my way around a Mac. And, yes, I’m kind of afraid I’m going to grow to like it.