Linux —— 文件传输优化

目录

scp命令

a) 把本地文件复制到远程主机

b) 把远程主机文件复制到本地

rsync命令

rsync命令和scp命令的对比

在rhel7中建立文件

设置免密登录使远程文件传输可以直接进行

创建测试脚本,检测两者的传输时间

执行结果

文件的打包及压缩

打包

压缩

打包时压缩 


实验环境

两台可以互相通信的主机,这里我使用的是rhel8.1和rhel7.6两个虚拟机。

rhel8:192.168.43.11

rhel7:192.168.43.10

scp命令

a) 把本地文件复制到远程主机

scp    本地文件    远程主机用户@远程主机ip:远程主机目录

命令 作用
scp     本地文件    远程主机用户@远程主机ip:远程主机目录 表示传输文件时显示进度
scp -q 本地文件    远程主机用户@远程主机ip:远程主机目录 -q表示传输文件时不显示进度
scp -r  本地目录    远程主机用户@远程主机ip:远程主机目录 -r表示复制目录

实验效果:

Linux —— 文件传输优化

Linux —— 文件传输优化

b) 把远程主机文件复制到本地

scp 远程主机用户@远程主机ip:远程主机文件  本地目录

命令 作用
scp     远程主机用户@远程主机ip:远程主机文件 本地目录 表示传输文件时显示进度
scp -q  远程主机用户@远程主机ip:远程主机文件  本地目录 -q表示传输文件时不显示进度
scp -r  远程主机用户@远程主机ip:远程主机目录  本地目录 -r表示复制目录

Linux —— 文件传输优化

Linux —— 文件传输优化

rsync命令

rsync    文件                                                         远程用户@远程主机ip:远程主机目录

rsync    远程用户@远程主机ip:远程主机目录    文件路径

命令 作用

-r

复制目录
-l 复制链接
-p 复制权限
-t 复制时间戳
-o 复制拥有者
-g 复制拥有组
-D 复制设备文件

/mnt :同步目录本身和目录中的文件 

Linux —— 文件传输优化

/mnt/ : 只同步目录中的文件

Linux —— 文件传输优化

-rl :同步链接

Linux —— 文件传输优化

-rlp : 同步权限

Linux —— 文件传输优化

-rlpog : 同步用户组

Linux —— 文件传输优化

-rlpogt : 同步时间

Linux —— 文件传输优化

-rD : 同步设备文件

Linux —— 文件传输优化

rsync命令和scp命令的对比

在rhel7中建立文件

dd if=/dev/zero of=/mnt/file1 bs=1M count=10

dd if=/dev/zero of=/mnt/file2 bs=1M count=20

dd if=/dev/zero of=/mnt/file3 bs=1M count=30

Linux —— 文件传输优化

 但是考虑到系统会将我们设置密码的时间也统计在内,会影响到实验结果,因此在这里我们将其设置为免密登  ↵

设置免密登录使远程文件传输可以直接进行

ssh-******                                                                                  生成**

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]            将**复制到rhel8主机

Linux —— 文件传输优化

创建测试脚本,检测两者的传输时间

vim check_scp.sh

time scp -qr /mnt [email protected]:/mnt
time scp -qr /mnt [email protected]:/mnt
time scp -qr /mnt [email protected]:/mnt

vim check_rsync.sh

time rsync -raCq /mnt [email protected]:/mnt
time rsync -raCq /mnt [email protected]:/mnt
time rsync -raCq /mnt [email protected]:/mnt

执行结果

Linux —— 文件传输优化

Linux —— 文件传输优化

文件的打包及压缩

打包

tar

参数 作用
c 创建
f 指定文件名称
x 解档
t 查看
r 向归档文件中添加文件
--get 解档指定文件
--delete 删除指定文件
-C 指定解档路径

打包文件

tar cf etc.tar /etc

Linux —— 文件传输优化

查看文件 

tar tf etc.tar

Linux —— 文件传输优化

向包中存入文件

tar rf etc.tar 文件传输优化

Linux —— 文件传输优化

从包中移除文件

tar xf etc.tar                                          取出全部文件

tar f etc.tar  --get  文件传输优化           取出单个指定文件

Linux —— 文件传输优化

Linux —— 文件传输优化

压缩

zip

zip   -r   etc.tar.zip etc.tar     ##zip格式压缩

unzip    etc.tar.zip               ##zip格式解压缩 

 

Linux —— 文件传输优化

Linux —— 文件传输优化

gzip

gzip       etc.tar               ##gzip格式压缩文件

gunzip  etc.tar.gz           ##gzip格式解压缩文件

 

Linux —— 文件传输优化

Linux —— 文件传输优化

 bzip2

bzip2       etc.tar               ##bzip2格式压缩文件

bunzip2  etc.tar.gz           ##bzip2格式解压缩文件

 

Linux —— 文件传输优化

Linux —— 文件传输优化

 xz

xz       etc.tar               ##xz格式压缩文件

unxz   etc.tar.gz           ##xz格式解压缩文件

 

Linux —— 文件传输优化

 

Linux —— 文件传输优化

打包时压缩 

 zip格式不支持,其余三种格式均可

gzip格式:tar zcf etc.tar.gz /etc

bzip2:     tar jcf etc.tar.bz2 /etc

xz格式:   tar Jcf etc.tar.xz /etc

解压缩时,直接将c替换为x即可解压缩。

Linux —— 文件传输优化

 

Linux —— 文件传输优化

 

Linux —— 文件传输优化