EDDYMENS

Published a week ago

How To Use Rsync To Sync Files Between Servers While Excluding Specific Folders

Table of contents

The rsync --exclude option flag can be used to exclude specific files and folders when syncing up with are remote server.

You can apply multiple --exclude statements to exclude several files and folders.

Syncing from Local to Server (Upload)

To sync files from your local machine to a server and exclude a specific folder:

$ rsync -av --exclude 'folder_to_exclude' --exclude 'file_to_exclude' /path/to/local/dir/ user@server:/path/to/remote/dir

Syncing from Server to Local (Download)

To sync files from a server to your local machine while excluding a specific folder/file:

$ rsync -av --exclude 'folder_to_exclude' --exclude 'file_to_exclude' user@server:/path/to/remote/dir/ /path/to/local/dir

Using an Exclude File

If you have many files and folders to exclude, you can list them in a text file and use the --exclude-from option to reference that file.

  1. Create an exclude file (e.g. exclude-list.txt), and list each file or folder on a new line:

exclude-list.txt

01: folder1 02: file2.txt 03: folder3
  1. Run rsync with the --exclude-from option, referencing the file:
$ rsync -av --exclude-from='exclude-list.txt' /path/to/source/ user@server:/path/to/destination/

-a: Archive mode, which preserves permissions, timestamps, etc, -v: Verbose mode, shows detailed output.

Here is another article you might like 😊 package: String does not match the pattern... |NPM|package.json|Warning