9

Liunx运维(六)-文件备份与压缩命令

 3 years ago
source link: http://www.cnblogs.com/mrwhite2020/p/14162377.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

文档目录:

二、gzip:压缩或解压文件

三、zip:打包和压缩文件

四、unzip:解压zip文件

六、rsync:文件同步工具

---------------------------------------分割线:正文--------------------------------------------------------

预置相关文件数据:

BVjAf22.png!mobile

一、tar:打包备份

1、tar -zcvf www.tar.gz ./html/ #打包,z-giz的解压或压缩,v过程,c建立tar包,f指定压缩的文件名字 iE36nuz.png!mobile

2、tar -ztvf www.tar.gz #t查看压缩包中内容,z可省略,v可省略

uEBRbmQ.png!mobile

3、tar -zxvf www.tar.gz -C /tmp/ #C指定解压的路径,不在就解压在当前目录

IVrUbaf.png!mobile

4、tar -zcvf www.tar2.gz ./html/ --exclude=html/mrwhite/test #排除目录打包,目录结尾不要加/,否则会报错

mqQr6rA.png!mobile

5、tar -zcvf www.tar3.gz ./html/ --exclude=html/mrwhite/test --exclude html/mrwhite #排除两个目录

muq63u2.png!mobile

6、tar zcvfX paichu.tar.gz list.txt ./html/ #排除指定文件内目录

eaQnaiY.png!mobile

7、tar zcvfh local.tar2.gz rc.local #h打包实际的文件

NzQJjqv.png!mobile

8、注意:

如需要打包的目录为相对路径,--exclude只能接相对路径

如需要打包的目录为绝对路径,--exclude可以接绝对路径也能接相对路径

9、tar zcf etc.tar.gz `find . -type f` #打包所有普通文件,使用tar内 反引号中嵌套find语

e2eiqeJ.png!mobile

二、gzip:压缩或解压文件

1、gzip *.html #压缩所有.html结尾的文件为.gz,并且自动删除源文件

meEbmaV.png!mobile

2、gzip -l *.gz #不解压显示压缩文件的信息

qU7Z3q.png!mobile

3、gzip -dv *.gz #d为解压,v为过程

iemARb.png!mobile

4、gzip -c test.html > test.gz #-c压缩时重定向

A7JJNf6.png!mobile

5、gzip -dc test.gz > test1.html #-dc解压缩时重定向

RZB36v2.png!mobile

6、zcat test.txt.gz #直接查看压缩包文件

yUJ3y2.png!mobile

三、zip:打包和压缩文件

1、zip test.zip 123.txt 124.txt #将123,124文件压缩为test.zip

BzayIjf.png!mobile

2、zip -r test3.zip 12345/ #将12345文件夹以及下面的文件压缩成test3.zip

zq2iemA.png!mobile

3、zip -r test4.zip ./12345/ -x 12345/test.zip #压缩12345文件夹中内容但是排除掉其中的test.zip

iMRVVvf.png!mobile

四、unzip:解压zip文件

1、unzip -l test2.zip #看压缩文件列表

2ARjUv.png!mobile

2、unzip test.zip #解压缩文件

26fQfmU.png!mobile

3、unzip test.zip #如已有文件 ,y替换,n不替换,A所有文件替换,N所有文件不替换,r重命名

fiuqua.png!mobile

4、unzip -v test.zip # 解压缩并显示信息

3yaMJ3E.png!mobile

5、unzip -o test.zip #解压缩并不替换覆盖

7byiUfM.png!mobile

6、unzip -d /haha/ test.zip #解压缩并指定目录

BBJNv2I.png!mobile

五、scp:远程文件复制(适合全量)

1、scp testScp.txt 192.168.37.9:/root/mytest2020122702/ #远程拷贝

NNnIVbi.png!mobile

查看远程服务器

vUJvqib.png!mobile

2、scp -p testScp.txt 192.168.37.9:/root/mytest2020122702/ #文件属性不变

7VRjiqZ.png!mobile

远程服务器,文件属性一致

jamUziR.png!mobile

3、scp -r mytest202012* 192.168.37.9:/root/ #-r递归复制目录

qI3imij.png!mobile

查看远程主机对应目录

RbyaY3Y.png!mobile

4、scp -rp 192.168.37.9:/root/mytest20201228 . #远程拷贝文件至当前目录

MJzq6vU.png!mobile

六、rsync:文件同步工具(适合增量)

1、rsync -av data1/ data2/ #-a为递归+保持属性,等价于-rtopgDl -v为详细输出 ,带斜杠为复制文件下的内容

qIjmy2b.png!mobile

2、rsync -av data1 data2/ #不带斜杠为复制目录本身及目录下的文件

QNNRJvJ.png!mobile

3、rsync data1/test1 data2  #复制data1下文件至data2目录下

EvUB3my.png!mobile

4、rsync -av --delete data2/ data1/  #删除data1中内容使其与data2中保持一致

eIZZBzn.png!mobile

5、rsync -av 192.168.37.8:/root/mytestRsync/ mytestRsync2/ #远程拉取目录

uM7biaq.png!mobile

Yneuy2u.png!mobile

6、rsync -av mytestRsync  192.168.37.9:/root/   #推送至远程目录

BVry6zB.png!mobile

bEn6baY.png!mobile

7、rsync -av -e 'ssh -p 22' . 192.168.37.9:/root/mytestRsync/ #-e选项借助ssh隧道加密

6VNrEz6.png!mobile

YfqqEvN.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK