0

I'm trying to automate some backups with rsync and have the following problem. I've got a file that contains some stuff to be excluded from the copying. Assume that the file has a single line

exc

That I want rsync to do is to exclude any files or directory called 'exc' in the directory that I'm giving rsync. However, if we have the following tree:

./
 |
 +---exc
 |      file1
 |
 +---directory
     +  file2
     |
     |
     +---exc
            file3

I hope the diagram is clear. What I want to happen is for the first exc to be excluded but for the second to be copied, while passing a file with the line exc will exclude both. There may be other files called exc at deeper levels so the line in the excludes file should point only to the root file exc.

What should the exclusion pattern look like?

1
  • 1. Can you control the format of the file/directory names in the exclusions file? If not it's likely impossible to solve the problem without processing it into a different file for the rsync process to use 2. Please can you edit your question to show the rsync command you've tried. The reason for this is to give us a hook on which we can build our changes/solutions. 3. Please clarify (in your question) what criteria determine that one exc should be excluded but the other one can be included Commented Nov 30, 2020 at 23:35

1 Answer 1

2

You can prepend --exclude with a / to anchor it to the root of the transfer. Thus, --exclude /exc will do what you want.

Using your test case:

mkdir -p exc directory/exc
touch exc/file1 directory/file2 directory/exc/file3
rsync -av --exclude /exc . /tmp/

/tmp will then contain the current folder, including directory/exc/*, except the exc/ at the original folder.

1
  • Perfect, thank you! Commented Nov 30, 2020 at 23:42

You must log in to answer this question.

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