4-10.10 Linux 中的文件同步传输 --- rsync --include 传输指定的数据

  • --include 指定要传输的数据,一般结合 --exclude 一起用。
  • 4-10.10 内容:
    通过 rsync -av --include --exclude 学习传输指定的数据。
  • 操作步骤:
    1、创建一些目录及文件用作测试。创建 backups_include 作为备份目录。源目录延用上一节 4-10.9 的 SRC
[root@localhost test]# mkdir backups_include    ## 新建 backups_include 备份目录
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include
└── SRC     ## 源目录
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files
[root@localhost test]# 

2、--include 一般结合 --exclude 一起用。--include 的作用是指定需要传输的内容。但是单纯的用 --include 并不能如我所愿的结果。因为单纯的 --include 没有起到只传输我想要的文件,而是把源目录的所有数据传输到目标目录。
--include 和 --exclude 搭档,--exclude 是起到排除的功能, 也就是当 --include 指定了我们所需要传输的数据时,通过 --exclude 排除其他内容的传输。这样就可以完成指定什么数据需要传输,其他不在范围内的数据都不传输。

下面是单纯的用 --include 指定传输 SRC 目录下所有结尾为 .txt 的文件效果。 单纯的用 --include 没有起到只传输我想要的文件,而是把源目录的所有数据传输到目标目录。

## 通过 tree 查看现在的 test 目录结构, backups_include 目录没有任何数据
[root@localhost test]# tree
.
├── backups_include
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files

## --include 指定传输 SRC目录下所有结尾为 .txt 的文件到 backups_include 目录
[root@localhost test]# rsync -av --include='*.txt' SRC/ backups_include/

sending incremental file list    ## 从传输列表看到结果不似如期只传输 SRC目录下所有结尾为 .txt 的文件。
                                 ## 而是把 SRC目录下所有的数据都传输到 backups_include 目录
./ 
file1.txt
file3.txt
file4.txt
directory/
directory/file2.txt
office_directory/
office_directory/docxfile.docx
office_directory/elsxfile.elsx
office_directory/pptxfile.pptx

sent 618 bytes  received 164 bytes  1,564.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include  ## backups_include 的内容就是 SRC 的内容
│   ├── directory
│   │   └── file2.txt
│   ├── file1.txt
│   ├── file3.txt
│   ├── file4.txt
│   └── office_directory
│       ├── docxfile.docx
│       ├── elsxfile.elsx
│       └── pptxfile.pptx
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

6 directories, 14 files
[root@localhost test]# 


  • 想要实现只传输 SRC 目录下的所有 *.txt 到 backups_include 目录,需要 --exclude 作过滤条件。沿用上面的例子:
## 现在的目录的结构,backups_include 的目录没有数据
[root@localhost test]# tree
.
├── backups_include
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files

## 沿用上面的命令加上  --exclude=' * ' 参数。
## 意思是指定传输 SRC目录下所有结尾为 .txt 的文件到 backups_include目录下。
## --exclude=' * ' 参数在这里的作用是除了 --include='*.txt' 指定所有 .txt 结尾的文件,其他都排除掉。
## 发送方:SRC/ ,接收方:backups_include/ ,
## SRC 目录下的所有 .txt 文件传输到 backups_include。

[root@localhost test]# rsync -av --include='*.txt' --exclude='*' SRC/ backups_include/

sending incremental file list    ## 从传输列表看出只传输了三个 .txt 结尾的文件,
                                 ## 这三个 .txt 文件就是 SRC 目录下的 .txt文件
./
file1.txt
file3.txt
file4.txt

sent 234 bytes  received 76 bytes  620.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include
│   ├── file1.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt    ##  {...
    ├── file3.txt    ##    SRC 目录下的 .txt 文件传输到 backups_include目录
    ├── file4.txt    ##  ...}
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 10 files
[root@localhost test]# 

  • 仔细观察 file1.txt、file3.txt 和 file4.txt 都是 SRC 目录下的一级目录文件。它们能够顺利通过 --include=' * .txt' 传输到 backups_include 目录。而,实际上 SRC 目录下还有一个 file2.txt。它在 directory 目录下,directory 目录是 SRC 的子目录,也就是 rsync -av --include=' * .txt' --exclude= '* ' SRC/ backups_include/ ,这条命令只能传输 SRC/ 下所有的 .txt 文件,子目录下的 .txt 文件没有传输的。我们需要做更多的测试去实现不同的传输效果。
    但,通过上面的实现,至少已证明了 --include 和 --exclude 搭档可以实现指定数据的传输。

3、传输 office_directory 目录及文件。

  • 方法一:根据条件进行传输( --include、--exclude 视为条件)
  • 分析思路:
  • 1、当前路径是 root 下的 test目录。首先,要了解 test 目录的结构才可以知道 office_directory 目录的路径。从目录结构可以看到 office_directory 目录在 SRC 目录下。office_directory 目录是 SRC 的子目录。
