Search code examples
bashrsyncquotes

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 (e.g. ".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?

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


Solution

  • 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.