1.scp,rsync的使用
##scp用于远程复制从一个终端复制到另外一个终端
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ll
total 0
drwxr-xr-x. 2 root root 6 May 4 03:19 cdrom
[root@localhost mnt]# ls
cdrom
[root@localhost mnt]# touch file{1..10}
##创建10个文件
[root@localhost mnt]# ls
cdrom file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@localhost mnt]# scp file1 root@192.168.0.200:/mnt
/##这个是将本机的文件file1复制到远程机上去(类似于上传)
root@192.168.0.200's password:
file1 100% 0 0.0KB/s 00:00
[root@localhost ~]# scp -r /mnt/ root@192.168.0.200:/mnt/
##加上 -r代表复制的是目录
root@192.168.0.200'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
file10 100% 0 0.0KB/s 00:00
[root@localhost mnt]# scp -r root@192.168.0.200:/mnt/ /mnt/
##这是远程复制,将远程的目录复制到本机来(类似于下载)
root@192.168.0.200's password:
file1 100% 0 0.0KB/s 00:00
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
file10 100% 0 0.0KB/s 00:00
[root@localhost mnt]# ls
cdrom mnt
[root@localhost mnt]# cd mnt
[root@localhost mnt]# ls
file1 hgfs mnt
[root@localhost mnt]# cd mnt
[root@localhost mnt]# ls
cdrom file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
##当我们需要传输大量的文件的时候scp命令就显得不够了,因为传输速度太慢我们就可以使用rsync命令
[root@localhost mnt]# rsync -r /etc/ root@192.168.0.200 /mnt/
##将本地的etc复制到192.168.0.200主机的mnt目录下,但是他会忽略很多文件如下
skipping non-regular file "systemd/system/sysinit.target.wants/lvm2-lvmpolld.socket"
skipping non-regular file "systemd/system/sysinit.target.wants/lvm2-monitor.service"
skipping non-regular file "systemd/system/sysinit.target.wants/multipathd.service"
skipping non-regular file "systemd/system/sysinit.target.wants/rhel-autorelabel-mark.service"
skipping non-regular file "systemd/system/sysinit.target.wants/rhel-autorelabel.service"
skipping non-regular file "systemd/system/sysinit.target.wants/rhel-domainname.service"
skipping non-regular file "systemd/system/sysinit.target.wants/rhel-import-state.service"
skipping non-regular file "systemd/system/sysinit.target.wants/rhel-loadmodules.service"
skipping non-regular file "systemd/system/system-update.target.wants/systemd-readahead-drop.service"
skipping non-regular file "systemd/system/timers.target.wants/unbound-anchor.timer"
skipping non-regular file "systemd/system/vmtoolsd.service.requires/vgauthd.service"
skipping non-regular file "xdg/autostart/abrt-applet.desktop"
skipping non-regular file "xdg/systemd/user"
[root@localhost mnt]# ls
cdrom file1 file2 file3 file4 file5
[root@localhost mnt]# ll
total 0
drwxr-xr-x. 2 root root 6 Jun 8 02:30 cdrom
-rw-r--r--. 1 root root 0 Jun 8 02:30 file1
-rw-r--r--. 1 root root 0 Jun 8 02:30 file2
-rw-r--r--. 1 root root 0 Jun 8 02:30 file3
-rw-r--r--. 1 root root 0 Jun 8 02:30 file4
-rw-r--r--. 1 root root 0 Jun 8 02:30 file5
[root@localhost mnt]# chown cxg.cxg *
##将u权限和g权限都修改了
[root@localhost mnt]# ll
total 0
drwxr-xr-x. 2 cxg cxg 6 Jun 8 02:30 cdrom
-rw-r--r--. 1 cxg cxg 0 Jun 8 02:30 file1
-rw-r--r--. 1 cxg cxg 0 Jun 8 02:30 file2
-rw-r--r--. 1 cxg cxg 0 Jun 8 02:30 file3
-rw-r--r--. 1 cxg cxg 0 Jun 8 02:30 file4
-rw-r--r--. 1 cxg cxg 0 Jun 8 02:30 file5
[root@localhost mnt]# ll
##权限改为满权限
total 0
drwxr-xr-x. 2 cxg cxg 6 Jun 8 02:30 cdrom
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file1
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file2
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file3
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file4
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file5
[root@localhost mnt]# ln -s /mnt/mnt/mnt/file1 /mnt/mnt/mnt/file
##挂一个软连接
[root@localhost mnt]# ll
total 4
drwxr-xr-x. 2 cxg cxg 6 Jun 8 02:30 cdrom
lrwxrwxrwx. 1 root root 18 Jun 8 02:59 file -> /mnt/mnt/mnt/file1
-rwxrwxrwx. 1 cxg cxg 28 Jun 8 02:51 file1
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file2
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file3
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file4
-rwxrwxrwx. 1 cxg cxg 0 Jun 8 02:30 file5
[root@localhost mnt]# rsync -r /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##发现这个远程复制不能复制我们连接的文件,这个file文件被跳过了,同时我们发现复制过去的文件没有mnt目录名字,我们可以将这个命令改为[root@localhost mnt]# rsync -r /mnt/mnt/mnt root@192.168.0.200:/mnt/
,root前的mnt后面少个/就可以了
[root@localhost mnt]# rsync -lr /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##如果我们的在-s前加上l就可以复制的时候带上软连接了
[root@localhost mnt]# rsync -plr /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##再加上p就可以不忽略原本文件的权限,将原本文件的权限也复制过去
[root@localhost mnt]# rsync -oplr /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##再加上o就可以将用户原本归属也复制过去
[root@localhost mnt]# rsync -ogplr /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##再加上g就可以将组原本的归属也复制过去(注意这个组和用户复制的是他的uid号,它对应的同一个uid号在主机终端叫啥,复制过去就是对应的那个uid和gid的名称
[root@localhost mnt]# timedatectl
##如果两个终端的市区一致,比如一个上海一个纽约,我们想让复制文件过去的时候也保留原本的时间怎么办?
Local time: Mon 2020-06-08 10:20:45 CST
Universal time: Mon 2020-06-08 02:20:45 UTC
RTC time: Mon 2020-06-08 10:20:44
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@localhost mnt]# rsync -ogtplr /mnt/mnt/mnt/ root@192.168.0.200:/mnt/
##加上个t保留原本的时间
##我们一般设备文件也没法复制的时候同步的
[root@localhost ~]# rsync -r /dev/pts root@192.168.0.200 /mnt/
skipping non-regular file "pts/0"
skipping non-regular file "pts/ptmx"
##发现这个设备文件会被忽略,发现复制过去的只有pts空目录,里面的设备文件没有复制
[root@localhost ~]# rsync -rD /dev/pts root@192.168.0.200 /mnt/
##加上大写的D就可以将设备文件也复制到终端了
##scp和rsync的区别就是scp针对复制到的终端,如果那边有相同的文件,他会继续复制,而rsync是如果复制到的终端有相同的文件,他就会检测,然后跳过
2.文件的归档
##归档相当于装箱,把所有的东西都装在一s
[root@localhost Desktop]# tar cvf bin.tar /usr/bin##tar
相当于装箱指令,c表示创建,v表示显示过程,f表示创建的文件的名字 bin/tar为文件名,后面/usr/bin
为装箱的内容
##可以看到我们的桌面上已经装箱好了这个bin.tar
[root@localhost Desktop]# rm -fr *##我们删除掉bin.tar
[root@localhost Desktop]# tar cf bin.tar /usr/bin
##如果我们不想显示过程就可以去掉中间的v就行,可以看到没有过程了
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
[root@localhost Desktop]# tar cvf bin.tar /usr/bin
##这个是查看bin文件的内容
tar: Removing leading `/' from member names
/usr/bin/
/usr/bin/xzdec
/usr/bin/catchsegv
/usr/bin/xzdiff
/usr/bin/gencat
/usr/bin/xzfgrep
/usr/bin/getent
/usr/bin/xzegrep
/usr/bin/iconv
/usr/bin/geoiplookup
/usr/bin/ldd
[root@localhost Desktop]# touch westos
##我们现在在创建一个文件
[root@localhost Desktop]# tar rf bin.tar westos
##然后再用tar rf命令将westos复制到bin.tar里面
[root@localhost Desktop]# tar f bin.tar --delete westos
##这个是删除westos文件
[root@localhost Desktop]# tar rf bin.tar westos
##我们再把westos文件放进去
[root@localhost Desktop]# tar xf bin.tar
##xf命令是将bin.tar文件的内容都拿出来
[root@localhost Desktop]# tar f bin.tar --get westos
##我们要是只想拿出来westos文件,就可以去掉x然后加上 --get westos
[root@localhost Desktop]# cd /mnt/
[root@localhost mnt]# ls
pts westos1 westos2 westos3 westos4 westos5
[root@localhost Desktop]# tar cf mnt.tar /mnt/
##我们把mnt目录的
tar: Removing leading `/' from member names
##可以看到他会自动取消掉我们的/,[root@localhost Desktop]# tar cf mnt.tar mnt/,mnt的前的根没有了,就相当于我们打包到了当前目录,但是如果我们确实想让他打包到根下的mnt下怎么办
##通过tar --help后发现 -P符合我们的要求
[root@localhost Desktop]# tar Pcf mnt.tar /mnt/
##这个时候就相当于没有报错提示/被取消了,我们可以看到我们打包的也是绝对路径
[root@localhost Desktop]# tar tf mnt.tar
## 加上t表示查看文件内容
tar: Removing leading `/' from member names
/mnt/
/mnt/pts/
/mnt/pts/0
/mnt/pts/ptmx
/mnt/westos1
/mnt/westos2
/mnt/westos3
/mnt/westos4
/mnt/westos5
[root@localhost Desktop]# tar xf mnt.tar
##这个是解档到当前路径
[root@localhost Desktop]# tar xPf mnt.tar
##加上P就代表归档到绝对路径的根下的mnt下
[root@localhost Desktop]# tar xf mnt.tar -C /opt/
##这个可以加上-C
再加上要解档到的绝对路径
[root@localhost Desktop]# ls /opt/
##可以看到我们的mnt文件已经归档到了根下的opt下了
mnt rh
##但是打包不会让我们的文件大小改变的,我们这个时候就可以考虑压缩。
3.文件的压缩
linux里面默认支持的有zip,gz,bzip2,xz四个格式的压缩格式
[root@localhost Desktop]# tar cf bin.tar /usr/bin
##我们先归档一个文件bin.tar
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
[root@localhost Desktop]# du -sh bin.tar
##bin.tar的大小是138M
138M bin.tar
[root@localhost Desktop]# zip -r bin.tar.zip bin.tar
##后面的第二位开始bin.tar.zip是压缩后的文件名,然后bin.tar是压缩的文件
adding: bin.tar (deflated 59%)
[root@localhost Desktop]# du -sh bin.tar
138M bin.tar
[root@localhost Desktop]# du -sh bin.tar.zip
##看到压缩的文件只有64M,节省了很多空间
64M bin.tar.zip
[root@localhost Desktop]# rm -fr bin.tar
##我们先删除bin.tar
[root@localhost Desktop]# unzip bin.tar.zip
##然后使用unzip命令解压我们刚才压缩的文件
Archive: bin.tar.zip
inflating: bin.tar
##除了zip的方式还有gz的格式
[root@localhost Desktop]# gzip bin.tar
[root@localhost Desktop]# ls
bin.tar.gz bin.tar.zip mnt mnt.tar
[root@localhost Desktop]# du -sh bin.tar.gz
##gz格式压缩的只有56M大小
56M bin.tar.gz
[root@localhost Desktop]# gunzip bin.tar.gz
##这个gunzip是解压缩的方法
##bzip2格式的的压缩包
[root@localhost Desktop]# bzip2 bin.tar
##这个是bzip压缩的方法
[root@localhost Desktop]# du -sh bin.tar.bz2
##这个压缩后是51M
51M bin.tar.bz2
[root@localhost Desktop]# bunzip2 bin.tar.bz2
##解压缩bz2格式
[root@localhost Desktop]# du -sh bin.tar.xz
##这个是压缩后的xz文件只有36M
36M bin.tar.xz
[root@localhost Desktop]# unxz bin.tar.xz
##这是xz格式的解压缩
##我们可以打包和压缩一步完成吗?
我们利用man tar
命令查看帮助,可以看到不同的指令针对不同格式的文件
##因此我们一步完成只有gz(z),bz2(j),xz(J)格式的压缩包才可以,后面的括号的内容是指令
[root@localhost Desktop]# tar zcf etc.tar.gz /etc
tar: Removing leading `/' from member names
[
root@localhost Desktop]# ls
##可以看到tar。gz的文件已经生成了
bin.tar bin.tar.zip etc.tar.gz mnt mnt.tar
[root@localhost Desktop]# tar jcf etc.tar.bz2 /etc
tar: Removing leading `/' from member names
[root@localhost Desktop]# ls
##我们的bz2格式的压缩包也已经生成了
bin.tar etc.tar.bz2 mnt
bin.tar.zip etc.tar.gz mnt.tar
[root@localhost Desktop]# ls
##可以看到xz文件已经生成
bin.tar etc.tar.bz2 etc.tar.xz mnt.tar
bin.tar.zip etc.tar.gz mnt
##最后我们通过查看文件格式类型查看是否完全成功
[root@localhost Desktop]# file *
##file后面加上*代表查看文件的属性
bin.tar: POSIX tar archive (GNU)
bin.tar.zip: Zip archive data, at least v2.0 to extract
etc.tar.bz2: bzip2 compressed data, block size = 900k
etc.tar.gz: gzip compressed data, block size = 900k
etc.tar.xz: XZ compressed data
mnt: directory
mnt.tar: POSIX tar archive (GNU)
##同理解包解档一步完成怎么办呢?
将原来的zcf和jcf,Jcf中的c都改为x就可以了
例如:tar zxf etc.tar.gz
解压gz格式的文件