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

by Forrest Sheng Bao

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 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.
For example, if I wanna copy all files under /data/MEG/MEG077 on to my current computer, I will do this
scp -r* .
The -r option means "recursive" that I need to copy everything in MEG077 hierarchically.


rsync is good to synchronize files incrementally. "Incrementally" means only copying changed files. It's syntax is as simple as scp.
For example, if I wanna make sure everything under /data on, except hidden files, is synchronized to my current folder, I will do this.
rsync -azv --delete . --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 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: