If you want to copy files to another computer, it’s more secure to use scp (secure copy) than ftp.
Using SCP:
scp -pr * username@example.net:destination_path
In the command, -p stands for “preserve”, in other words, it keeps the last modified time, access time, etc the same, and -r recursively copies subdirectories.
Using Rsync:
If you want to just copy files that have changed, a better solution is to use Rsync:
rsync -ra –progress –verbose –delete –rsh=/usr/bin/ssh –size-only * username@example.net:destination_path
-r in this command is for recursive copies, and -a stands for archive, which keeps the modified times the same. –progress and –verbose outputs everything it’s doing and lets you know how much more it needs to check, –delete deletes anything that doesn’t exist at the source location any longer, –rsh makes sure you’re using ssh for security reasons, –size-only updates anything where the filesize has changed. Normally, it compares hashes of the files but that takes more time to process. The * is the source location, which would normally be a path somewhere if it’s in a cronjob, and the last part is the destination location, which doesn’t need a trailing slash; for example, public_html/images
To automate Rsync, you’ll need to have ssh keys installed so that you can bypass the need to enter in a password.
Installing an ssh key:
Run this command on the source computer:
ssh-keygen -t dsa
This will create a pair of keys at $home_dir/.ssh/ and by default, the public key will be called id_dsa.pub
Security Note: When you create your key, it will ask for a password. If you leave it blank, you’ll be able to ssh to the destination box without needing a password, which is useful when you need ssh in an automated script; however, this makes it less secure in the case where somebody compromises your user account - they would then have access to all of the machines with your public key installed. However, if you use a password, you’ll need to enter that password each time you want to ssh, which will be problematic in automated scripts.
Copy the contents of the public key to the destination computer at:
$home_dir/.ssh/authorized_keys
Troubleshooting:
Occasionally, I’ve gotten this error, which prevented me from copying files inside a particular directory, and it confused me at first because my user had read permissions, but it wouldn’t copy until I gave the directory execute permissions too:
rsync: recv_generator: failed to stat “/path/to/file”: Permission denied (13)
The way I got around this was to run: ‘chmod 777 thefile’, but ‘chmod 700 thefile’ should work too assuming that you don’t want anybody seeing the file but you. A 7 in that command sets it to rwx or read, write, and execute.
SCP is great for copying the contents of a folder, while Rsync is great to mirror or back up files, and with a combination of the two, you’ll rarely need to use FTP again.