[root@localhost ~]# cd test
[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory    ## office_directory 目录是 SRC 的子目录
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files
[root@localhost test]# 

  • 2:梳理需要指定传输的内容。题意要求传输 office_directory 目录及文件。
  • 3:根据题意整理条件。传输 office_directory 目录:--include='office_directory/',传输 office_directory 目录下的文件 --include='office_directory/*'
  • 4:当传输完指定传输的内容后,其他内容不需要传输。需要用
    --exclude=' * ' 排除所有。这样就只会传输指定的内容,其他没有指定的内容不会传输。
  • 5:梳理源目录路径,也就是输出端。这里称为 ‘ 路径1 ’。当前操作的路径是 root 下的 test 目录。test 目录下包含 backups_include 和 SRC。SRC 目录下就是 office_directory 目录。我们需要输出的是 office_directory 目录及文件,所以源目录定位在上级目录 SRC/。
  • 6:目标目录,也就是接收端。这里称为 ‘ 路径2 ’。backups_include 目录。
  • 7:语句整合,根据以上分析得出语句结构就是:
    命令 -参数 --条件1 --条件2 -- 条件3 路径1 路径2
    rsync -av --include='office_directory/' --include='office_directory/*' --exclude=' * ' SRC/ backups_include/
  • 8:输出情况如下:
## --include='office_directory/':传输 office_directory/ 目录
## --include='office_directory/*':传输 office_directory/ 目录下的文件和目录
## --exclude='*':不传输所有文件
## SRC/:源路径
## backups_include/:目标路径

[root@localhost test]# rsync -av --include='office_directory/' --include='office_directory/*' --exclude='*' SRC/ backups_include/
sending incremental file list    ## 从传输列表看出只传输了 --include 指定的条件。
office_directory/                ## office_directory 目录本身
office_directory/docxfile.docx    ## office_directory 目录下的文件
office_directory/elsxfile.elsx
office_directory/pptxfile.pptx

sent 296 bytes  received 77 bytes  746.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include    ## office_directory 目录及文件传输到 backups_include
│   └── office_directory
│       ├── docxfile.docx
│       ├── elsxfile.elsx
│       └── pptxfile.pptx
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

5 directories, 10 files
[root@localhost test]# 

  • 9、命令图解:
  • 10、 rsync -av --include='office_directory/ ' --include='office_directory/ * ' --exclude=' * ' SRC/ backups_include/
    语句大意:除了传输 SRC 目录下的 office_directory 目录及文件到 backups_include 目录,其他不传输。

  • 11、注意事项:传输目录及文件,需要先 --include 指定传输目录,再 --include 指定传输目录下的文件。如:需要传输 office_directory 目录及文件,就需要先 --include='office_directory/' 指定传输目录,后传输目录下的所有文件 --include='office_directory/ * '。

  • 如果不传输目录,直接传输目录下的所有文件 --include='office_directory/*' ,是不会传输成功的。

## 直接传输目录下的所有文件 --include='office_directory/*'
[root@localhost test]# rsync -av --include='office_directory/*' --exclude='*' SRC/ backups_include/
sending incremental file list    ## 传输列表没有任何的文件
./

sent 43 bytes  received 19 bytes  124.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include    ## backups_include目录不似预期的接收到 office_directory的所有文件
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files
[root@localhost test]# 

  • 另外需要注意的是层级关系。传输的目录或文件,路径1 (源目录)定位为上一级目录。

  • 方法二:设置对应的路径配合条件进行传输
    已知需要传输某路径的所有数据,可以写成 --include=' * ',传输所有数据的意思,另一个排除参数也是写成 --exclude=' * ' ,也就是排除所有数据。后面用源路径来控制发送端的内容范围。
## --include=' * '  保留所有内容
## --exclude=' * '  删除所有内容
## 传输 SRC目录下的 office_directory目录及内容到 backups_include,SRC其他内容除外,不传输。
## 源目录(路径1)写成 具体的目录。也就是 SRC/office_directory。
## 这样写就会根据 源目录(路径1) 来执行传输的数据。

[root@localhost test]# rsync -av --include='*' --exclude='*' SRC/office_directory backups_include/
sending incremental file list    ## 从传输列表看出,只有传输了 office_directory 目录及内容
office_directory/
office_directory/docxfile.docx
office_directory/elsxfile.elsx
office_directory/pptxfile.pptx

sent 276 bytes  received 77 bytes  706.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# tree
.
├── backups_include    ## backups_include 目录只有 office_directory 目录及内容
│   └── office_directory
│       ├── docxfile.docx
│       ├── elsxfile.elsx
│       └── pptxfile.pptx
└── SRC    ## 源目录
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

5 directories, 10 files
[root@localhost test]# 

rsync -av --include='*' --exclude='*' SRC/office_directory backups_include/ 大意
  • rsync -av --include=' * ',--exclude=' * ' SRC/office_directory/ backups_include/
    当源目录 SRC/office_directory/ 加上 / 符号,只传输 SRC/office_directory/ 目录下的文件(不含 office_directory 目录本身)
[root@localhost test]# rsync -av --include='*',--exclude='*' SRC/office_directory/ backups_include/
sending incremental file list    ## 从传输列表看出只传输了 office_directory 的文件
./
docxfile.docx
elsxfile.elsx
pptxfile.pptx

sent 249 bytes  received 76 bytes  650.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include    ## backups_include 只有 office_directory 的文件
│   ├── docxfile.docx
│   ├── elsxfile.elsx
│   └── pptxfile.pptx
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory    ## office_directory 目录没有传输到 backups_include
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 10 files
[root@localhost test]# 

4、传输 SRC 目录下的所有 txt 到 backups_include 目录(含子目录及内容)。
分析思路:

  • 1、通过 tree 可以看到 directory 是 SRC 子级目录,它下面有 file2.txt 文件。--include 选项应该写为:--include='directory/' ,传输 directory/ 目录。--include='directory/*.txt',传输 directory/ 目录下所有 .txt 结尾的文件。

  • 2、SRC 目录下的子级还有三个 .txt 文件,分别是:file1.txt、file3.txt 和 file4.txt。--include 选项应该写为:--include='*.txt',传输所有的 .txt 结尾的文件。

[root@localhost test]# tree    ## 现时的目录结构
.
├── backups_include    ## backups_include 没有数据
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files
  • 3、SRC 内含的 .txt 文件的分布结构已经清楚。综合上述两点 --include 选项应该写为:--include='directory/' --include='directory/ * .txt' --include=' * .txt' 。

  • 4、需要传输的选项已经写了之后,其余的数据不需要传输。用 --exclude='*' 进行所有数据的排除。

  • 5、发送方,源目录是 SRC/。接收方,目标目录是 backups_include/。

  • 6、路径应该写为:SRC/ backups_include/。

  • 7、传输 SRC 目录下的所有 .txt 文件(含子目录及内容)到 backups_include 目录的整条语句应该是:rsync -av --include='directory/' --include='directory/ * .txt' --include=' * .txt' --exclude=' * ' SRC/ backups_include/。效果如下:

## 执行只传输 SRC 目录下的 .txt 结尾的文件到 backups_include 目录
[root@localhost test]# rsync -av --include='directory/' --include='directory/*.txt' --include='*.txt' --exclude='*' SRC/ backups_include/
sending incremental file list    ## 从传输列表看出只有 .txt 的文件 和 含有 .txt 文件的子目录
./
file1.txt
file3.txt
file4.txt
directory/
directory/file2.txt

sent 359 bytes  received 103 bytes  924.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ## 执行完语句的目录结构
.
├── backups_include     ## backups_include目录下只有 .txt 的文件 和 含有 .txt 文件的子目录及 .txt 文件
│   ├── directory
│   │   └── file2.txt
│   ├── file1.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

5 directories, 11 files
[root@localhost test]# 


5、如果只想传输 .txt 结尾的文件到 backups_include 目录,不想传输子目录文件夹。可用发送方,也就是源目录的路径进行控制。
分析思路:

  • 1、只传输 .txt 结尾的文件可以写为 --include='*.txt'。

  • 2、除了 .txt 文件,其余不传输可以写为 --exclude='*'。

  • 3、根据目录结构分析,SRC 目录下的子级有三个 .txt 文件,分别是:file1.txt、file3.txt 和 file4.txt。

[root@localhost test]# tree    ## 现时的目录结构
.
├── backups_include     ## backups_include 没有数据
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files
  • 4、发送方,源目录的路径写为 SRC/。SRC 目录下的子级目录 directory 下还有一个 file2.txt 文件。因为 file2.txt 是 SRC 目录下的子级目录 directory 下的文件,路径可以写为 SRC/directory/,意思也是指定 SRC/directory/ 这个路径下寻找 .txt 文件,如果有就传输。

  • 5、传输 SRC 目录下的所有 .txt 文件到 backups_include 目录的整条语句应该是:rsync -av --include=' * .txt' --exclude=' * ' SRC/ SRC/directory/ backups_include/

## 传输 SRC 目录下 .txt 结尾的文件到 backups_include 目录,不传输子目录文件夹。
[root@localhost test]# rsync -av --include='*.txt' --exclude='*' SRC/ SRC/directory/ backups_include/

sending incremental file list
./
file1.txt
file2.txt
file3.txt
file4.txt

sent 336 bytes  received 95 bytes  862.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ## 执行完语句的目录结构
.
├── backups_include     ## backups_include目录下只有 .txt 的文件,没有目录
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 11 files
[root@localhost test]# 

只想传输 .txt 结尾的文件到 backups_include 目录 图解

6、传输多个二级目录及文件。

  • 1、创建多级目录及文件测试用例。
[root@localhost test]# tree
.
├── backups_include
└── SRC
    ├── directory
    │   └── file2.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

4 directories, 7 files

## SRC 目录下创建 directory1、directory2 和 directory3 二级目录
[root@localhost test]# mkdir SRC/directory1 SRC/directory2 SRC/directory3    
[root@localhost test]# 

## SRC/directory 目录下创建 directory4 和 directory5  三级目录
[root@localhost test]# mkdir SRC/directory/directory4 SRC/directory/directory5
 
## 为 SRC 二级目录 directory1、directory2 和 directory3 目录分别创建 demo1.txt、demo2.txt 和 demo3.txt 文件。
[root@localhost test]# touch SRC/directory1/demo1.txt
[root@localhost test]# touch SRC/directory2/demo2.txt
[root@localhost test]# touch SRC/directory3/demo3.txt

## 为 SRC 三级目录 directory4 和 directory5目录分别创建 demo4.txt 和 demo5.txt 文件
[root@localhost test]# touch SRC/directory/directory4/demo4.txt
[root@localhost test]# touch SRC/directory/directory5/demo5.txt
[root@localhost test]# 
[root@localhost test]# tree    ## 添加完二级目录、文件 和 三级目录、文件的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4    ## 新添加内容
    │   │   └── demo4.txt
    │   ├── directory5     ## 新添加内容
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1     ## 新添加内容
    │   └── demo1.txt
    ├── directory2     ## 新添加内容
    │   └── demo2.txt
    ├── directory3     ## 新添加内容
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files
[root@localhost test]# 


  • 2、传输多个二级目录及 .txt 结尾 文件的原理和上面的 例5 原理一样。通过 --include 把目录和目录下 *.txt 文件类型标识清楚。以传输 SRC 目录下的二级目录及所有 .txt 文件为例:

  • 3、从目录结构可以看到 SRC 的二级目录有 directory、directory1、directory2、directory3 和 office_directory。其中,二级目录里直接有 .txt 文件的目录有 directory、directory1、directory2 和 directory3 。

[root@localhost test]# tree    文件的目录结构
.
├── backups_include
└── SRC
    ├── directory    ## 二级目录
    │   ├── directory4    
    │   │   └── demo4.txt
    │   ├── directory5     
    │   │   └── demo5.txt
    │   └── file2.txt    ## 二级目录 .txt 文件
    ├── directory1     ## 二级目录及 .txt 文件 
    │   └── demo1.txt
    ├── directory2    ## 二级目录及 .txt 文件 
    │   └── demo2.txt
    ├── directory3    ## 二级目录及 .txt 文件 
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx
9 directories, 12 files
  • 4、根据题意传输多个二级目录及 .txt 结尾文件,那么用 --include 把目录和目录下的 *.txt 文件类型标识清楚。
    如:--include='directory' --include='directory/ * .txt' (传输 directory 目录及目录下所有 .txt 文件)。
    --include='directory1' --include='directory1/ * .txt' (传输 directory1 目录及目录下所有 .txt 文件)。
    --include='directory2' --include='directory2/ * .txt' (传输 directory2 目录及目录下所有 .txt 文件)。
    --include='directory3' --include='directory3/ * .txt' (传输 directory3 目录及目录下所有 .txt 文件)。

  • 5、--exclude=' * ' (除了 --include 选项的内容,其余排除传输)

  • 6、发送方,源目录是 SRC/。接收方,目标目录是 backups_include/。

  • 7、传输多个二级目录及 .txt 结尾文件的整条语句应该是:
    rsync -av --include='directory' --include='directory/ * .txt' --include='directory1' --include='directory1/ * .txt' --include='directory2' --include='directory2/ * .txt' --include='directory3' --include='directory3/ * .txt' --exclude=' * ' SRC/ backups_include/
    效果如下:

## 传输多个 SRC 二级目录及 .txt 结尾的文件到 backups_include 目录的语句应该是:
[root@localhost test]# rsync -av --include='directory' --include='directory/*.txt' --include='directory1' --include='directory1/*.txt' --include='directory2' --include='directory2/*.txt' --include='directory3' --include='directory3/*.txt' --exclude='*' SRC/ backups_include/
sending incremental file list    ## 从传输列表中看出都是 二级目录及直属的 .txt 文件
./
directory/
directory/file2.txt
directory1/
directory1/demo1.txt
directory2/
directory2/demo2.txt
directory3/
directory3/demo3.txt

sent 427 bytes  received 115 bytes  1,084.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ## 执行完语句的目录结构 
.
├── backups_include    ## backups_include目录下只有二级目录和直属的 .txt 的文件
│   ├── directory
│   │   └── file2.txt
│   ├── directory1
│   │   └── demo1.txt
│   ├── directory2
│   │   └── demo2.txt
│   └── directory3
│       └── demo3.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

13 directories, 16 files
[root@localhost test]# 


7、传输三级目录及文件原理也是一样。通过 --include 把目录和目录下 *.txt 文件类型标识清楚。以传输 SRC 目录下的三级目录及所有 .txt 文件为例:

  • 1、从目录结构可以看出三级目录有 directory4 和 directory5,它们分别有 demo4.txt 和 demo5.txt 文件。
[root@localhost test]# tree
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4    ## 三级目录及 .txt 文件
    │   │   └── demo4.txt
    │   ├── directory5     ## 三级目录及 .txt 文件
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files
  • 2、用 --include 把目录和目录下的 *.txt 文件类型标识清楚。
    --include='directory' (传输二级目录 directory,传输完二级目录后再传输三级目录)。
    --include='directory4' --include='directory4/ * .txt' (传输 directory4 目录及目录下所有 .txt 文件)。
    --include='directory5' --include='directory5/ * .txt' (传输 directory5 目录及目录下所有 .txt 文件)。

  • 3、传输 SRC 三级目录及文件到 backups_include 目录的语句应该是:
    rsync -av --include='directory' --include='directory4' --include='directory4/ * .txt' --include='directory5' --include='directory5/ * .txt' --exclude=' * ' SRC/ backups_include/
    效果如下:

## 传输 SRC 三级目录及文件到 backups_include 目录
[root@localhost test]# rsync -av --include='directory' --include='directory4' --include='directory4/*.txt' --include='directory5' --include='directory5/*.txt' --exclude='*' SRC/ backups_include/
sending incremental file list    ## 从传输列表中看出都是 三级目录及直属的 .txt 文件
./
./
directory/
directory/directory4/
directory/directory4/demo4.txt
directory/directory5/
directory/directory5/demo5.txt

sent 284 bytes  received 73 bytes  714.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include     ## backups_include目录下只有三级目录和直属的 .txt 的文件
│   └── directory
│       ├── directory4
│       │   └── demo4.txt
│       └── directory5
│           └── demo5.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

12 directories, 14 files
[root@localhost test]# 

8、传输 SRC 目录下的所有 .txt 文件(不含目录)到 backups_include 目录。

  • 1、从目录结构看出 SRC 目录下有 file1.txt、file3.txt 和 file4.txt 三个文件。另外,也有 directory、directory1、directory2 、directory3 和 office_directory 目录。

  • directory 目录下有 file2.txt 文件、directory4 和 directory5 目录,directory4 目录有 demo4.txt, directory5 目录有 demo5.txt。

  • directory1 目录下有 demo1.txt。

  • directory2 目录下有 demo2.txt。

  • directory3 目录下有 demo3.txt。

  • office_directory 目录下没有 .txt 文件。

[root@localhost test]# tree    ## 目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files

  • 2、传输所有的 .txt 可以写为:--include=' * .txt',除了 .txt 其他不传输可以写为:--exclude=' * '。

  • 3、发送方 (源目录):含有 .txt 文件的目录路径。

  • --- SRC/

  • --- SRC/directory/

  • --- SRC/directory1/

  • --- SRC/directory2/

  • --- SRC/directory3/

  • ---SRC/directory/directory4/

  • ---SRC/directory/directory5/

  • 4、接收方 (目标目录):backups_include/ 目录

  • 5、结合以上分析,传输 SRC 目录下的所有 .txt 文件(不含目录)到 backups_include 目录的语句应该是:
    rsync -av --include=' * .txt' --exclude=' * ' SRC/ SRC/directory/ SRC/directory1/ SRC/directory2/ SRC/directory3/ SRC/directory/directory4/ SRC/directory/directory5/ backups_include/

## 传输 SRC 目录下的所有 .txt 文件(不含目录)到 backups_include 目录
[root@localhost test]#  rsync -av --include='*.txt' --exclude='*' SRC/ SRC/directory/ SRC/directory1/ SRC/directory2/ SRC/directory3/ SRC/directory/directory4/ SRC/directory/directory5/ backups_include/
sending incremental file list    ## 从传输列表看出只传输了 SRC 目录下所有的 .txt 文件
./
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo5.txt
file1.txt
file2.txt
file3.txt
file4.txt

sent 734 bytes  received 190 bytes  1,848.00 bytes/sec
total size is 0  speedup is 0.00

[root@localhost test]# tree    ##完成传输的目录结构
.
├── backups_include
│   ├── demo1.txt
│   ├── demo2.txt
│   ├── demo3.txt
│   ├── demo4.txt
│   ├── demo5.txt
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 21 files
[root@localhost test]# 

rsync -av --include='*.txt' --exclude='*' SRC/ SRC/directory/ SRC/directory1/ SRC/directory2/ SRC/directory3/ SRC/directory/directory4/ SRC/directory/directory5/ backups_include/ 图解

9、优化 例 8 语句

  • 通过 例 8 传输一个多层目录下的所有 .txt 文件中,不难发现主要是靠源目录的路径进行控制。理解了原理后,会发现这样写的语句会有些问题。
    1)、首先是对目录结构要有一定了解,清晰知道层级关系。这样就会使每次操作都要先了解目录、文件的名称和层级关系,耗费一定的时间精力。
    2)、精确到目录路径的写法,在很多目录层级结构的时候就会显得语句臃肿(很长)。
    3)、遇到很多目录层级结构的时候,容易写错,写漏。

  • 通过 例8 可以理解一下语句操作,实际上可以对语句做一些有优化。以例 8 的
    rsync -av --include=' * .txt' --exclude=' * ' SRC/ SRC/directory/ SRC/directory1/ SRC/directory2/ SRC/directory3/ SRC/directory/directory4/ SRC/directory/directory5/ backups_include/ 语句为例。

  • 通过观察目录结构发现,SRC 目录最大就是三级目录,也就是三层。源的路径可以用 * 号来代替一个层级。不用精确到第二级目录的名字、第三级目录的名字。
    例如:SRC 目录第二级目录有 directory、directory1、directory2 和 directory3。可以写成 SRC/ * /。这里的 * 就代表了第二级所有的目录,也包含了 directory、directory1、directory2 和 directory3。

  • 所以,优化后的语句应该是:rsync -av --include=' * .txt' --exclude=' * ' SRC/ SRC/ * / SRC/ * / * / backups_include/


    rsync -av --include=' * .txt' --exclude=' * ' SRC/ SRC/ * / SRC/ * / * / backups_include/ 图解
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_include
└── SRC                                         ## 第一级 SRC/
    ├── directory                                   ##  第二级 SRC/*/
    │   ├── directory4                                  ##  第三级 SRC/*/*/
    │   │   └── demo4.txt
    │   ├── directory5                                  ##  第三级 SRC/*/*/
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1                                  ##  第二级 SRC/*/
    │   └── demo1.txt
    ├── directory2                                  ##  第二级 SRC/*/
    │   └── demo2.txt
    ├── directory3                                  ##  第二级 SRC/*/  
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory                            ##  第二级 SRC/*/
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files
  • 执行语句的效果
