I'm trying to copy all files and folders from one directory to another, but exclude certain files. Specifically, I want to exclude subversion files and folders. However, I'd like a general yet concise solution.

I imagine I'll find the need to exclude several types of files in the near future. For example, I might want to exclude .svn, *.bak, and *.prj.

Here is what I've put together so for, but it is not working for me. The first part, find works, but I'm doing something wrong with xargs and cp. I tried cp with and without the -R. Also, I'm using OS X and it appears to have a less featured version of xargs than linux systems.

I may be wrong, but I think that this is trickier than you think. Even if your find command properly uses -prune to exclude .svn items, you then pass the -R flag to cp which tells that command to be recursive. When it does so, you lose whatever granularity you had in the find command. I'm going to tinker with this for a minute, but I think the answer is not to use -R in the cp command.
–
TelemachusSep 28 '09 at 14:34

It seems to work for me on a Linux system. Can you be more specific as to what "it is not working" means? Any error messages? Files getting/not getting copied that you expect?
–
Dennis WilliamsonSep 28 '09 at 15:03

It's the -R flag, I'm pretty sure. Remove that and you should be fine (though you might want to add -mindepth 1 to ignore the toplevel directory folder which you don't want to copy, I assume)
–
TelemachusSep 28 '09 at 15:22

I usually do these things by copying everything, and then deleting the unwanted files in the target directories. That's often much simpler.
–
Jan DoggenMay 19 '14 at 19:52

(note: If you're not too familiar with rsync yet, be sure to read on that man page about how rsync deals with a trailing slash in the source path. It behaves differently if you include the slash than if you don't. Search for 'trailing slash')

Oh rats, I just re-read your question and saw that you said you don't have rsync installed. On my MacBook Pro (OS X 10.6.1) it's in /usr/bin/rsync. It was installed for me under Tiger (10.4) and Leopard (10.5) also.
–
Doug HarrisSep 28 '09 at 15:06

I'm pretty sure that you (and the OP) don't want the -R flag in the xargs part of the command.
–
TelemachusSep 28 '09 at 15:13

Good catch, I copied that from the original question. Will edit now.
–
Doug HarrisSep 28 '09 at 15:18

1

Thanks Doug! One thing I preach, "use the right tool for the job". I've recently come from Windows world to OSX and am still fumbling around in ignorance. I posted this same question in a linux channel and someone quickly said, just use "rsynch" I typed "rsynch" in the terminal and saw it didn't exist and continued investigating the find | xargs approach. I'm a bit stubborn like that. Anyhow, OSX does have "rsync" by default, and your post was very helpful. I don't have enough experience here yet to know which is better, but rsync sure is much more concise. Thanks!
–
Michael PrescottSep 29 '09 at 13:50

If you'll be doing any work with linux machines in addition to your work on OS X, I think you'll find it worth your time to learn how to use rsync. Its primary functionality is intelligent copying of just the changed stuff (like robocopy on windows, if you're familiar with that). Because it copies just the delta, it's a great way to handle (non-Time Machine) backups, code deployments and the like.
–
Doug HarrisSep 29 '09 at 14:53

Not to be offensive, but I'm not sure why this answer is getting up-voted. I'm aware of svn's capabilities, but "I'd like a general yet concise solution. I imagine I'll find the need to exclude several types of files in the near future"
–
Michael PrescottSep 29 '09 at 13:55

Thanks Telemachus, it is helpful. I don't have the experience to critique, but I'll repeat what I've been told since my original post. "xargs is broken" The unknown irc commenter that told me that inspired me to look around a bit more and I think Doug Harris's answer addresses the problem. That tell xargs to use null characters. I think that is the -0 switch?
–
Michael PrescottSep 29 '09 at 14:08

@Michael: xargs isn't broken, but on Unix-like systems, the default is not to use filenames (or directory names) with spaces in them. If you have spaces or "funny" characters in your filenames (or directory names), then you have to do extra work to deal with that. (In GNU find, there is a whole section in the man page called "UNUSUAL FILENAMES" because of this issue.) The -0 flag in xargs and -print0 for find help to handle these problems. I promise you, however, that you don't want to use -R in your copy command. It will undo whatever you do to avoid the SVN directories.
–
TelemachusSep 29 '09 at 14:42

Without any benchmarking, my initial thought is that this is a double waste of CPU cycles: the copying and the removing. It may take a little extra human time to craft the right find command, but you only do that once. You may use the command hundreds of times, once you get it right.
–
TelemachusSep 28 '09 at 16:38

@Telemachus - true, but that's what computers are (supposed to be) good at - doing the tricky stuff so we don't have to! Really - what harm is there in copying some files only to have them deleted soon after, if it means the commands you invent to do it are very simple?
–
Steve FollySep 28 '09 at 22:30

@Steve: there's no harm, really. As far as it goes, this is a fine solution. It follows one principle I like: "Do the simplest thing possible that works." On the other hand, it violates another principle I like even more: "Learn your tools." I would prefer to learn how to use find itself better, so that I didn't have to do this. But you're right: there's nothing really wrong with this solution.
–
TelemachusSep 29 '09 at 12:01

@Telemachus: I do agree with "Learn your tools". When I started out, I'm sure my find command above would have looked very cryptic to me then :-)
–
Steve FollySep 30 '09 at 18:44