Linux中tar文件压缩与解压

文件压缩与解压缩

一般什么情况下使用文件压缩?

备份数据,数据传输

节省磁盘空间

减少带宽使用

减少负载 减少IO操作

什么情况下进行压缩比较合适?

错过业务高峰期,由于文件的压缩会瞬间加大cpu的负载,所以如果压缩的文件过大,应在服务器业务低谷期进行数据压缩备份

tar 命令压缩与解压缩

语法格式

​ tar zcvf 压缩包名称.tar.gz 要压缩的内容 多个文件 多个目录

​ 如:tar zcvf 123.tar.gz 123.txt 456.txt 789.txt

参数: z gzip压缩

​ c 创建

​ v 显示过程

​ f 指定文件名称

​ x 解压缩

​ C 指定解压的位置

​ t 查看文件中的文件名称

打包文件 	
				[root@oldboyedu ~]# #打包当前的hosts文件
				[root@oldboyedu ~]# tar zcvf hosts.tar.gz hosts
				打包多个文件
				[root@oldboyedu ~]# tar zcvf hosts.tar.gz hosts oldboy.txt passwd 
				hosts
				oldboy.txt
				passwd

				打包目录 
				[root@oldboyedu ~]# tar zcf etc.tar.gz /etc
				tar: Removing leading `/' from member names		# 如果全路径打包会提示从成员中删除/ 保护系统安全
				
				不让提示 使用相对路径打包
				[root@oldboyedu ~]# cd /
				[root@oldboyedu /]# tar zcf etc.tar.gz etc
				[root@oldboyedu /]# 
				
				
				打包后的文件直接放在某个目录
				[root@oldboyedu /]# ll /opt/
				total 0
				[root@oldboyedu /]# tar zcf /opt/etc.tar.gz etc
				[root@oldboyedu /]# ll opt/
				total 10012
				-rw-r--r-- 1 root root 10248462 Nov  6 10:42 etc.tar.gz

解压

语法格式

​ tar xf 压缩包名称

​ 默认解压到当前目录下,可加参数-C来指定解压到哪个目录

​ tar xf 压缩包名称 -C 指定的目录

​ 如: tar xf 123.tar.gz -C /tmp/

查看压缩包中的文件名称

​ tar tf 123.tar.gz

批量打包文件中的内容

 ```python

批量打包文件中的内容
--exclude=PATTERN 排除不需要打包的文件
[root@oldboyedu ~]# tar zcvf test.tar.gz --exclude=all.hosts ./*
./all.tar.gz
./dir/
./dir/oldboy/
./hehe.txt
./hosts
./oldboy.txt
./passwd
./test.
./test.avi
./test.sh

			--exclude-from=FILE
			[root@oldboyedu ~]# tar zcvf test.tar.gz --exclude-from=exclude.txt ./*
			./exclude.txt
			./passwd
			./test.
			./test.avi
			./test.sh
 ```

zip 压缩和解压缩

打包
zip 包名字 需要打包的内容
解压
unzip 包名字
-d 指定解压的位置

posted @ 2020-11-11 21:51  杨梅杨梅  阅读(3710)  评论(0编辑  收藏  举报