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

4

8

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?

Premarital answered 16/7, 2013 at 16:24 Comment(0)
B
5

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.
Backgammon answered 16/7, 2013 at 17:42 Comment(0)
U
5

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

Underplay answered 16/10, 2013 at 12:33 Comment(3)
Should that be n- or -n in the --filter="dir-merge,n- .rsync-exclude"? What dos the n do?Sparkle
@GiffordN. to be honest, this is too long ago and I also don't find the answer right now, sry :/ But I would guess it defines the file that rsync should use in each folder for filtering.Underplay
I found it, it should be n- as described in the FILTER RULES section of the rsync man pages. Additionally, the n specifies that the rules are not inherited by subdirectories. and the - means that the rules that are read in from the file default to having the exclude modifier set, e.g. dir-merge,n- .non-inherited-per-dir-excludesSparkle
P
0

If you don't mind using tar instead of pure rsync, then you can use the tar --exclude-tag=FILE option. This obviously doesn't get the incremental backup benefits of rsync, so it may not be exactly what you're looking for.

tar cv --exclude-tag=.rsync-exclude ...
Preinstruct answered 13/9, 2023 at 21:19 Comment(0)
P
-3
rsync -avz --exclude 'dir' /source /destination 
Parcheesi answered 20/3, 2018 at 12:53 Comment(2)
Please, add a short description in the answer to help users understandBehave
This does not address the question.Dundalk

© 2022 - 2024 — McMap. All rights reserved.