Linux系统之文件在系统中的传输

本文介绍了Linux系统中通过scp和rsync进行文件传输的操作,包括上传、下载以及同步,并讲解了使用tar进行归档和压缩的方法,包括gzip、bzip2和xz压缩格式。

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

scp

scp进行数据复制——有没有都复制,稳定(准确)性高

  1. 上传——scp file root@ip:dir
  2. 下载——scp root@ip:file dir

##将westos文件上传到ip为28的主机中
[root@westos_server Desktop]# mkdir westos
[root@westos_server Desktop]# touch westos/file{1…9}
[root@westos_server Desktop]# ls westos/
file1 file2 file3 file4 file5 file6 file7 file8 file9
##进行文件上传工作
[root@westos_server Desktop]# scp ‘/root/Desktop/westos’ root@172.25.254.28:/root/Desktop
The authenticity of host ‘172.25.254.28 (172.25.254.28)’ can’t be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.25.254.28’ (ECDSA) to the list of known hosts.
root@172.25.254.28’s password:
/root/Desktop/westos: not a regular file
[root@westos_server Desktop]# scp -r ‘/root/Desktop/westos’ root@172.25.254.28:/root/Desktop
root@172.25.254.28’s password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
file4 100% 0 0.0KB/s 00:00
file5 100% 0 0.0KB/s 00:00
file6 100% 0 0.0KB/s 00:00
file7 100% 0 0.0KB/s 00:00
file8 100% 0 0.0KB/s 00:00
file9 100% 0 0.0KB/s 00:00
在这里插入图片描述 ##将ip为18的主机上的westos文件上传到ip为28的主机中
[root@westos_client Desktop]# rm -rf *
[root@westos_client Desktop]# scp -r root@172.25.254.18:/root/Desktop/westos .
root@172.25.254.18’s password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
file4 100% 0 0.0KB/s 00:00
file5 100% 0 0.0KB/s 00:00
file6 100% 0 0.0KB/s 00:00
file7 100% 0 0.0KB/s 00:00
file8 100% 0 0.0KB/s 00:00
file9 100% 0 0.0KB/s 00:00
在这里插入图片描述
注意:1.在上传或下载目录时需要添加-r

rsync

数据同步——默认会忽略文件属性,连接文件,设备文件
优点:有的时候什么都不做,没有复制数据,速度比scp快

名称方法
同步目录rsync -r
同步权限rsync -p
同步文件所有人rsync -o
同步文件所有组rsync -g
同步连接rsync -l
同步设备文件rsync -D
同步文件时间戳rsync -t
##新建一个文件夹,里面的文件及其组属于tian用户
[root@westos_server Desktop]# mkdir /mnt/westos
[root@westos_server Desktop]# touch /mnt/westos/file{1..4}
[root@westos_server Desktop]# useradd tian
[root@westos_server Desktop]# chmod 777 /mnt/westos/*
[root@westos_server Desktop]# chown tian.tian /mnt/westos/*
[root@westos_server Desktop]# ll /mnt/westos/*
-rwxrwxrwx. 1 tian tian 0 Jul 19 01:03 /mnt/westos/file1
-rwxrwxrwx. 1 tian tian 0 Jul 19 01:03 /mnt/westos/file2
-rwxrwxrwx. 1 tian tian 0 Jul 19 01:03 /mnt/westos/file3
-rwxrwxrwx. 1 tian tian 0 Jul 19 01:03 /mnt/westos/file4
##同步目录,将文件westos传递给ip为208的主机
[root@westos_server Desktop]# rsync -r /mnt/westos root@172.25.254.208:/root/Desktop
The authenticity of host '172.25.254.208 (172.25.254.208)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.208' (ECDSA) to the list of known hosts.
root@172.25.254.208's password: 

在这里插入图片描述

##同步目录和权限,将文件westos传递给ip为208的主机
[root@westos_server Desktop]# rsync -rp /mnt/westos root@172.25.254.208:/root/Desktop
root@172.25.254.208's password: 
Permission denied, please try again.
root@172.25.254.208's password: 

在这里插入图片描述

##同步文件所有人,目录和权限,将文件westos传递给ip为208的主机
[root@westos_server Desktop]# rsync -rpo /mnt/westos root@172.25.254.208:/root/Desktop
root@172.25.254.208's password: 

在这里插入图片描述
查看显示1002是tian用户的ip地址,因为在该主机中没有tian’用户

##同步文件所有人,所有组,目录和权限,将文件westos传递给ip为208的主机
[root@westos_server Desktop]# rsync -rpog /mnt/westos root@172.25.254.208:/root/Desktop
root@172.25.254.208’s password:

##创建连接
[root@westos_server Desktop]# ln -s /mnt/westos /mnt/westos1
[root@westos_server Desktop]# ls -ld /mnt
drwxr-xr-x. 3 root root 33 Jul 19 01:16 /mnt
[root@westos_server Desktop]# ls -ld /mnt/westos1
lrwxrwxrwx. 1 root root 11 Jul 19 01:16 /mnt/westos1 -> /mnt/westos

