2

How to exclude all files except specific files with rsync?

I'm trying to only backup specific files with rsync, to exclude all files , except some specific files, namely the .md files, and solutions from all these pages didn't work for me:

$ rsync -vua --exclude="*" --include="*.md" ../log ./
sending incremental file list

sent 19 bytes  received 12 bytes  62.00 bytes/sec
total size is 0  speedup is 0.00

$ rsync -vua --include="*.md" --exclude="*" ../log ./
sending incremental file list

sent 19 bytes  received 12 bytes  62.00 bytes/sec
total size is 0  speedup is 0.00

$ du -sh ../log
1016M   ../log

$ apt-cache policy rsync
rsync:
  Installed: 3.2.7-0ubuntu0.22.04.2
  Candidate: 3.2.7-0ubuntu0.22.04.2
  Version table:
 *** 3.2.7-0ubuntu0.22.04.2 500
        500 http://ca-toronto-1-ad-1.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
        100 /var/lib/dpkg/status
     3.2.3-8ubuntu3 500
        500 http://ca-toronto-1-ad-1.clouds.archive.ubuntu.com/ubuntu jammy/main amd64 Packages

2 Answers 2

4

I assume you want to copy the files from anywhere below ../log.

If a name matches an --include or --exclude pattern, the remaining patterns on the command line will not be used ("first match wins"). You will never recurse if you don't match */ with an --include pattern.

Taking these things into consideration, match *.md and */ with --include before matching * with --exclude:

rsync --archive --update --verbose --include='*.md' --include='*/' --exclude='*' ../log .

Add -m or --prune-empty-dirs if you wish to avoid creating empty directories.

0
3

Options apply from left to right, so you need to include items before you exclude the rest

rsync -av --include '*.md' --include '*/' --exclude '*' --prune-empty-dirs src/ dst/

Here, we include the files you want, include subdirectories (so we can descend into them looking for matches), but exclude everything else. Directories that would end up being empty are skipped too.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.