Table of Contents
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
1 |
rsync -rv -e 'ssh -i ~/sshkeys/key.pem' --exclude '**/__pycache__' --exclude '**/*.pyc' --exclude '**/.idea' ~/project/app ec2-user@12.34.56.78:/home/ec2-user/ |
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:
1 |
scp -r user@remote:/copy/from /copy/to |