简体   繁体   中英

how to transfer files that match a pattern with rsync

Good morning, I want to transfer specific files in a directory to a remote machine while keeping the architecture of the subdirectories. Moreover, I only want to transfer files that have a peculiar extension (eg ".txt"). I have tried the following:

rsync -aP --include *.txt ./sourceDirectory user@hostIP:destDirectory

but it copies to the destination ( destDirectory ) all the files, and not only those which match the .txt pattern. Could anybody help me with such riddle?

PS: obviously, the directory sourceDirectory contains subdirectories where are located my .txt files.

I used to have the same problem when rsync videos to home NAS. If you read the manual of rsync, the --include flag really means "Do not exclude". It actually has to work together with a --exclude flag.

The follow command will do your job:

rsync -aP --include="*/" --include="*.txt" --exclude="*" sourceDirectory destDirectory

The command literally means exclude everything, except for subfolders and txt file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM