rsync文件同步包含文件/目录(--exclude)和排除文件/目录(--include)用法

首页 > 技术

rsync文件同步包含文件/目录(--exclude)和排除文件/目录(--include)用法

来源:Eddy 发布时间:2021-12-01 10:55

在讲解用法之前,先看看官方文档有这么一段话:

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.

理解这段话是弄明白如何使用--include和--exclude的核心。rsync在同步文件时,会按你写的匹配参数顺序去匹配文件名,第一个被匹配上的会对应生效。如果是exclude,那么这个文件会被排除;如果是include,这个文件不会被排除;如果没有匹配上,这个文件也不会被排除。所以写--include、--exclude参数时,顺序很重要。

假设要同步的源目录结构如下:

/root/s/

├── java

│   └── 1.txt

├── mysql

│   └── 1.txt

├── php

│   ├── 1.txt

│   ├── java

│   ├── java.txt

│   ├── php11.txt

│   └── zzz

│       └── jjj

│           └── hei.txt

└── php111.txt


现在我想同步 /root/s/ 目录到 /root/d/ 目录,但是我只想同步 php 子目录的所有内容,那该怎么写呢?正确的写法如下:

rsync -av --include="php" --include="php/**" --exclude="*" /root/s/ /root/d/


如果只想同步 php 子目录下的文件和子目录,但不包括子目录下的内容,应该这样写:

rsync -av --include="php" --include="php/*" --exclude="*" /root/s/ /root/d/


上一篇:男子早餐店吃... 下一篇:她打仗比花木...
猜你喜欢
热门阅读
同类推荐