Using rsync instead of scp


When trying to copy files to a server using scp, and wanting to exclude a specific directory (such as cache) during the copy process, I used the following method. This was performed for an AWS EC2 instance. Since I have all the server-side code in a single Git repository, I wanted to copy only a portion of it to the server.

Command

Description

In the above command, a specific directory is excluded and copied to a remote machine. This creates the /home/ec2-user/app directory on the remote machine and copies the contents there.

Below is a brief explanation of the options:

rsync

This command uses remote shell to access the remote machine. It typically uses ssh as the remote shell.

-r

The recursive option. It processes files and directories recursively, allowing for directory copying.

-v

The common verbose option. It displays detailed information.

-e

Specifies the remote shell to use. Here, it’s using ssh -i ~/sshkeys/key.pem to access with a private key.

–exclude

Specifies files to exclude. Here, three patterns are specified.

Simple Case Command

If you just want to copy entire directories, you can use scp as follows: