1

I want to do the following with rsync.

Directory structure example

  • home/netadmin/docker-data
  • home/netadmin/docker-data/uptime-kuma/
  • home/netadmin/docker-data/uptime-kuma/error.log

I want to rsync the docker-data directory but want to exclude the home/netadmin/docker-data/uptime-kuma/ directory but want to include home/netadmin/docker-data/uptime-kuma/error.log.

I'm using the following command :

rsync -av --delete --include-from='/home/netadmin/include.txt' --exclude-from='/home/netadmin/exclude.txt' /home/netadmin/docker-data /home/netadmin/temp/

The directory is being excluded but the file is not copied.

My question is, is it possible?

This is how my include and exclude files look like :

exclude.txt

uptime-kuma/

include.txt

+ error.log
1
  • wouldn't you need uptime-kuma/error.log? Since it's looking relative to the directory the transfer is in (/home/netadmin/docker-data) Commented Feb 27, 2024 at 19:09

2 Answers 2

1

The "trick" is to not exclude the uptime-kuma directory, but its contents.

With an include pattern of error.log and an exclude pattern of uptime-kuma/*, you would include the file uptime-kuma/error.log before excluding everything else in that subdirectory.

With uptime-kuma/ instead of uptime-kuma/*, you would exclude the directory, and rsync would never look inside it.

rsync -av --delete \
    --include=error.log \
    --exclude='uptime-kume/*' \
    ~netadmin/docker-data ~netadmin/temp/

I hope this is an equivalent test:

$ tree
.
`-- topdir
    |-- file-1
    |-- file-2
    |-- file-3
    |-- file-4
    |-- file-5
    `-- subdir
        |-- file-a
        |-- file-b
        |-- file-c
        |-- file-d
        |-- file-e
        `-- myfile

2 directories, 11 files
$ rsync -av --include=myfile --exclude='subdir/*' topdir/ copydir
sending incremental file list
created directory copydir
./
file-1
file-2
file-3
file-4
file-5
subdir/
subdir/myfile

sent 436 bytes  received 171 bytes  1,214.00 bytes/sec
total size is 0  speedup is 0.00
$ tree copydir
copydir
|-- file-1
|-- file-2
|-- file-3
|-- file-4
|-- file-5
`-- subdir
    `-- myfile

1 directory, 6 files

As a tip: You will get the reasoning behind why rsync includes or excludes things if you use -vv.

This is using the "wrong" exclusion pattern:

$ rm -r copydir
$ rsync -avv --include=myfile --exclude='subdir/' topdir/ copydir
sending incremental file list
[sender] hiding directory subdir because of pattern subdir/
created directory copydir
delta-transmission disabled for local transfer or --whole-file
./
file-1
file-2
file-3
file-4
file-5
total: matches=0  hash_hits=0  false_alarms=0 data=0

sent 326 bytes  received 211 bytes  1,074.00 bytes/sec
total size is 0  speedup is 0.00

This is using the "correct" exclusion pattern:

$ rm -r copydir
$ rsync -avv --include=myfile --exclude='subdir/*' topdir/ copydir
sending incremental file list
[sender] hiding file subdir/file-a because of pattern subdir/*
[sender] hiding file subdir/file-b because of pattern subdir/*
[sender] hiding file subdir/file-c because of pattern subdir/*
[sender] hiding file subdir/file-d because of pattern subdir/*
[sender] hiding file subdir/file-e because of pattern subdir/*
[sender] showing file subdir/myfile because of pattern myfile
created directory copydir
delta-transmission disabled for local transfer or --whole-file
./
file-1
file-2
file-3
file-4
file-5
[generator] risking file subdir/myfile because of pattern myfile
subdir/
subdir/myfile
total: matches=0  hash_hits=0  false_alarms=0 data=0

sent 436 bytes  received 307 bytes  1,486.00 bytes/sec
total size is 0  speedup is 0.00
0
0

I have found an other method. I use

cp --parents

option to copy only the specific files I want in the excluded directories and also to keep the directory structure.

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.