We want to migrate contents of an old server to a new one but rather than coping everything, we want to exclude any directory that has a .swf file in it. We are aware of the --exclude flag but it will only exclude the file(s), not the parent directory (and it's contents), if there is a .swf file

If this cannot be done with rsync, is there a bash script we can use to copy files from one server to another, with this condition?

1 Answer 1


You could generate a list of directories which contain *.swf files, and then convert that list into an exclude file for rsync.


find /path/to/topdir/ -iname '*.swf' -printf "%h\n" |
    sort -u |
    sed -e 's/^/- /; s:$:/**:' > rsync-exclude.txt

output will look something like this, which can be used with rsync's --exclude-from=FILE option:

- /path/to/topdir/directory1/**
- /path/to/topdir/directory2/**
- /path/to/topdir/directory3/**
- /path/to/topdir/directory5/subdir1/**

Note that rsync's include and exclude options can be difficult to master, they do not work quite like either globs or regular expressions. You will want to practice with --dry-run and --verbose first until you get the patterns exactly right.

  • I'm getting sed: -e expression #1, char 16: unterminated `s' command. Any ideas why?
    – ibex
    Jul 24, 2017 at 6:37
  • i must have somehow deleted the trailing : in the second sed expression. fixed now.
    – cas
    Jul 24, 2017 at 6:39
  • If there are .swf files in sub directories, they will be listed in the output file. But this is not a problem at all when using the text file in --exclude flag. Unfortunately still don't have enough reputation to upvote.
    – ibex
    Jul 24, 2017 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .