8

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?

1

3 Answers 3

10

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"

0
4

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.

1
  • the syntax here is much more clear than rsync Apr 11, 2020 at 15:00
0

This worked for me:

rsync -ave 'ssh -p 22' --filter="+ */" --exclude="*_blah.blah" [email protected]:/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!

You must log in to answer this question.

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