[root@localhost test]# 
[root@localhost test]# rsync -av --include='*.txt' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ## 从传输列表看出只传输了 SRC 目录下所有的 .txt 文件
./
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo5.txt
file1.txt
file2.txt
file3.txt
file4.txt

sent 750 bytes  received 190 bytes  1,880.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# tree    ##完成传输的目录结构
.
├── backups_include
│   ├── demo1.txt
│   ├── demo2.txt
│   ├── demo3.txt
│   ├── demo4.txt
│   ├── demo5.txt
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 21 files
[root@localhost test]# 

  • 通过 * 代表一级的形式可以避免了每个路径都写一次目录而造成的语句臃肿,还可以避免了目录的遗漏和书写错误。只要了解目录结构有多少层就可以了。

10、指定传输某些文件。

  • 通过 --include='文件名' 指定需要传输的文件。源路径方面,同样采用 * 代替层级的方法。
    例:传输 SRC 目录下的 file1.txt、demo1.txt、demo4.txt 和 pptxfile.pptx 文件到 backups_include 目录。
[root@localhost test]# tree    ## 现时的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files

## --include='file1.txt' --include='demo1.txt' --include='demo4.txt' --include='pptxfile.pptx'
## 指定传输 file1.txt、demo1.txt、demo4.txt 和 pptxfile.pptx 文件
## --exclude='*'  (除了 --include 指定的文件,其他不传输)
## SRC/ SRC/*/ SRC/*/*/ (从 SRC 目录、SRC 二级目录 和 SRC 三级目录 筛查指定传输的文件,有就传输。)
## backups_include/ (目标目录)

