How to include files that are excluded in an rsync

20,668

The first matching rule applies, so include .htaccess before excluding .*.

rsync -avP --include=".htaccess" --exclude=".*" . user@server:/dir

This copies .htaccess at every level. I don't know what you intended with ./.htaccess; if you want to match a file at the root of the copy only, start the pattern with a /. If you only want the root .htaccess, you can't just use --include='/.htaccess' --exclude='.*', because the non-rooted rule actually takes precedence here, you need to do something more complicated:

rsync -avP --exclude='/*/**/.htaccess' --include='.htaccess' --exclude=".*" . user@server:/dir

Further reading: basic principles for rsync filters.

Share:
20,668
Rich
Author by

Rich

I write scientific software to support environmental and natural sciences. Mostly Python scripts that use a lot of parallel processing.

Updated on September 18, 2022

Comments

  • Rich
    Rich over 1 year

    I'm trying to set up an rsync that excludes all .* files except for .htaccess but unfortunately, this doesn't work:

    rsync -avP --exclude=".*" --include="./.htaccess" ./. user@server:/dir
    

    Is it possible to somehow exclude general exclude rules?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    @fred.bear: That one was a genuine typo, and a nasty one. Thanks for spotting it!
  • Rich
    Rich about 13 years
    Thanks, I had tried putting the --include first, but the "./" in front of the "./.htaccess" was what was killing it.
  • Tobias Kienzler
    Tobias Kienzler over 11 years
    @Rich note that if there are hidden directories that contain a .htaccess file, you'll have to --include='.*/' (I think) before the final --exclude, see also here
  • haridsv
    haridsv about 3 years
    Any idea why rsync -avP --exclude=/.htaccess src/dir/ dst/dir/ works as expected (excludes .htaccess only at the root directory), but rsync -avP --exclude=/.htaccess src/dir dst doesn't (it fails to exclude even at the root)?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 3 years
    @haridsv In the first case, the root is src/dir and you're copying the root, so src/dir/.htaccess is excluded. In the second case, the root is src and you're copying dir, so src/.htaccess is excluded.
  • haridsv
    haridsv about 3 years
    You are right! This works: rsync -avP --exclude=/dir/.htaccess src/dir dst