I want to move some subset of files from dirA to dirB (let's say files with "blah" in the filename), but I want all the nested directories to be the same in the new location. How can I do that?
-
possible duplicate of Rsync filter: copying one pattern only – Gilles 'SO- stop being evil' Apr 08 '11 at 20:14
3 Answers
The magic of rsync
filter rules:
$ rsync -av --filter="+ */" --filter="-! *blah*" /source /dest
Consult the rsync
man page for the details on filter rules, but here's the condensed version for this particular need.
--filter="+ */"
means "include everything that is a directory"
--filter="-! *blah*
means "exclude everything that does NOT include blah in the filename"

- 6,970
- 3
- 28
- 19
If you need to copy these files cp will do:
cd dirA
find . -iname "*blah*" | xargs -If cp --parents f dirB
Option --parents
preserves subdirs - it creates the full directory path for the destination.
This worked for me:
rsync -ave 'ssh -p 22' --filter="+ */" --exclude="*_blah.blah" uid@555.55.555.55:/source/directory/ /destination/directory/
The -e
switch defines rsync
transfer protocol with port as -p 22
. Also, the trailing /
slashes are important to let the program know it's dealing with directories.
Thanks @pdo for the extra hours in my work day!

- 101