I am trying to find the optimum way to back up dotfiles, and I have devised a way to do it. I am using git with rsync to back up all the dotfiles. The rsync script looks like this right now:

rsync -avhP $HOME/.??* --exclude-from=$HOME/.dotBackup/exclusions.list $HOME/.dotBackup

However, I do not think that is the optimal way, and instead of excluding files using exclude-from arg, I want to do it the other way round, and have a list of specific directories and files I want to back up. And this is where things get a little murky. For example, I might want to back up some files in ~ and the directory of ~ looks like this:

|- Downloads (do not back up)
|- .bashrc (back up)
|- .config
     |- nvim (back up including subdirectories)
     |- code
          |- extensions (do not back up)
          |- preferences.json (back up)

How can I do such a thing using rsync?


2 Answers 2


Here is an example using an rsync filter file, use it with option --filter='dir-merge /.rsync-filter' or simply -F.

Put the file .rsync-filter into your rsync source directory (your $HOME) with the following content:

# include .bashrc
+ /.bashrc

# include .config dir (parent of nvim and code)
+ /.config/

# exclude (overrides nvim include below)
- /.config/nvim/but_not_this_dir/

# include nvim dir and everything in this directory
+ /.config/nvim/***

# include code dir and preferences.json
+ /.config/code/
+ /.config/code/preferences.json

# exclude everything in the source dir
- *

Since we exclude everything in the source directory with - *, you need to add all the directories you want to include manually including their parent dirs up to the source dir. To include foo/bar/, but not foo/abc/, you need to add + /foo/ to override - * and + /foo/bar/. Overrides to these includes need to be added before those includes.

Test your command using the dry-run option -n to see the output, add -vv to see which files/directories are shown/hidden by the filters.

Your modified command would be:

rsync -avFhp ~/ ~/.dotBackup

It makes sense to use a filter like this if you need a lot of overrides (sync this and that dir, but exclude this/foo and that/bar/baz). Use the --files-from option in roaima's answer for simpler use cases.

  • Roaima's answer was simpler, but this one feels a lot more comprehensive, which is why it's chosen as the answer. Jan 31, 2023 at 19:08

I might want to back up files in ~/.config/ABC/ except ~/.config/ABC/ex How can I do such a thing using rsync?

  • Include the files and directories that you want
  • Exclude the files and directories that you don't want

So, assuming the command is run from $HOME, we have this:

rsync --dry-run -avP --exclude '/ex' .config/ABC/ ABCBackup

Or if you are talking about modifying your existing command that copies all dot files and directories underneath $HOME:

rsync --dry-run -avP --exclude '.config/ABC/ex/' --exclude '.dotbackup/' .??* .dotBackup

Working from the statement, "I want to […] have a list of specific directories and files I want to back up", this is much much easier.

Put the list of files into a file such as $HOME/.to_backup. Where you have complete directories to be included add those too - with a trailing slash. All paths can be relative to $HOME. Using your example tree, the file would include these entries:


And then use this command (remove --dry-run once it's working as expected):

cd    # Start from $HOME so relative paths in the source file work correctly
rsync --dry-run --files-from .to_backup -arvP . .dotBackup/
  • I have edited my question to make it a bit more representative of my use case. Hope it helps. Jan 31, 2023 at 16:22
  • One more thing: What if I change my example and want to back up the directory .config/code except .config/code/extensions? Feb 1, 2023 at 2:44
  • @NibaronChakraborty in that scenario I'd go for a filter file Feb 1, 2023 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .