2

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.

1

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 -
0

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.

-1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.