Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make rsync exclude all directories that contain a file with a specific name

I would like rsync to exclude all directories that contain a file with a specific name, say ".rsync-exclude", independent of the contents of the ".rsync-exclude" file.

If the file ".rsync-exclude" contained just "*", I could use rsync -r SRC DEST --filter='dir-merge,- .rsync-exclude'.

However, the directory should be excluded independent of the contents of the ".rsync-exclude" file (it should at least be possible to leave the ".rsync-exclude" file empty).

Any ideas?

like image 647
klaus se Avatar asked Jul 16 '13 16:07

klaus se


2 Answers

rsync does not support this (at least the manpage does not mention anything), but you can do it in two steps:

  1. run find to find the .rsync-exclude files
  2. pipe this list to --exclude-from (or use a temporary file)

       --exclude-from=FILE
          This option is related to the --exclude option, but it specifies a FILE that contains exclude  patterns
          (one per line).  Blank lines in the file and lines starting with ';' or '#' are ignored.  If FILE is -,
          the list will be read from standard input.
    

alternatively, if you do not mind to put something in the files, you can use:

       -F     The -F option is a shorthand for adding two --filter rules to your command.  The first time it is  used
          is a shorthand for this rule:

             --filter='dir-merge /.rsync-filter'

          This  tells  rsync  to  look for per-directory .rsync-filter files that have been sprinkled through the
          hierarchy and use their rules to filter the files in the transfer.  If -F is repeated, it is  a  short-
          hand for this rule:

             --filter='exclude .rsync-filter'

          This filters out the .rsync-filter files themselves from the transfer.

          See the FILTER RULES section for detailed information on how these options work.
like image 189
mnagel Avatar answered Oct 11 '22 13:10

mnagel


Old question, but I had the same one..

You can add the following filter:

--filter="dir-merge,n- .rsync-exclude"

Now you can place a .rsync-exclude file in any folder and write the names of the files and folders you want to exclude line by line. for example:

#.rsync-exclude file

folderYouWantToExclude
allFilesThatStartWithXY*
someSpecialImage.png

So you can use patterns in there too.

What you can't do is:

#.rsync-exclude file

folder/someFileYouWantToExlude

Hope it helps! Cheers

like image 42
Lorenz Haase Avatar answered Oct 11 '22 12:10

Lorenz Haase