0

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

/
    my_work
               /dir1
                    keepdir1.ddd
                    keepdir2.ddd
                    file.cfg
                    (lots of files and directories I don't want)
               /dir2
                    (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*/*
- *
2
  • 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. Commented May 2, 2012 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. Commented May 2, 2012 at 12:36

1 Answer 1

1

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"
done

Sample output, with non-applicable wildcards producing messages:

dest
dest/my_work
dest/my_work/dir1
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
dest/my_work/dir2
my_work/dir2/file.cfg -> dest/my_work/dir2/file.cfg
cp: my_work/dir2/keepdir*: No such file or directory
dest/my_work/dir3
cp: my_work/dir3/*.cfg: No such file or directory
cp: my_work/dir3/keepdir*: No such file or directory
1
  • That's perfect! Thanks so much. That's way easier than fooling with rsync! Commented May 2, 2012 at 13:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.