rsync是Unix下的一款应用软件,它能同步更新两处计算机的文件与目录,并适当利用差分编码以减少数据传输量。
我主要用rsync命令来同步两个目录文件,利用灵活的--exclude
和--include
参数可以从一个目录中单独同步某一类文件,并且支持匹配多种模式。下面主要记录一下如何方便的匹配多种模式。
exclude一种matching pattern
主要以同步服务器和本地目录为例,username@xxx.xxx.xxx.xxx
换成服务器的账户和IP地址,将服务器上/home/username/your_dirpath/
目录中的所有文件(排除所有*.pth文件)复制到本地/home/your_localdir/
下
rsync -avzP --exclude *.pth \
username@xxx.xxx.xxx.xxx:/home/username/your_dirpath/ \
/home/your_localdir/
exclude 多种matching pattern
匹配多种模式写法并不是*.pth | *.pkl
, 有效的写法如下:
- 第一种写法
rsync -avzP --exclude *.pth --exclude *.pkl \
username@xxx.xxx.xxx.xxx:/home/username/your_dirpath/ \
/home/your_localdir/
- 第二种写法
rsync -avzP --exclude={*.pth,*.pkl} \
username@xxx.xxx.xxx.xxx:/home/username/your_dirpath/ \
/home/your_localdir/
- 第三种写法
rsync -avzP --exclude-from exclude_file.txt \
username@xxx.xxx.xxx.xxx:/home/username/your_dirpath/ \
/home/your_localdir/
exclude_file.txt文件中可以写想要排除的文件匹配模式,例如:
*.pth
*.pkl