[root@localhost test]# rsync -av --include='file1.txt' --include='demo1.txt' --include='demo4.txt' --include='pptxfile.pptx' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表中发现,传输的文件都是 --include 指定的文件
./
demo1.txt
demo4.txt
file1.txt
pptxfile.pptx

sent 434 bytes  received 95 bytes  1,058.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##数据传输完的目录结构
.
├── backups_include    ## --include 指定传输的文件
│   ├── demo1.txt
│   ├── demo4.txt
│   ├── file1.txt
│   └── pptxfile.pptx
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 16 files
[root@localhost test]# 

11、指定传输某字母或单词开头的目录或文件。

  • 例:指定传输 SRC 目录下 file 开头的目录或文件。通过 --include='file * ' 指定传输 file 开头的目录或文件。源路径方面,同样采用 * 代替层级的方法。
[root@localhost test]# tree     ## 现时的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files

## --include='file*' 指定传输 file开头的目录或文件
## --exclude='*'  (除了 file 开头的目录或文件,其他不传输)
## SRC/ SRC/*/ SRC/*/*/ (从 SRC 目录、SRC 二级目录 和 SRC 三级目录 筛查指定传输的文件,有就传输。)
## backups_include/ (目标目录)

[root@localhost test]# rsync -av --include='file*' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表可以看出,传输的文件都是 file开头的文件
./
file1.txt
file2.txt
file3.txt
file4.txt

