首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rsync:如何只同步特定目录中的某些文件类型?

Rsync:如何只同步特定目录中的某些文件类型?
EN

Unix & Linux用户
提问于 2017-06-02 17:55:15
回答 2查看 4.3K关注 0票数 2

我希望将所有图像类型文件从几个目录复制到/images。我希望将所有视频类型文件从相同的目录复制到/videos。我想排除所有的点目录和点文件(隐藏文件和目录)。

目录结构:

代码语言:javascript
复制
DCIM (GOOD)
DCIM/.thumbnails (BAD)
DCIM/AccessoryCamera (GOOD)
.cloudagent (BAD)
Pictures (GOOD)
Downloads (GOOD)

我正在努力:

代码语言:javascript
复制
rsync -ravtz --progress --prune-empty-dirs --include "DCIM/" --include "Download/*" --include "Picture/" --include "*.jpg" --include "*.png" --include "*.gif" --include "*.nef" --exclude '"*"' --exclude '".*"' --exclude '"*/"' --exclude '".*/"' --exclude '"DCIM/.thumbnails/*' --log-file=/mm/rsync.log /mnt/S8/sdcard/ '/mm/images'

rsync -ravtz --progress --prune-empty-dirs --include "DCIM/" --include "Download/*" --include "Picture/" --include "*.mov" --include "*.mpeg" --include "*.mpg" --include "*.mp4" --exclude '"*"' --exclude '".*"' --exclude '"*/"' --exclude '".*/"' --exclude '"DCIM/.thumbnails/*' --log-file=/mm/rsync.log /mnt/S8/sdcard/ '/mm/videos'
EN

回答 2

Unix & Linux用户

发布于 2017-06-04 00:30:13

有关如何构建rsync筛选器的更多解释,请参见Rsync过滤器:只复制一个模式。总的想法是包括您需要的内容(包括任何导致您需要的目录),并在最后排除其余的目录。

代码语言:javascript
复制
rsync -az --progress --prune-empty-dirs \
    --exclude '.*' \
    --include "/DCIM" --include "/Download" --include "/Picture" --exclude '/*' \
    --include "*.jpg" --include "*.png" --include "*.gif" --include "*.nef" \
    --include '*/' --exclude '*' \
    --log-file=/mm/rsync.log /mnt/S8/sdcard/ '/mm/images'
  1. 排除所有点文件。
  2. 包含一些目录,并在顶层排除所有其他内容。
  3. 包括您想要的文件类型。
  4. 包括所有目录(顶层已经排除的目录除外),并排除所有其他目录。
票数 4
EN

Unix & Linux用户

发布于 2017-06-02 20:02:38

(1)创建一个文本文件,其中包含要从设备同步的目录:

./input_dirs

./input_dirs的内容:

./DCIM ./Pictures ./DCIM/AccessoryCamera ./Downloads

使用--files-from=./input_dirs.txt参数将相关目录加载到RSYNC中。

(2)利用--filter='dir-merge ./filter_file'参数加载所需的扩展过滤器。(如:*.png *.gif *.jpg)

./filter_file的内容:

+ *.png + *.jpg + *.gif - /*

(3)我只是在服务器上作为一个测试来运行这个程序,它起了作用。请注意,应该复制的文件只有.png__、.gif、*.jpg:

结果:

[root@localhost ~]# ls -Fal total 1012 dr-xr-x---. 4 root root 4096 Jun 5 20:03 ./ dr-xr-xr-x. 26 root root 4096 May 30 15:16 ../ -rw-------. 1 root root 1219 May 30 15:04 anaconda-ks.cfg -rw-------. 1 root root 7161 Jun 5 19:45 .bash_history -rw-r--r--. 1 root root 18 Apr 29 2010 .bash_logout -rw-r--r--. 1 root root 176 Apr 29 2010 .bash_profile -rw-r--r--. 1 root root 176 Apr 29 2010 .bashrc -rw-r--r--. 1 root root 100 Apr 29 2010 .cshrc -rwxr--r--. 1 root root 9565 May 30 20:41 cve.sh* drwxr-xr-x. 2 root root 4096 Jun 5 20:03 dest/ -rw-r--r--. 1 root root 14704 Dec 27 12:40 epel-release-7-9.noarch.rpm -rw-r--r--. 1 root root 508 May 30 22:08 file1 -rw-r--r--. 1 root root 508 May 30 22:08 file2 -rw-r--r--. 1 root root 29 Jun 5 20:03 filter_file -rw-r--r--. 1 root root 3 Jun 5 19:46 input_dirs -rw-r--r--. 1 root root 28978 May 30 15:04 install.log -rw-r--r--. 1 root root 7572 May 30 15:01 install.log.syslog -rw-------. 1 root root 88 Jun 4 17:49 .lesshst drwxr-----. 3 root root 4096 Jun 4 17:45 .pki/ -rw-r--r--. 1 root root 624068 May 24 04:34 samba-4.4.4-14.el7_3.x86_64.rpm -rw-r--r--. 1 root root 266168 May 24 04:34 samba-libs-4.4.4-14.el7_3.x86_64.rpm -rw-r--r--. 1 root root 129 Apr 29 2010 .tcshrc -rw-r--r--. 1 root root 0 Jun 5 19:55 test.gif -rw-r--r--. 1 root root 0 Jun 5 19:55 test.jpg -rw-r--r--. 1 root root 0 Jun 5 19:54 test.png

[root@localhost ~]# rsync -av --dry-run --files-from=./input_dirs --filter='dir-merge ./filter_file' ./ ./dest building file list ... done ./ test.gif test.jpg test.png sent 92 bytes received 24 bytes 232.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN)

删除-试运行以实际复制文件。

票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/368857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档