Linux中的文件传输

需要两台主机并且保证这两台主机是可以通信的

在这里插入图片描述

scp命令

scp 本地文件 远程主机用户@远程主机 ip:远程主机目录的绝对路径
scp 远程主机用户@远程主机 ip:远程主机文件的绝对路径 本地文件

a)把本地文件复制到远程主机 (上传)
scp westos [email protected]:/root/Desktop
scp -r westosdir [email protected]:/root/Desktop ## -r 表示复制目录
scp -q westos [email protected]:/root/Desktop ## -q 传输文件时不显示
进度
b)把远程文件复制到本地(下载)
scp [email protected]:/root/Desktop/westos_rhel8 /root/Desktop

在这里插入图片描述
在这里插入图片描述

rsync命令

a) rsync 和 scp 命令的对比

  • 172.25.254.207 :
    dd if=/dev/zero of=/root/Desktop/westosfile1 bs=1M count=10 ##dd=截取,if=inputfile
    ##of=outputfile
    #bs=blocksize
    #count=快的个数
    dd if=/dev/zero of=/root/Desktop/westosfile2 bs=1M count=20
    dd if=/dev/zero of=/root/Desktop/westosfile3 bs=1M count=30

    在这里插入图片描述
  • 2 .在主机之间建立免密登录使远程文件传输可以直接执行
  • ssh-keygen ## 生成密钥
    ssh-copy-id -i /root/.ssh/id_rsa.pub. [email protected]

    在这里插入图片描述

    在这里插入图片描述
    3)创建测试脚本
    vim check_scp.sh ##检测 scp 传输时间
    time scp -qr /root/Desktop [email protected]:/root/Desktop
    time scp -qr /root/Desktop [email protected]:/root/Desktop
    time scp -qr /root/Desktop [email protected]:/root/Desktop
    在这里插入图片描述

vim check_rsync.sh ##检测 rsync 的传输时间
time rsync -raCq /root/Desktop [email protected]:/root/Desktop
time rsync -raCq /root/Desktop [email protected]:/root/Desktop
time rsync -raCq /root/Desktop [email protected]:/root/Desktop在这里插入图片描述

  • 执行:
  • sh check_scp.sh :在这里插入图片描述
  • 执行:
  • sh check_rsync.sh :
    在这里插入图片描述
    以上执行效果我们可以看出 rsync 三次执行时间后两次远远小与第一次
  • b)rsync 用法
    rsync 文件 远程用户@远程主机 ip:远程主机目录
    rsync 远程用户@远程主机 ip:远程主机目录 文件路径
    在这里插入图片描述
    rsync
-r 复制目录
-l 复制链接
-p 复制权限
t 复制时间戳
o 复制拥有者
g 复制拥有组
D 复制设备文件

**执行命令看效果:
rsync -r [email protected]:/root/Desktop  /mnt ##同步目录本身其目录中的文件在这里插入图片描述

rsync -r [email protected]:/root/Desktop/  /mnt ##只同步目录中的文件
在这里插入图片描述

rsync -rl [email protected]:/root/Desktop/  /mnt ##同步链接
rsync -rlp [email protected]:/root/Desktop/  /mnt ##同步权限
rsync -rlpog [email protected]:/root/Desktop/  /mnt ##同步用户组
rsync -rlpogt [email protected]:/root/Desktop/  /mnt ##同步时间
rsync -rD [email protected]:/dev/pts  /mnt ##同步设备文件**

文件的归档压缩

  • 1.文件归档
  • 所有命令都可以加参数v,来显示归档或者压缩的过程;
    tar
    |c |创建 |
    |–|--|
    | x | 指定文件名称 |
    | x | 解档
    |–|--|
    | v |显示过程 |
    | t | 查看
    |–|--|
    | r | 向归档文件中添加文件 |
    |- -get |解档指定文件 |
    |–|--|
    |- -delete | 删除制定文件 |
    |- C | 指定解档路径 |
  • tar cf etc.tar /etc/

在这里插入图片描述

  • tar rf etc.tar westosfile在这里插入图片描述

  • 解档
    在这里插入图片描述

  • tar f etc.tar --get westosfile 在这里插入图片描述

  • tar f etc.tar --delete westosfile

  • 删除文件:

  • 在这里插入图片描述

  • 文件的压缩

zip -r mnt.tar.zip mnt.tar #zip 格式压缩
unzip mnt.tar.zip #zip 格式解压缩
gzip mnt.tar #gzip 格式压缩
gunzip mnt.tar.gz gzip 格式解压缩
bzip2 mnt.tar #bzip2 格式压缩
bunzip2 etc.tar.bz2 #bzip2 格式解压缩
xz mnt.tar #xz 格式压缩
unxz mnt.tar.xz #xz 格式解压缩

zip格式压缩
在这里插入图片描述
解压
在这里插入图片描述
xz格式压缩
在这里插入图片描述
解压
在这里插入图片描述
**3.tar+压缩

  1. gzip
    tar zcf etc.tar.gz /etc
    tar zxf etc.tar.gz

  2. bzip2

tar jcf etc.tar.bz2 /etc
tar jxf etc.tar.bz2

  1. xz
    tar Jcf etc.tar.xz /etc
    tar Jxf etc.tar.xz
    在这里插入图片描述
    在这里插入图片描述