fast copy

本文介绍了高效复制文件的几种方法,包括递归复制、管道方式、使用SCP和SSH进行网络传输,以及利用netcat和socat进行数据传输。重点强调了不同场景下选择最合适的方法,并讨论了网络传输过程中的压缩与解压技术,以及如何处理稀疏文件以节省磁盘空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[edit] The normal way

To copy files recursively from src1/, src2/, to dest/ you do

cp -Rv src1/ src2/ dest/

[edit] The piped way

cp uses character by character copy. Using the kernel pipes support, we could copy files block by block. tar converts the directories recursively into a single stream.At one end we create a single stream out of source directories and at other end we extract this stream and put it in the destination directory. The transfer between the two ends is by means of pipes.

tar -Sc src1/ src2/ | tar -C dest/ -xv

This is not necessarily faster, but is much more flexible. Note that ext2, ext3 and most modern Linux file systems have a capability called 'sparse files'. This is used to store large files with lots of zeroed content in an efficient manner. Most of the time you should not care about this, but certain programs make extensive use of this feature (e.g. net-p2p/mldonkey). You should be careful when copying sparse files using this method, as your disk usage can explode if you forget to use the -S switch on the left side. Whereas the cp utility does handle sparse files automatically, tar (without -S) does not. Sparse files in the source directory will be stored in full representation on the destination directory.

[edit] Network with SCP

If you want to transfer a file, the following may be used

# Remote to local
scp -C remotebox:path/to/sourcefile .
# Local to remote
scp -C localfile remotebox:path/to/destination/

If multiple files or directories are to be transferred, the following may be used

# Remote to local
scp -rC remotebox:path/to/sourcedir .
# Local to remote
scp -Cr localdir remotebox:path/to/destination/

[edit] Network with SSH

A distinction must be whether from a local system to a remote or vice versa is to be copied:

# to the remote system
tar czv dir/files | ssh remote.box.com "tar xz -C /dir/"
# to the remote system with faster encryption 
tar czv ListOfFiles | ssh -c blowfish remote.box.com tar xz -C /home/user/PathToCopy
# from remote to local
ssh remote.box.com tar cz -C BeginDirCopyFiles |tar xz -C DirToCopy

Transfer of individual files can also be made with tar, but also suitable for this cat:

# local to remote
cat dir/file | ssh -C remote.box.com "cat > /dir/file"
# combined with gzip or bz2
cat dir/file | gzip | ssh -C remote.box.com "gunzip > /dir/file"
cat dir/file | bzip2 | ssh -C remote.box.com "bunzip2 > /dir/file"
# remote to local
ssh remote.box.com "cat /dir/file" > dir/file
# combined with gzip or bz2
ssh remote.box.com "cat /dir/file | gzip" | gunzip > dir/file
ssh remote.box.com "cat /dir/file | bzip2" | bunzip2 > dir/file

[edit] Network with netcat

Transfers over netcat can have miniscule CPU needs, unlike transfers over ssh. However the data is transmitted without encryption and authentication.

Destination box: nc -l -p 2342 | tar -C /target/dir -xz -
Source box: tar -cz /source/dir | nc Target_Box 2342

For further CPU use reduction, lzop can be used in place of the tar z option for much faster but less effective compression.

Destination box: nc -l -p 2342 | lzop -d | tar -C /target/dir -x -
Source box: tar -c /source/dir | lzop | nc Target_Box 2342

Network with socat Same idea as Netcat.

Destination box: socat -u - tcp4-listen:2342 | tar x -C /target/dir
Source box: tar c /source/dir | socat -u - tcp4:Target_Box:2342

We can use a variety of compression methods in a general way:

Destination box: socat -u - tcp4-listen:2342 | ${UNZIP} | tar x -C /target/dir
Source box: tar c /source/dir | ${ZIP} | socat -u - tcp4:Target_Box:2342

We then define ZIP as one of the following:

   *  cat
   *  lzop
   *  gzip
   *  bzip2

and UNZIP as:

   *  cat
   *  lzop -d
   *  gunzip
   *  bunzip2

What's the difference?


time -p tar /usr/src/linux-2.6.3 | ${ZIP} | cat > /dev/null
   *  cat: 182MB, 1.1sec
   *  lzop: 64MB, 4sec
   *  gzip --fast: 51MB, 9sec
   *  gzip: 41MB, 18sec
   *  gzip --best: 41MB, 49sec
   *  bzip2: 32MB, 134sec

(edit: umm... this is kind of irrelevant. It all depends on how fast your network connection is, and how fast each computer is. If your processors/disks are really slow and your network is really fast, strait cat would work best. if your processors/disks are really fast but your network is, say, a dialup, or just a slow wireless connection or something, you're way better off with the smallest transfer (like, bzip2 or gzip). Reality is, you're gonna be somewhere in the middle, but there's a reason that they put the kernel in .bz2 on kernel.org)

FastCopy ver2.08 2011/02/28 SHIROUZU Hiroaki FastCopy is the Fastest Copy/Delete Software on Windows. It can copy/delete unicode and over MAX_PATH(260byte) pathname files. It always run by multi-threading. It don't use MFC, it is compact and don't requre mfcxx.dll. FastCopy is BSD license, you can modify and use. License: --------------------------------------------------------------- Copyright 2004-2011 SHIROUZU Hiroaki All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY SHIROUZU Hiroaki ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIROUZU Hiroaki OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --------------------------------------------------------------- Usage丗 Please see fastcopy.chm Build: FastCopy/Install/ShellExt ... VC4.1 ShellExt64 ... x64 compiler
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值