Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

~/dir1/dir2/regexpmatch.txt

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

/media/backup/dir1/dir2/regexpmatch.txt

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

share|improve this question
add comment

3 Answers

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
add comment

You should combine find and rsync commands this way:

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

Example of usage:

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

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

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.