Linux之系统中的文件传输
实验环境
而要两台主机并保证这两台主机是可以通信的
node26:172.25.254.26
node126:172.25.254.126
node26&node126
systemctl disable --now firewalld
systemctl stop firewalld
一、scp命令
scp 本地文件 远程主机用户@远程主机ip:远程主机目录的绝对路径
scp 远程主机用户@远程主机ip:远程主机文件的绝对路径 本地文件
实验步骤:
1.在node126建立实验素材
touch westos
mkdir westosdir
2.测试
a.把本地文件复制到远程主机(上传)
远程复制文件
[root@node126 test]# touch westos
[root@node126 test]# mkdir westosdir
[root@node126 test]# scp westos [email protected]:/root/Desktop
[email protected]'s password:
westos 100% 0 0.0KB/s 00:00
[root@node126 test]#
远程复制目录
[root@node126 test]# scp -r westosdir [email protected]:/root/Desktop
[email protected]'s password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
[root@node126 test]#
``
『node26』
[root@node26 ~]# scp -r /etc/ [email protected]:/mnt
[root@node26 ~]# tar cf etc.tar /etc/
tar: Removing leading `/' from member names
[root@node26 ~]# du -sh etc.tar
27M etc.tar
[root@node26 ~]#
速度差异惊人。
[root@node26 ~]# scp etc.tar [email protected]:/mnt
[email protected]'s password:
etc.tar 100% 27MB 189.5MB/s 00:00
[root@node26 ~]#
[root@node126 ~]# du -sh /etc/
30M /etc/
[root@node126 ~]# du -sh /mnt/etc.tar
27M /mnt/etc.tar
[root@node126 ~]#
文件的归档和压缩
1. 文件归档
tar c
创建
tar f
指定文件名称
tar x
解档
tar v
现实过程
tar t
查看
tar r
向归档文件中添加文件
tar --get
解档指定文件
tar --delete
删除指定文件
tar-C
指定解档路径
tar -P
don’t remove “/”
tar c
创建 ##tar中,/etc表示目录本身,/etc/表示目录及其子目录
tar f
指定文件名称 ##不单独使用
刚刚在实验中,
[root@westos_student26 test]# tar cf westos.tar /ect
然后
我查看了westos.tar,空的。tar中,/etc表示目录本身,/etc/表示目录及其子目录
[root@node26 ~]# tar tf westos.tar /etc/ ##错的,不存在这种用法
tar: westos.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar x
解档
[root@node26 mnt]# tar xf westos.tar
tar v
现实过程
[root@node126 ~]# tar cvf etc.tar /etc/
##无提示
[root@node26 mnt]# tar cf etc123.tar /etc/
tar: Removing leading `/' from member names
[root@node26 mnt]#
tar t
查看
[root@node26 mnt]# tar tf westos.tar
tar r
向归档文件中添加文件
[root@node26 mnt]# tar rf westos.tar 444
[root@node26 mnt]# tar tf westos.tar
tar --get
解档指定文件
[root@node26 mnt]# tar f westos.tar --get etc/hostname
tar --delete
删除指定文件
[root@node26 mnt]# tar f westos.tar --delete 444
[root@node26 mnt]# tar tf westos.tar
444文件不见了
tar-C
指定解档路径
[root@node26 mnt]# tar xf westos.tar -C /opt
[root@node26 mnt]# ll /opt/
tar -P
don’t remove “/”
[root@node26 mnt]# tar -Pcf westetc.tar /etc/
[root@node26 mnt]# tar tf westetc.tar
du -sh /etc
文件大小查询命令
[root@node26 mnt]# du -sh /etc
30M /etc
[root@node26 mnt]#
2.文件的压缩
//zip
[root@node26 mnt]# zip -r westos.tar.zip westos.tar
adding: westos.tar (deflated 77%)
[root@node26 mnt]#
[root@node26 mnt]# rm westos.tar -fr
[root@node26 mnt]# ll
total 6436
-rw-r--r--. 1 root root 6587087 Jan 29 07:01 westos.tar.zip
[root@node26 mnt]#
[root@node26 mnt]# unzip westos.tar.zip
Archive: westos.tar.zip
inflating: westos.tar
[root@node26 mnt]#
gzip
需对tar文件进行操作
gzip
gunzip
etc.tar.gz
后缀名
su -sh xxx.tar.gz
文件大小查询
gzip
[root@westos_student26 test]# gzip etc.tar
gunzip
[root@westos_student26 test]# gunzip etc.tar.gz
文件大小对比
/etc/压缩速度: 瞬间
/etc/解压速度: 瞬间
bz2
bzip2
bunzip2
etc.tar.bz2
``
bzip2
[root@westos_student26 test]# bzip2 etc.tar
bunzip2
[root@node26 mnt]# bunzip2 westos.tar.bz2
etc.tar.bz2
[root@node26 mnt]# du -sh westos.tar.bz2
4.8M westos.tar.bz2
[root@node26 mnt]#
xz
xz
unxz
westos.tar.xz
[root@node26 mnt]# xz westos.tar
unxz
[root@node26 mnt]# unxz westos.tar.xz
westos.tar.xz
[root@node26 mnt]# du -sh westos.tar.xz
4.0M westos.tar.xz
[root@node26 mnt]#
tar z
[root@node26 mnt]# tar zcf etc.tar.gz /etc/
tar: Removing leading `/' from member names
[root@node26 mnt]# tar zxf etc.tar.gz
tar j
[root@node26 mnt]# tar jcf etc.tar.bz2 /etc/
tar: Removing leading `/' from member names
[root@node26 mnt]# tar jxf etc.tar.bz2
tar J
[root@node26 mnt]# tar Jcf etc.tar.xz /etc/
tar: Removing leading `/' from member names
[root@node26 mnt]# tar Jxf etc.tar.xz
三、rsync
rsync -r
复制目录
rsync -l
复制链接
rsync -p
复制权限
rsync -t
复制时间戳
rsync -o
复制拥有者
rsync -g
复制拥有组
rsync -D
复制设备文件
实验环境
在westos_node1中
watch -n1 ls -lR /root/Desktop
在rhel8中
[root@node126 ~]# touch /root/Desktop/file{1..5}
[root@node126 ~]# chmod 777 /root/Desktop/*
[root@node126 ~]# useradd westos
[root@node126 ~]# chown westos /root/Desktop/*
[root@node126 ~]# ln -s /root/Desktop/file1 /root/Desktop/file
『node26』执行:
执行命令效果:
rsync -r
##只同步目录中的文件 /目录/
[root@node26 ~]# rsync -r [email protected]:/root/Desktop/ /mnt
[email protected]'s password:
skipping non-regular file "file"
[root@node26 ~]#
rsync -r
同步目录本身及其目录中的文件 /目录
[root@node26 ~]# rsync -r [email protected]:/root/Desktop /mnt
[email protected]'s password:
skipping non-regular file "Desktop/file"
[root@node26 ~]#
rsync -l
复制链接
[root@node26 ~]# rsync -rl [email protected]:/root/Desktop/ /mnt
[email protected]'s password:
[root@node26 ~]#
rsync -p
复制权限
[root@node26 ~]# rsync -rlp [email protected]:/root/Desktop/ /mnt
[email protected]'s password:
rsync -o
复制拥有者
rsync -g
复制拥有组
[root@node26 ~]# rsync -rlpog [email protected]:/root/Desktop/ /mnt
[email protected]'s password:
rsync -t
复制时间戳
[root@node26 ~]# rsync -rlpogt [email protected]:/root/Desktop/ /mnt
[email protected]'s password:
rsync -D
复制设备文件
[root@node26 ~]# rsync -rD [email protected]:/dev/pts /mnt
[email protected]'s password: