zip压缩工具、tar打包、打包并压缩

zip压缩工具

zip可以压缩目录,压缩后原文件不删除。

先安装zip包,运行命令yum install -y zip

压缩文件 zip [压缩后文件名] [指定压缩文件]

[root@g_linux01 tmp]# zip 123 1.txt
  adding: 1.txt (deflated 37%)
[root@g_linux01 tmp]# ls
123.zip         systemd-private-cfeb657afc1f4ee3922e516bced89173-chronyd.service-0KXk3k   systemd-private-d49d765ffa054e279ee7df6a586b6eb0-vgauthd.service-3sA0lH
1.txt           systemd-private-cfeb657afc1f4ee3922e516bced89173-vgauthd.service-Q2kZn0   systemd-private-d49d765ffa054e279ee7df6a586b6eb0-vmtoolsd.service-IDX3Dr
2018-02-27.log  systemd-private-cfeb657afc1f4ee3922e516bced89173-vmtoolsd.service-thaBHF
shell           systemd-private-d49d765ffa054e279ee7df6a586b6eb0-chronyd.service-76UyZy

压缩目录文件 zip -r [压缩后文件名] [指定文件或目录可以是多个]

[root@g_linux01 tmp]# zip -r gg.zip 1.txt shell 
  adding: 1.txt (deflated 37%)
  adding: shell/ (stored 0%)
  adding: shell/lx1.sh (stored 0%)

解压缩unzip [解压文件],如果解压在同一目录下,会询问是否覆盖

安装yum install -y unzip

[root@g_linux01 tmp]# unzip 123.zip
Archive:  123.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: 1.txt     

unzip 123.zip -d /tmp/test 解压123.zip文件到/tmp/test下

查看文件列表:unzip -l 【文件名】但无法直接查看文件内容

[root@g_linux01 tmp]# unzip -l gg.zip
Archive:  gg.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       35  02-27-2018 13:45   1.txt
        0  02-27-2018 11:48   shell/
       45  02-27-2018 11:48   shell/lx1.sh
---------                     -------
       80                     3 files


tar打包

tar -cvf [打包后的文件名] 【打包文件1】【打包文件2】

-c创建 v可视化 f指定打包文件名

[root@g_linux01 test]# ls
1.txt  2.txt  lianxi
[root@g_linux01 test]# tar -cvf 12.tar 1.txt 2.txt
1.txt
2.txt
[root@g_linux01 test]# ls
12.tar  1.txt  2.txt  lianxi

解压 tar -xvf 【打包文件】(解压后会覆盖同目录下的相同文件)

查看打包文件列表:tar -tf 【打包文件】

[root@g_linux01 test]# tar -tf 12.tar
1.txt
2.txt

打包时过滤掉指定文件

tar -cvf 123.tar --exclude 1.txt --exclude 2.txt lianxi 1.txt 2.txt(也可以写成--exclude "*.txt")

[root@g_linux01 test]# tar -cvf 123.tar --exclude 1.txt --exclude 2.txt lianxi 1.txt 2.txt
lianxi/
[root@g_linux01 test]# ls
123.tar  12.tar  1.txt  2.txt  lianxi


打包并压缩

tar -zcvf 123.tar.gz 123 压缩打包gzip  

tar -zxvf 123.tar.gz       解压

tar -jcvf 123.tar.bz2 123 压缩打包bz2

tar -jxvf 123.tar.bz2

tar -Jcvf 123.tar.xz 123压缩打包xz

tar -Jxvf 123.tar.xz

一般情况下,gzip bz2 xz压缩效果越来越好,速度越来越慢

tar -tf 123.gz/123.bz2/123.xz 查看打包压缩文件列表

猜你喜欢

转载自my.oschina.net/u/3771583/blog/1626283