linux学习之 rsync VS. cp

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:

1
2
3
4
5
# This will copy the stuff directory (and its contents) to the documents directory
rsync -a /home/tim/stuff /home/tim/documents

# This will copy only the contents of stuff to the documents directory
rsync -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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值