sent 418 bytes  received 95 bytes  1,026.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##数据传输完的目录结构
.
├── backups_include    ## --include 指定传输的文件
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 16 files
[root@localhost test]# 

12、指定传输含有某字母或单词的文件。

  • 例:指定传输 SRC 目录下含有 s 的目录或文件。通过 --include=' * s * ' 指定传输含有 s 的所有目录或文件。源路径方面,同样采用 * 代替层级的方法。
[root@localhost test]# tree    ## 现时的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files

## --include='*s*' 指定传输含有 s 的目录或文件
## --exclude='*'  (除了含有 s 的目录或文件,其他不传输)
## SRC/ SRC/*/ SRC/*/*/ (从 SRC 目录、SRC 二级目录 和 SRC 三级目录 筛查指定传输的文件,有就传输。)
## backups_include/ (目标目录)

[root@localhost test]# rsync -av --include='*s*' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表可以看到含有 s 的文件
elsxfile.elsx

sent 228 bytes  received 35 bytes  526.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##数据传输完的目录结构
.
├── backups_include    ## 含有 s 的文件
│   └── elsxfile.elsx
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 13 files
[root@localhost test]# 

  • 如果把 s 改为 c 会有很多目录和文件传输,因为很多目录和文件都含有 c。
[root@localhost test]# rsync -av --include='*c*' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ## 含有 c 的目录和文件都在传输列表
./
docxfile.docx
directory/
directory/directory4/
directory/directory5/
directory1/
directory2/
directory3/
directory4/
directory5/
office_directory/
office_directory/docxfile.docx

