View Full Version : [SOLVED] Recursive copy with exclusions

November 9th, 2009, 07:41 PM
I require a solution for this problem:

Let there be a directory original/ with the following structure:

Let there also be a file named directives with the following content:

How would I recursively copy the contents of original/ to new/ and exclude the files and directories listed in directives. The original directory tree must be reproduced in new/.
I've tried a few things with find and xargs to little avail (they would copy all the files to new (except those in the excluded directories), but the directory tree would not be preserved).
I'm starting to think about writing my own recursive copy function, but there must surely be an easier way...

November 9th, 2009, 10:53 PM
Use rsync. It's not just for remote directory copies.

You can use some combination of the following options (from the man page) to include/exclude whatever files you want.

-f, --filter=RULE add a file-filtering RULE
-F same as --filter=’dir-merge /.rsync-filter’
repeated: --filter=’- .rsync-filter’
--exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
--include=PATTERN don’t exclude files matching PATTERN
--include-from=FILE read include patterns from FILE
--files-from=FILE read list of source-file names from FILE

November 10th, 2009, 03:54 PM
This looks like it might be exactly what I mean... I guess I've always underestimated the usefulness of rsync... Let me fiddle around with it and I'll let you know...