Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi let's say I have a directory like this:

                    (lots of files and directories I don't want)
                    (same layout as dir 1)

So I want to copy the directory "my_work" and all of the "dir1", "dir2", etc sub directories. But within each of those I want to keep only certain files, and certain directories. In the directories I keep I want to recursively include everything.

Here's what I've tried so far but nothing seems to copy:

rsync -rl --include-from=~/rsync_include /my_work ~/backup

And the contents of rsync_include are:

+ *file.cfg
+ *keepdir*/*
- *
share|improve this question
Well is there a commonality to the files you want to exclude, so that wildcards could be used? If not, then you have to just list them all in the include file. – Paul May 2 '12 at 12:32
Yeah, I think I could use wildcards but can a directory be a wildcard? Will it know to copy the matching directory recursively and not apply the exclude * inside a match directory? That's my confusion in a nutshell. – Greg May 2 '12 at 12:36
up vote 1 down vote accepted

Unless you forgot to provide important information, the following script will work.

#!/usr/bin/env bash

for DIR in my_work/* ; do
    mkdir -pv dest/"$DIR"
    cp -av "$DIR"/*.cfg "$DIR"/keepdir* dest/"$DIR"

Sample output, with non-applicable wildcards producing messages:

my_work/dir1/file.cfg -> dest/my_work/dir1/file.cfg
my_work/dir1/keepdir1.ddd -> dest/my_work/dir1/keepdir1.ddd
my_work/dir1/keepdir2.ddd -> dest/my_work/dir1/keepdir2.ddd
my_work/dir2/file.cfg -> dest/my_work/dir2/file.cfg
cp: my_work/dir2/keepdir*: No such file or directory
cp: my_work/dir3/*.cfg: No such file or directory
cp: my_work/dir3/keepdir*: No such file or directory
share|improve this answer
That's perfect! Thanks so much. That's way easier than fooling with rsync! – Greg May 2 '12 at 13:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.