Secure transfer File on Linux – SCP and RSYNC

The command to secure copy a file in linux is scp.

How do I copy a file from one computer to another?

The general form is:

# scp source target

an example:

# scp joeshmoe@miles.shadlen.org:matlab/mfile.m matlab/

This will copy the file mfile.m from the matlab directory on miles to the directory matlab on whatever machine the user is on currently. If there is no directory matlab in the current directory, an error message will be returned. Either type out the whole pathway (~/matlab – if the directory matlab is in your home directory) or the pathway relative to your current directory (./ for current directory). The name of the file can also be printed in the target, which is useful if you want to give the file a new name in its new place. Let’s say you wanted to copy the file example.m from your matlab directory (which is in your current directory) on the computer you are on, change the name to silly.m and place it in a directory called unfinished in your home directory on miles.

# scp example.m joeshmoe@miles.shadlen.org:unfinished/silly.m

The command to synchronize two directories (or two directory trees) so that they look the same is called rsync.

How do I sync two directories?

To update the files on a target to reflect changes you have made at a source, use

# rsync -avub -e ssh source target

where source and target are written according to scp syntax. For
example, given ~/matlab/project/ directories on both merleau and
localhost, you can type

# rsync -avub -e ssh ~/matlab/project merleau.rprc.washington.edu:matlab/

to update all the files in project on merleau to reflect the changes you
have made to them on localhost. If the directory project does not exist on merleau inside the directory matlab, rsync will create it. And if you had typed matlab/project/ as the target, rsync would create another directory project inside the directory project. It is important whether there is a slash at the end of the source directory. A trailing / on a source name means “copy the contents of this directory”. Without a trailing slash it means “copy the directory”. The -b option copies all of the backup files as well, which can lead to excessive copies of backups, since sync used with these options does not delete any files. Rsync, used with this syntax, will not overwrite newer files in the target.

A little thought will convince you that a complete synchronization may require doing a rsync in both directions.

For more information,

#  man rsync

Leave a Reply

Your email address will not be published. Required fields are marked *