Score:2

Using wildcards in "sources" for `rsync` command

cn flag

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

Artur Meinild avatar
vn flag
Did you try `--delete-after` instead of just `--delete`?
FedKad avatar
cn flag
@ArturMeinild The end result does not change.
pLumo avatar
in flag
I guess you should use a different structure where you have everything in one parent only, `~/bin/1` `~/bin/2` ...
Artur Meinild avatar
vn flag
You need to consider pLumo's advice, unless you want to find a way to get the directory listing on the destination host to include as additional sources in your command.
Score:5
in flag

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.

FedKad avatar
cn flag
Thanks. In my case, the `--include` option should be like this: `--include="/home/my_user/bin*"` (with no trailing slash).
pLumo avatar
in flag
ah yes, because you also want files starting with `bin`, fixed
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.