sent 600 bytes  received 97 bytes  1,394.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##传输完成的目录结构
.
├── backups_include    ## 含有 c 的目录和文件
│   ├── directory
│   │   ├── directory4
│   │   └── directory5
│   ├── directory1
│   ├── directory2
│   ├── directory3
│   ├── directory4
│   ├── directory5
│   ├── docxfile.docx
│   └── office_directory
│       └── docxfile.docx
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

18 directories, 14 files


13、指定传输某字母或单词结尾的文件。

  • 例:指定传输 SRC 目录下 x 结尾的目录或文件。通过 --include=' * x' 指定传输 x 结尾的所有目录或文件。源路径方面,同样采用 * 代替层级的方法。
[root@localhost test]# tree    ## 现时的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files

## --include='*x' 指定传输 x 结尾的目录或文件
## --exclude='*'  (除了 x 结尾的目录或文件,其他不传输)
## SRC/ SRC/*/ SRC/*/*/ (从 SRC 目录、SRC 二级目录 和 SRC 三级目录 筛查指定传输的文件,有就传输。)
## backups_include/ (目标目录)

[root@localhost test]# rsync -av --include='*x' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表看出传输的文件都是 x 结尾
./
docxfile.docx
elsxfile.elsx
pptxfile.pptx

sent 363 bytes  received 76 bytes  878.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##传输完成的目录结构
.
├── backups_include    ## x 结尾的目录和文件
│   ├── docxfile.docx
│   ├── elsxfile.elsx
│   └── pptxfile.pptx
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 15 files
[root@localhost test]# 


