2011-09-22

scp and rsync: two cool UNIX commands for file transferring between computers

by Forrest Sheng Bao http://fsbao.net

This is a simple tutorial for my colleagues who need to transfer files between Linux/Mac OS X boxes. I love scp and rsync. They are both very simple and there is no need to start a GUI.

scp

scp means "SSH copy". Thus you can copy a file from a server to you computer (or even another server) via SSH. It's syntax is very simple.
scp OPTIONS SOURCE DESTINATION
For example, if I wanna copy all files under /data/MEG/MEG077 on example.com to my current computer, I will do this
scp -r  example.com:/data/MEG/MEG077/* .
The -r option means "recursive" that I need to copy everything in MEG077 hierarchically.

rsync

rsync is good to synchronize files incrementally. "Incrementally" means only copying changed files. It's syntax is as simple as scp.
rsync OPTIONS SOURCE DESTINATION OPTIONS
For example, if I wanna make sure everything under /data on example.com, except hidden files, is synchronized to my current folder, I will do this.
rsync -azv --delete example.com:/data/ . --exclulde=".*"

In -azv options, z means compressing the transmission while v means verbose. The --delete option means deleting files that are not on the source but the destination. The --exclude option specifies the filename pattern to avoid in synchronization. Since hidden files on Linux/Mac OS X begin their filename with a dot, --exclulde=".*" means avoiding all hidden files.

Please be aware of the last backslash (/) after example.com:/data. When there IS a backslash, it means all subfolders under that path. If there is none, it means the folder itself specified by the path. The same rule applies to both source and destination.

You can also synchronize your local folder to remote servers, or even between two local folders, using rsync.

No comments: