Exclude One Directory
rsync -av --exclude='node_modules' ./app/ user@your_server_ip:/var/www/app/
This copies the contents of ./app to the server and skips every folder or file named node_modules, at any depth. Quote patterns so the shell does not expand them.
Exclude Several Items
Repeat the option:
rsync -av --exclude='node_modules' --exclude='.git' --exclude='*.log' ./app/ user@your_server_ip:/var/www/app/
In bash, brace expansion writes the same thing in a shorter form: --exclude={'node_modules','.git','*.log'}. The shell turns it into three options before rsync runs.
Use an Exclude File
For a longer list, put one pattern per line in a file. Lines starting with # are comments:
# exclude.txt
node_modules/
.git/
*.log
/storage/cache/
.env
rsync -av --exclude-from='exclude.txt' ./app/ user@your_server_ip:/var/www/app/
The pattern style is close to that of a .gitignore file, but the two are not identical.
How Patterns Match
| Pattern | Matches |
|---|---|
cache | Any file or folder named cache, at any level |
cache/ | Only folders named cache |
/cache | Only cache at the top of the transfer |
*.log | Files ending in .log, anywhere |
logs/* | The contents of logs, but not the folder |
A leading slash anchors a pattern to the root of the transfer, not to the root of the file system. /cache means the cache folder directly inside the source you named.
The Trailing Slash on the Source
./app/copies the contents of app into the destination. The top of the transfer is inside app, so the anchored pattern is/cache../appcopies the folder itself, creatingappinside the destination. The top of the transfer now contains app, so the anchored pattern becomes/app/cache.
A slash on the destination makes no difference. When an anchored exclude seems to be ignored, the source slash is the first thing to check.
Always Dry-Run First
rsync -avn --exclude-from='exclude.txt' ./app/ user@your_server_ip:/var/www/app/
-n (--dry-run) lists what would be transferred and changes nothing. Read the list, check that the excluded items are missing from it, then run the same command without -n. Add -i to see why each file would be copied.
Include and Exclude Together
rsync checks the rules in order, and the first match wins. To copy only PHP files and the folders that contain them:
rsync -avm --include='*/' --include='*.php' --exclude='*' ./src/ ./php-only/
--include='*/' lets rsync enter every folder, --exclude='*' drops everything else, and -m removes the folders that end up empty.
Take Care with --delete
--delete removes files from the destination that no longer exist in the source. A wrong source path, such as an empty folder, can empty the destination. Always run it with -n first and make sure a backup exists, as described in snapshot vs backup. Excluded files on the destination are protected from --delete; --delete-excluded removes them as well.
Common rsync Options
| Option | What it does |
|---|---|
-a | Archive: recursive, keeps permissions and times |
-v | Lists files as they transfer |
-z | Compresses data in transit |
-P | Shows progress, resumes partial files |
-n | Dry run |
-e "ssh -p 2222" | Uses SSH on another port |
For one-off copies, scp is enough. If SSH runs on a custom port, see how to change the SSH port.