Skip to content
Log In Sign Up info@muvcloud.com

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

PatternMatches
cacheAny file or folder named cache, at any level
cache/Only folders named cache
/cacheOnly cache at the top of the transfer
*.logFiles 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.
  • ./app copies the folder itself, creating app inside 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

OptionWhat it does
-aArchive: recursive, keeps permissions and times
-vLists files as they transfer
-zCompresses data in transit
-PShows progress, resumes partial files
-nDry 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations