linux学习之 rsync VS. cp

来源:互联网 发布:阿里云 nodejs 编辑:程序博客网 时间:2024/05/19 00:15

Use Rsync Instead of Cp

MAY 9TH, 2010

I tend to use rsync when others would typically use a simple cp for copying files. A few reasons:

  1. It can be canceled in the middle, and resumed later.
  2. It can show a progress bar that (while not perfect) is great for large files or lots of files.
  3. It will only copy the changed files and won’t clobber already existing directories of files at the target.

rsync is available on Linux, Mac, and there’s a binary somewhere on teh intarwebs for Windows.

To use, you almost always want to pass the -a flag, which stands for “archive” – basically a convenience flag for -r (recursive), -p (permissions), -t (timestamps), and a few others. I can’t think of a time I haven’t used -a when using rsync.

Following that, basic usage looks like this:

1
rsync -a source destination

The other thing to remember when using rsync is that it’s picky with slashes; if you put a trailing lash on a path, then rsync takes that to mean you want to copy the contents of that path. If, on the other hand, you leave off the slash, it will copy the path and its contents. A few examples will help:

12345
# This will copy the stuff directory (and its contents) to the documents directoryrsync -a /home/tim/stuff /home/tim/documents# This will copy only the contents of stuff to the documents directoryrsync -a /home/tim/stuff/ home/tim/documents

Some other handy arguments:

  • --stats
  • --progress

So, to put it all together:

1
rsync -a --stats --progress /home/tim/stuff /home/tim/documents

And, better yet, rsync can work over ssh (if it’s installed on both hosts). Just put an ssh host on the front of either path:

1
rsync -a --stats --progress /home/tim/stuff tim@foo.example.com:/home/tim/

or

1
rsync -a --stats --progress tim@foo.example.com:/home/tim/stuff /home/tim/

So, there you have it. rsync can do tons more stuff, but this is a great start. If this is all you learn of rsync, you will be that much better off.

 May 9th, 2010

0 0
原创粉丝点击