Rsync How to Include Directories But Not Files

Rsync: Exclude directory contents, but include directory

The --exclude option takes a PATTERN, which means you just should just be able to do this:

rsync -avz --exclude='dir/to/skip/*' /my/source/path /my/backup/path

Note that the PATTERN is quoted to prevent the shell from doing glob expansion on it.

Since dir/to/skip doesn't match the pattern dir/to/skip/*, it will be included.

Here's an example to show that it works:

> mkdir -p a/{1,2,3}
> find a -type d -exec touch {}/file \;
> tree --charset ascii a
a
|-- 1
| `-- file
|-- 2
| `-- file
|-- 3
| `-- file
`-- file

3 directories, 4 files

> rsync -r --exclude='/2/*' a/ b/
> tree --charset ascii b
b
|-- 1
| `-- file
|-- 2
|-- 3
| `-- file
`-- file

3 directories, 3 files

It is important to note that the leading / in the above PATTERN represents the root of the source directory, not the filesystem root. This is explained in the rsync man page. If you omit the leading slash, rsync will attempt to match the PATTERN from the end of each path. This could lead to excluding files unexpectedly. For example, suppose I have a directory a/3/2/ which contains a bunch of files that I do want to transfer. If I omit the leading / and do:

rsync -r --exclude='2/*' a/ b/

then the PATTERN will match both a/2/* and a/3/2/*, which is not what I wanted.

rsync include specific files and exclude directory

You need to put --exclude='build' before --include '*/'. Both of these rules could apply to the "build" directory, and whichever is given first takes precedence, so to get the --exclude rule to override the --include rule, you need to list it first.

From the rsync man page, in the FILTER RULES section (with my emphasis):

As the list of files/directories to transfer is built, rsync checks
each name to be transferred against the list of include/exclude
patterns in turn, and the first matching pattern is acted on: if it is
an exclude pattern, then that file is skipped; if it is an include
pattern then that filename is not skipped; if no matching pattern is
found, then the filename is not skipped.

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 only directory pattern

This should work:

--include='*/'
--include='*cache*/**'
--exclude='*'
--prune-empty-dirs

That says:

  • Include all folders (this is necessary to search inside them).
  • Include all files with "cache" in the name of a parent directory.
  • Exclude everything else.
  • Prune away any folders that were copied but turned out to contain no caches. Unfortunately, this also removes any empty folders within cache directories, but hopefully that's not important to you.

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"/

Include folder with same name that other in exclude in rsync

There's two ways to do this:

  1. With --include:

    rsync \
    .... \
    --include=app/vendor \
    --exclude=vendor \
    ....

    I.e. put the --include before the --exclude

  2. With a more specific --exclude:

    rsync \
    .... \
    --exclude=/vendor \
    ....

    I.e. exclude only the vendor in the source directory root (not to be confused with the filesystem root).

The basic rule for include/exclude rules is that rsync will use the first rule that matches any given file. You put your specific rules at the top, and the general rules at the bottom. If you get them backward you'll find that your specific override does not work.

Copy whole directory but exclude all folders and subfolders with certain name

Instead of cp, you can use tar with option --exclude to control what you want copied or not.

The full command is:

tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
  • So any path that contains the "outdir" pattern will be excluded.
  • Without the --exclude option, it will copy the entire structure of your current directory under TARGET_DIRECTORY.
  • You can replace the . in the first tar by your desired source directory.

rsync to copy only particular folders

Either use rsync -av src1 src2 src3 ... dst or put all the folders you want to rsync in a text file (each folder in a separate line) and use rsync -arv --files-from=sources.txt dst.

Note that by default -a implies --recursive but not when --files-from is used, so in this case -r must be specified explicitly.



Related Topics



Leave a reply



Submit