14、指定多个传输条件可以用 { } 大扩号把条件括起来,条件与条件之间用 ,逗号分隔开。只用一个 --include参数。可进步一步优化语句,避免臃肿。

  • 例:指定传输 SRC 目录下 demo4.txt、demo1.txt、file1.txt 和 elsxfile.elsx 文件。通过 --include={'demo4.txt','demo1.txt','file1.txt','elsxfile.elsx'} 把内容括起来。源路径方面,同样采用 * 代替层级的方法。
[root@localhost test]# tree    ## 没有传输前的目录结构
.
├── backups_include
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 12 files
## 用 { } 大括号的方法指定传输 SRC 目录下 demo4.txt、demo1.txt、file1.txt 和 elsxfile.elsx 文件。
[root@localhost test]# rsync -av --include={'demo4.txt','demo1.txt','file1.txt','elsxfile.elsx'} --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表看到 { } 大括号指定的文件 
./
demo1.txt
demo4.txt
elsxfile.elsx
file1.txt

sent 437 bytes  received 95 bytes  1,064.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ##传输完成的目录结构
.
├── backups_include    ## backups_include 接收 { } 大括号的文件
│   ├── demo1.txt
│   ├── demo4.txt
│   ├── elsxfile.elsx
│   └── file1.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 16 files
[root@localhost test]# 

  • { } 大括号括起指定的传输内容适用于多种形式条件筛查。把原来每个条件配一个 include,改为一个 include,内容用 { } 括起来,简化了语句。

15、--include-from 通过加载文件的形式读取文件记录的参数,从而实现传输指定元素的功能。
例:用 --include-from 方式传输 SRC/ 目录下的所有 .txt 文件

