Using wildcards in "sources" for `rsync` command

I am trying to backup some files on another drive using rsync.

As a test case, I am trying the following command:

rsync -Rav --delete --stats /home/my_user/bin* "$dest"

This command will copy files and directories (with their contents) present in my $HOME directory and having names like ~/bin, ~/binb, ~/bin1, ~/bin2, etc. to the destination.

However, when I delete any of the files or directories called ~/bin1 or ~/bin2 for example, these are not deleted in the destination too. The only thing that works is when there is a file in a directory say ~/bin2/file1 and this file is deleted, then this file is also deleted in the destination (but not in the case when the directory ~/bin2/ is removed altogether).

Obviously this problem is created by the shell expanding /home/my_user/bin* to all the file and directory names starting with bin in my $HOME directory at the time the rsync command is executed. So, I tried to quote the source like "/home/my_user/bin*", but in this case I get the error:

rsync: [sender] link_stat "/home/my_user/bin*" failed: No such file or directory (2)

What options are needed, so, I can use wildcards for the source(s) and I will have the exact replica of the source file and directory structure, also in the destination?

Note: rsync version 3.2.3 protocol version 31

4

1 Answer

You can use --include / --exclude like:

rsync -Rav --delete \ --include="/home/my_user/bin*" \ --exclude="/home/my_user/*" \
/home/my_user/ "$dest"

But I think it would be easier to have a different structure where you can sync only one parent directory.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like