在这里插入图片描述

查看显示1002是tian用户的ip地址,因为在该主机中没有tian’用户

##同步连接,文件所有人,所有组,目录和权限,将文件westos传递给ip为208的主机
[root@westos_server Desktop]# rsync -rpogl /mnt/westos1 root@172.25.254.208:/root/Desktop
root@172.25.254.208’s password:

在这里插入图片描述

连接失效,该连接指向原来的/mnt/westos文件,现文件没有该指向

归档文件——tar 

归档文件,把很多文件变成1个文件

名称方法
创建tar -c
显示过程tar -v
指定归档文件名称tar -f
解档tar -x
查看归档内容tar -t
添加文件到归档中tar -r
查看归档内容tar -t
解档指定文件tar --get
删除归档中的指定内容tar --delete
指定解档目录tar -C
##执行history后练习tar命令
##创建并指定归档名称为etc.tar
    1  tar cf etc.tar /etc
    2  rm -rf etc.tar 
##创建,指定归档名称并显示出归档过程为etc.tar
    3  tar cvf etc.tar /etc
##查看归档内容
    4  tar tf etc.tar 
    5  touch file
##添加指定文件file到归当中
    6  tar rf etc.tar file
##释放出指定文件file到当前目录中
    7  tar xf etc.tar 
    8  rm -rf etc file
##解档指定文件file到当前目录
    9  tar f etc.tar --get file
##删除归档中的指定内容  
   10  tar f etc.tar --delete file
   11  tar xf etc.tar 
##将解档文件放到指定目录
   12  tar xf etc.tar -C /mnt/
   13  ls /mnt/

在这里插入图片描述

压缩

- zip

名称方法
压缩zip -r xxx.tar.zip xxx.tar
解压缩unzip xxx.tar.zip

##归档/etc为etc.tar文档
[root@westos_server Desktop]# tar cf etc.tar /etc
tar: Removing leading `/’ from member names
##压缩文件
[root@westos_server Desktop]# zip -r etc.tar.zip etc.tar
adding: etc.tar (deflated 72%)
##解压缩,因为压缩前文档未被代替,故出现下面内容
[root@westos_server Desktop]# unzip etc.tar.zip
Archive: etc.tar.zip
replace etc.tar? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: rtc1.tar
inflating: rtc1.tar
在这里插入图片描述

源文件保存,不能打包压缩一体化

- gz

名称方法
压缩gzip ;xxx.tar.gz
解压缩gunzip xxx.tar.gz
打包压缩tar zcf ;xxx.tar.gz  /xxx
解压缩tar zxf xxx.tar.gz

##可以直接压缩文件
[root@westos_server Desktop]# gzip etc.tar
##解压缩
[root@westos_server Desktop]# gunzip etc.tar.gz
[root@westos_server Desktop]# rm -rf *
##归档压缩一体化
[root@westos_server Desktop]# tar zcf etc.tar.gz /etc
tar: Removing leading `/’ from member names
##归档压缩一体化
[root@westos_server Desktop]# tar zxf etc.tar.gz
在这里插入图片描述

源文件不会保存
-bz2

名称方法
压缩bzip2 ;xxx.tar
解压缩bunzip2 xxx.tar.bz2
打包压缩tar zcf ;xxx.tar.bz2  /xxx
解压缩tar zxf xxx.tar.bz2

##归档/etc为etc.tar文档
[root@westos_server Desktop]# tar cf etc.tar /etc
tar: Removing leading `/’ from member names

##压缩文件
[root@westos_server Desktop]# bzip2 etc.tar
##解压缩文件
[root@westos_server Desktop]# bunzip2 etc.tar.bz2
[root@westos_server Desktop]# rm -rf *
##归档压缩一体化
[root@westos_server Desktop]# tar jcf etc.tar.bz2 /etc
tar: Removing leading `/’ from member names
##归档压缩一体化
[root@westos_server Desktop]# tar jxf etc.tar.bz2

在这里插入图片描述
源文件不会保存
- xz

名称方法
压缩xz .tar.zipxxx.tar
解压缩unxz xxx.tar.xz
打包压缩tar Jcf xxx.tar.xz  /xxx

##归档/etc为etc.tar文档
[root@westos_server Desktop]# tar cf etc.tar /etc
tar: Removing leading `/’ from member names

##压缩文件
[root@westos_server Desktop]# xz etc.tar
##解压缩文件
[root@westos_server Desktop]# unxz etc.tar.xz
[root@westos_server Desktop]# rm -rf *
##归档压缩一体化
[root@westos_server Desktop]# tar Jcf etc.tar.xz /etc
tar: Removing leading `/’ from member names
在这里插入图片描述

不能解压解档一体化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值