How to use include and exclude precedence with rsync?
I'm unsure if there's a more native way to cross post an answer, but I asked on Stack Exchange and got this excellent answer that solved what I was trying to do perfectly, with context.
rsync: How to apply a recursively include filter while excluding other files
Try first including folders themselves:
rsync -zarv \
--include="/*.html" \
--include="/js/" \
--include="/js/**/" \
--include="/js/**.min.js" \
--include="/js/**.min.js" \
--include="/js/**.min.js.map" \
--include="/css/" \
--include="/css/**/" \
--include="/css/**.min.css" \
--include="/css/**.min.css.map" \
--include="/img/***" \
--exclude="*" \
--delete \
./ "$to"/
Make rsync exclude all directories that contain a file with a specific name
rsync does not support this (at least the manpage does not mention anything), but you can do it in two steps:
- run
find
to find the.rsync-exclude
files 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.
Using Rsync include and exclude options to include directory and file by pattern
The problem is that --exclude="*"
says to exclude (for example) the 1260000000/
directory, so rsync
never examines the contents of that directory, so never notices that the directory contains files that would have been matched by your --include
.
I think the closest thing to what you want is this:
rsync -nrv --include="*/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
(which will include all directories, and all files matching file_11*.jpg
, but no other files), or maybe this:
rsync -nrv --include="/[0-9][0-9][0-9]0000000/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
(same concept, but much pickier about the directories it will include).
Rsync include, exclude and filters
I finally found where my issue was. I had the source dir in double quotes thus interpreting the * as a literal... Thanks anyway.
Complex rsync filtering (includes and excludes)
If I understand the situation correctly, you need to add the option --prune-empty-dirs
(in short, -m
) to your command. Those directories are not matched by the exclude rule which exludes their children, so they are copied by default. -m
prevents this. If you want to prevent other files to be copied, you must add specific exclude rules for them. For instance, the files 1.txt
, 2.txt
and so on can be excluded by --exclude=/*.txt
. All in all, your command should look as follows:
rsync -avhr \
--progress \
--prune-empty-dirs \
--include="/20170409_*/schedule/" \
--include="/20170410_*/schedule/" \
--include="/20170411_*/schedule/" \
--exclude="/*.txt" \
--exclude="/*/*" \
/source/dateTimeDirectories/ \
/destination/dateTimeDirectories/
Related Topics
Signal Handling in Asm: Why am I Receiving Sigsegv When Invoking the Sys_Pause Syscall
Removing Sensitive Data from Git. "Fatal: Ambiguous Argument 'Rm'"
Compiler Can't Find Libxml/Parser.H
Multiplication with Expr in Shell Script
Replace All Lines That Do Not Contain Matched String
How to Rename Multiple Files Beginning with a Unix Timestamp - Imapsync Issue
Oracle:Io Exception: the Network Adapter Could Not Establish the Connection
Using Gnu/Linux System Call 'Splice' for Zero-Copy Socket to Socket Data Transfers in Haskell
How to *Only* Get the Number of Bytes Available on a Disk in Bash
Specifying Non-Standard Baud Rate for Ftdi Virtual Serial Port Under Linux
Read a File and Split Each Line into Multiple Variables
Less Gets Keyboard Input from Stderr
Match a String That Contains a Newline Using Sed