Linux中压缩与打包

1、Linux系统中常见的压缩命令(gzip、bzip2、xz和zip)
gzip命令:
[root@localhost test]# touch 1.txt //创建一个1.txt文件
-rw-r–r–. 1 root root 0 4月 1 05:31 1.txt
[root@localhost test]# gzip 1.txt
[root@localhost test]# ll
-rw-r–r–. 1 root root 26 4月 1 05:31 1.txt.gz //1.txt文件没有了,取而代之的是1.txt.gz
[root@localhost test]# mkdir 1 //创建一个目录1
[root@localhost test]# gzip ./1
gzip: ./1 is a directory – ignored //gzip命令无法作用于目录
gzip压缩命令有2种解压方式:
[root@localhost test]# gzip -d 1.txt.gz //gzip -d 和gunzip都可以解压
[root@localhost test]# ll
drwxr-xr-x. 2 root root 4096 4月 1 05:33 1
-rw-r–r–. 1 root root 0 4月 1 05:31 1.txt //压缩包没有了,取而代之的是原文件
注意:gzip命令的后缀名是.gz

bzip2命令:
[root@localhost test]# bzip2 1.txt
[root@localhost test]# ls
1 1.txt.bz2
[root@localhost test]# bzip2 1 //bzip2命令也不能作用于目录
bzip2: Input file 1 is a directory.
bzip2压缩命令有2种解压方式:
[root@localhost test]# bzip2 -d 1.txt.bz2 //bzip2 -d和bunzip2
[root@localhost test]# ls
1 1.txt
注意:bzip2命令的后缀名是.bz2

zip命令:
zip是压缩工具,unzip是解压缩工具
压缩文件: zip filename.zip filename
[root@localhost test]# zip 1.txt.zip 1.txt
adding: 1.txt (stored 0%)
-rw-r–r–. 1 root root 0 4月 1 01:04 1.txt //原文件1.txt也在,压缩后1.txt.zip
-rw-r–r–. 1 root root 160 4月 1 01:07 1.txt.zip
压缩目录: zip -r dir.zip dir/
[root@localhost test]# zip -r 1.zip 1 //压缩目录时,要加上参数-r
adding: 1/ (stored 0%)
[root@localhost test]# ll
drwxr-xr-x. 2 root root 4096 4月 1 01:04 1
-rw-r–r–. 1 root root 0 4月 1 01:04 1.txt
-rw-r–r–. 1 root root 160 4月 1 01:07 1.txt.zip
-rw-r–r–. 1 root root 154 4月 1 01:10 1.zip
注意:压缩目录时要加上-r,不加,就是压缩目录本身,而不是目录及目录下的所有内容
解压缩zip压缩包: unzip filename.zip
其他常见的用法:
unzip -l filename.zip 列出压缩包里面文件列表清单
unzip -d /tmp/ filename.zip 把压缩包解压到指定目录
unzip -l 文件名.zip 查看压缩包压缩了那些内容
unzip 文件名.zip 解压缩
总结:
gzip和bzip2只能作用于文件,对目录无效
zip能作用于文件和目录。

2、Linux系统中打包tar命令
tar打包工具:可以打包目录也可以打包文件
语法格式:tar [-zjxcvf] filename
打包:tar -cvf test.tar tset (test是文件也可以是目录)
例如:tar -cvf etc.tar /etc/
-c:表示建立包
-v:可视化
-f:压缩时跟“-f 文件名”,意思是压缩后的文件名为filename, 解压时跟 “-f 文件名”,意思是解压filename.
-u: 在包里增加一个新文件,比如 tar -u 1.txt -vf test.tar
tar -u 1 -vf etc.tar

解包: tar -xvf test.tar
-x:解包或者接压缩的意思
不管是打包还是解包,原来的文件是不会删除的,
但解包是它会覆盖当前已经存在的文件或者目录。
-C: 解压到指定目录,比如 tar -C /tmp/ -xvf test.tar (注意大小写)
打包是同时使用gzip压缩:tar -czvf 1.tar.gz 1其中1可以是文件也可以是目录
-z:打包同时使用gzip压缩
解压:tar -xzvf 1.tar.gz
打包的同时使用bzip2压缩:tar -cjvf 1.tar.bz2 1
-j:打包同时使用bzip2压缩
解压:tar -jxvf 1.tar.bz2
–exclude可以在打包时候排除某些文件或者目录
tar –exclude 1.txt -czvf 1.tar.gz dir/
cd /data // data目录下有我们要拷贝的目标文件目录 test

3、tar的一个有意思的用法:用tar远程拷贝数据
tar cvf - test| ssh 10.0.1.11 “cd /copy1/; tar xvf -”
首先将要拷贝的目录test打包,”-” 代表标准输出,然后再ssh 到目标主机 10.0.1.11 ,
运行相应的命令。其中tar xvf - 意思是,将前面的标准输出内容作为解包的对象。
总结:其实就是想拷贝的目录先打包,然后将打包好的文件拷贝到目标主机, 最后在目标主机上解包。只不过,我们用一条命令实现了边打包边解包的过程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值