Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I would like to do a backup where I recursively go through a dir structure and only get directories with files whose names matching a particular regexp. I would like to keep the directory structure, so that if there is a match at


it creates the same dir structure in the target and the file is copied over to


preferably with rsync, but if that is not possible another program would do.

share|improve this question

How about combining find with tar? An example to find all .c files:

find . -type f -name '*.c' -print | tar zcf backup.tar.gz -T -
share|improve this answer

You should combine find and rsync commands this way:

find . -regex .*X | rsync --files-from=- ./ b/

Example of usage:

$ find b/
$ find a/
$ rm -rf b
$ find . -regex .*X | rsync --files-from=- ./ b/
$ find b/

Here I copy only that files that are conform to the regular expression .*X.

share|improve this answer

From the rsync man page:

Here s  an  example that copies all .pdf files in a hierarchy, only creating
the necessary destination directories to hold the .pdf  files,  and  ensures
that  any  superfluous  files and directories in the destination are removed
(note the hide filter of non-directories being used instead of an exclude):

rsync -avm --del --include= *.pdf  -f  hide,! */  src/ dest

If you didn t want to remove superfluous destination files, the  more  time-
honored  options  of "--include= */  --exclude= * " would work fine in place
of the hide-filter (if that is more natural to you).
share|improve this answer

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.