## 首先,需要新建一个文件记录传输的参数。
[root@localhost test]# vim include-file.txt      ## vim 编辑并生成 include-file.txt 文件


-------------- 输入的文本内容 -------------
*.txt             ## 按 i 进入编辑模式,输入*.txt  (所有 .txt 结尾的文件)
~                                                                                                      
~                                                                                                      
~                                                                                                                                                                                                      
:wq!
------ 输入完内容按 Esc 键退出编辑状态,输入 :wq! 强制保存退出 -------

[root@localhost test]# 
[root@localhost test]# tree      ##没有传输时的目录结构
.
├── backups_include
├── include-file.txt      ## 生成的 include-file.txt 文件
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 13 files
[root@localhost test]# 

## --include-from='include-file.txt' 加载 include-file.txt,
## 读取其内容,它的内容是 *.txt。意思是所有 .txt 结尾的文件,
## 效果等同于 --include='*.txt'。

[root@localhost test]# rsync -av --include-from='include-file.txt' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表看出传输的内容都是 .txt 文件
./
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo5.txt
file1.txt
file2.txt
file3.txt
file4.txt

sent 750 bytes  received 190 bytes  1,880.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree    ## 传输完成后的目录结构
.
├── backups_include    ## backups_include 下都是 .txt 文件
│   ├── demo1.txt
│   ├── demo2.txt
│   ├── demo3.txt
│   ├── demo4.txt
│   ├── demo5.txt
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   └── file4.txt
├── include-file.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 22 files
[root@localhost test]# 
                                  

16、加载文件多项传输元素的方法、原理和添加单项元素一样。只是,每个元素为一行。
例:用 --include-from 方式传输 SRC/ 目录下的所以 e开头、文件名含 1 的字符、文件名含 3 的字符 和 tx结尾的目录和文件。

## 首先,需要新建一个文件记录传输的参数。
[root@localhost test]# vim include-file.txt        ## vim 编辑并生成 include-file.txt 文件

-------------- 输入的文本内容 -------------
e*          ## 按 i 进入编辑模式,输入e* (e开头的目录和文件)
*1*         ## 输入*1*  (含 1 的目录和文件)
*3*         ## 输入*3*  (含 3 的目录和文件)
*tx          ## 输入*tx  (tx结尾的目录和文件)
~                                                                                                      
~                                                                                                      
~                                                                                                                                                                                                        
:wq!   
------ 输入完内容按 Esc 键退出编辑状态,输入 :wq! 强制保存退出 -------

[root@localhost test]# tree    ##没有传输时的目录结构
.
├── backups_include
├── include-file.txt    ## 生成的 include-file.txt 文件
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

9 directories, 13 files

## --include-from='include-file.txt' 加载 include-file.txt,
## 读取其内容,它的内容是 e*、*1*、*3*、*tx。,
## 意思是所有 e 开头的文件、文件名含 1 的字符、文件名含 3 的字符 和 tx结尾的文件,
## 效果等同于--exclude={'e*','*1*','*3*','*tx'}

[root@localhost test]# rsync -av --include-from='include-file.txt' --exclude='*' SRC/ SRC/*/ SRC/*/*/ backups_include/
sending incremental file list    ##从传输列表看出传输的内容都是 e 开头、文件名含 1 或 3 的字符 和 tx结尾的文件
./
demo1.txt
demo3.txt
elsxfile.elsx
file1.txt
file3.txt
pptxfile.pptx
directory1/
directory1/demo1.txt
directory3/
directory3/demo3.txt

sent 772 bytes  received 183 bytes  1,910.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost test]# 
[root@localhost test]# tree      ## 传输完成后的目录结构
.
├── backups_include    ## backups_include 下都是 都是 e 开头、文件名含 1 或 3 的字符 和 tx结尾的文件,
│   ├── demo1.txt
│   ├── demo3.txt
│   ├── directory1
│   │   └── demo1.txt
│   ├── directory3
│   │   └── demo3.txt
│   ├── elsxfile.elsx
│   ├── file1.txt
│   ├── file3.txt
│   └── pptxfile.pptx
├── include-file.txt
└── SRC
    ├── directory
    │   ├── directory4
    │   │   └── demo4.txt
    │   ├── directory5
    │   │   └── demo5.txt
    │   └── file2.txt
    ├── directory1
    │   └── demo1.txt
    ├── directory2
    │   └── demo2.txt
    ├── directory3
    │   └── demo3.txt
    ├── file1.txt
    ├── file3.txt
    ├── file4.txt
    └── office_directory
        ├── docxfile.docx
        ├── elsxfile.elsx
        └── pptxfile.pptx

11 directories, 21 files
[root@localhost test]# 
                                                    
  • 使用 --include-from 方式可以把参数写进文件,需要修改参数时在文件修改。随时可以调动文件进行指令操作,不用每次都 --include指定参数。增加效率。另外,也可以进一步简化语句避免臃肿。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容