zip压缩工具
xz,bzip2,gzip都不支持压缩目录
zip可以压缩目录
压缩文件
zip 2.txt.zip 2.txt
[root@pantinglinux]# zip 2.txt.zip 2.txt
adding: 2.txt (deflated 99%)
[root@pantinglinux]# du -sh *
108K 2.txt
4.0K 2.txt.zip
压缩目录+文件
zip -r test.zip 2.txt test/
[root@pantinglinux]# zip -r test.zip 3.txt test/
解压文件
unzip test.zip
指定解压到某个目录
[root@pantinglinux]# unzip 2.txt.zip -d /tmp
Archive: 2.txt.zip
inflating: /tmp/2.txt
解压的时候,不能指定解压后的文件名称
如果指定了,就会先创建指定的文件名
[root@pantinglinux]# unzip 2.zip -d /1.txt
Archive: 2.zip
inflating: /1.txt/2.txt
creating: /1.txt/2/
查看文件列表,但是不可以查看文件内容
unzip -l 2.zip
tar打包工具
tar是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便复制或者移动。
tar [-zjxcvfpP] filename tar
-z:表示同时用gzip压缩
-j:表示同时用bzip2压缩
-J:表示同时用xz压缩
-x:表示解包或者解压缩
-t:表示查看tar包里的文件
-c:表示建立一个tar包或者压缩文件包
-v:表示可视化
-f:后面跟文件名,-f filename,表示压缩后的文件名filename,或者解压文件filename。如果是多个参数组合的情况下,-f参数写到最后面
-p:表示使用原文件的属性,压缩前什么属性压缩后还什么属性
-P:表示可以使用绝对路径
--exclude filename:表示在打包或压缩时,不要将filename文件包括在内
第一步在test目录下建立test111目录,然后在test111目录下建立2.txt文件,并将nihao写到2.txt。拷贝1.txt到test111目录下。接着,用tar命令把test111.tar(-f参数后紧跟打包后的文件名,然后再跟需要打包的目录或文件)。tar命令并不会使原文件消失。
[root@pantinglinux]# rm -f test111.tar
[root@pantinglinux]# ls
1.txt 2.txt 3.txt test111
[root@pantinglinux]# tar -cf test.tar test111 1.txt
[root@pantinglinux]# ls
1.txt 2.txt 3.txt test111 test.tar
先删除原来的test111目录,然后解包test.tar
[root@pantinglinux]# rm -rf test111
[root@pantinglinux]# ls
1.txt 2.txt 3.txt test.tar
[root@pantinglinux]# tar -xvf test.tar
test111/
test111/2.txt
test111/1.txt
1.txt
--exclude选项
[root@pantinglinux]# ls
1.txt test.tar
[root@pantinglinux]# tar -xvf test.tar
test111/
test111/2.txt
test111/1.txt
1.txt
[root@pantinglinux]# tar -cvf test111.tar --exclude 1.txt test111
test111/
test111/2.txt
test111.tar放到了--exclude选项前面。该选项除了排除文件,也可以排除目录
[root@pantinglinux]# mkdir test111/test222
[root@pantinglinux]# tar -cvf test111.tar --exclude test222 test111
test111/
test111/2.txt
test111/1.txt
打包的同时使用gzip压缩
tar命令支持打包时直接压缩,支持gzip压缩,bzip2压缩和xz压缩
[root@pantinglinux]# tar -czvf test111.tar.gz test111
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@pantinglinux]# ls
1.txt test111 test111.tar test111.tar.gz test.tar
-tf选项,可以查看包或者压缩包的文件列表
[root@pantinglinux]# tar -tf test111.tar.gz
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@pantinglinux]# tar -tf test.tar
test111/
test111/2.txt
test111/1.txt
1.txt
-zxvf选项,可以解压.tar.gz格式的压缩包
[root@pantinglinux]# rm -rf test111
[root@pantinglinux]# ls
1.txt test111.tar test111.tar.gz test.tar
[root@pantinglinux]# tar -zxvf test111.tar.gz
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@pantinglinux]# ls
1.txt test111 test111.tar test111.tar.gz test.tar
打包的同时使用bzip2压缩
[root@pantinglinux]# tar -cjvf test111.tar.bz2 test111
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@pantinglinux]# ls
1.txt test111 test111.tar test111.tar.bz2 test111.tar.gz test.tar
使用-tf选项来查看压缩包的文件列表
[root@pantinglinux]# tar -tf test111.tar.bz2
test111/
test111/2.txt
test111/1.txt
test111/test222/
使用-jxvf选项来解压.tar.bz2格式的压缩包
[root@pantinglinux]# tar -jxvf test111.tar.bz2
test111/
test111/2.txt
test111/1.txt
test111/test222/
使用zip压缩
[root@pantinglinux]# zip 1.txt.zip 1.txt
-bash: zip: 未找到命令
[root@pantinglinux]# yum install -y zip
[root@pantinglinux]# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 64%)
[root@pantinglinux]# zip test111.zip test111/*
adding: test111/1.txt (deflated 64%)
adding: test111/2.txt (stored 0%)
adding: test111/test222/ (stored 0%)
当目录下还有二级目录甚至甚至更多目录时,zip命令仅仅是把二级目录本身压缩。如果想要一并压缩二级目录下的文件,必须加上-r选项
[root@pantinglinux]# zip -r test111.zip test111/
updating: test111/1.txt (deflated 64%)
updating: test111/2.txt (stored 0%)
updating: test111/test222/ (stored 0%)
updating: test111/ (stored 0%)
解压缩zip,unzip命令
[root@pantinglinux]# unzip 1.txt.zip
-bash: unzip: 未找到命令
[root@pantinglinux]# yum install -y unzip