Linux6.1文件压缩和打包

常见压缩文件
Windows          .rar        .zip         .7z
Linux               .zip   ;     .gz   ;     .bz2   ;    .xz  ;  .tar.gz   ;    .tar.bz2 ;    .tar.xz

gzip压缩工具

gzip不能压缩目录

gzip  -数字(压缩级别)     filename          #压缩级别很少用,默认6
gzip      -d     filename.gz                     #解压缩
gunzip            filename.gz                   #解压缩

[root@chy002 /]# cd chyuanliu/
[root@chy002 chyuanliu]# touch 123.txt
[root@chy002 chyuanliu]# gzip 123.txt 
[root@chy002 chyuanliu]# ls
123.txt.gz
[root@chy002 chyuanliu]# file 123.txt.gz 
123.txt.gz: gzip compressed data, was "123.txt", from Unix, last modified: Thu Nov  9 04:29:48 2017
[root@chy002 chyuanliu]# gzip -d 123.txt.gz 
[root@chy002 chyuanliu]# vim 123.txt 
[root@chy002 chyuanliu]# gzip 123.txt 
[root@chy002 chyuanliu]# zcat 123.txt.gz 
asdfasdfasdf
asdfasdfasdf
asdfasdfasdf
asdfasdfasdf
asdfasdfasdf
[root@chy002 chyuanliu]# vim 12.txt
[root@chy002 chyuanliu]# gzip -c 12.txt > 123.txt.gz 
[root@chy002 chyuanliu]# ls
123.txt.gz  12.txt
[root@chy002 chyuanliu]# zcat 123.txt.gz 
123123123
123123123

bzip2压缩工具

bzip2和gzip真心几乎一样。
bzip2  [-zd] filename
-d 解压缩
-z  压缩      可加可不加。
bzip2  同样不可以压缩目录
bzcat  查看.bz2的文本文件内容

xz压缩工具

xz和gzip、bzip2用法几乎一样。同样不可以压缩目录。
xz   [-dz]   filename
-d   解压缩
-z   压缩。  可以不加
xzcat   查看.xz文本文件内容

zip压缩工具

#zip可以压缩目录,zip压缩后不删除源文件
[root@chy002 chyuanliu]# zip 123.txt.zip 123.txt      #zip  压缩后文件名    压缩前文件名
  adding: 123.txt (deflated 45%)
[root@chy002 chyuanliu]# ls
123.txt  123.txt.zip  12.txt
[root@chy002 chyuanliu]# mkdir 123
[root@chy002 chyuanliu]# cd 123
[root@chy002 123]# vi 123.txt
[root@chy002 123]# vi 2345.txt
[root@chy002 123]# cd ..
[root@chy002 chyuanliu]# ls
123  123.txt  123.txt.zip  12.txt  
[root@chy002 chyuanliu]# ls ./123
123.txt  2345.txt
[root@chy002 chyuanliu]# zip 123.zip 123
  adding: 123/ (stored 0%)
[root@chy002 chyuanliu]# ls
123  123.txt  123.txt.zip  123.zip  12.txt
[root@chy002 chyuanliu]# cp 123.zip /
[root@chy002 /]# unzip 123.zip 
Archive:  123.zip
   creating: 123/
[root@chy002 /]# cd 123
123/     123.zip  
[root@chy002 /]# cd 123/
[root@chy002 123]# ls
[root@chy002 chyuanliu]# rm -r 123.zip 
[root@chy002 chyuanliu]# zip -r 123.zip 123                #压缩目录,需要-r
  adding: 123/ (stored 0%)
  adding: 123/123.txt (deflated 6%)
  adding: 123/2345.txt (deflated 13%)
[root@chy002 chyuanliu]# rm -r /123
123/     123.zip  
[root@chy002 chyuanliu]# rm -r /123.zip 123/
[root@chy002 chyuanliu]# ls
123.txt  123.txt.zip  123.zip  12.txt
[root@chy002 chyuanliu]# unzip 123.zip
Archive:  123.zip
   creating: 123/
  inflating: 123/123.txt             
  inflating: 123/2345.txt            
[root@chy002 chyuanliu]# ls
123  123.txt  123.txt.zip  123.zip  12.txt
[root@chy002 chyuanliu]# !t
tree 123
123
├── 123.txt
└── 2345.txt

0 directories, 2 files
[root@chy002 chyuanliu]# unzip 123.zip -d /tmp/    #-d选项指定解压到哪个目录,不能指定文件
Archive:  123.zip
   creating: /tmp/123/
  inflating: /tmp/123/123.txt        
  inflating: /tmp/123/2345.txt       
[root@chy002 chyuanliu]# unzip -l 123.zip   #-l查看压缩包文件列表,无法查看内容
Archive:  123.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  11-09-2017 05:17   123/
       17  11-09-2017 05:16   123/123.txt
       24  11-09-2017 05:17   123/2345.txt
---------                     -------
       41                     3 files

tar打包

 tar打包工具,可以把目录打包成一个文件,整合所有文件为大文件整体,方便移动拷贝。

tar   -c   建立一个tar包或者压缩文件包
      -x   解包或者解压缩
      -f   后面跟文件名
      -v   可视化
      -t   查看tar包的文件
           -exclude filename   过滤指定文件
      -z   同时用gzip压缩
      -j   同时用bzip2压缩
      -J   XZ压缩

[root@chy002 chyuanliu]# ls
123  123.txt  123.txt.zip  123.zip  12.txt
[root@chy002 chyuanliu]# tar -cvf 123.tar 123/
123/
123/123.txt
123/2345.txt
[root@chy002 chyuanliu]# tar -cf 123.tar 123/
[root@chy002 chyuanliu]# ls
123  123.tar  123.txt  123.txt.zip  123.zip  12.txt
[root@chy002 chyuanliu]# tar -xvf 123.tar 123
123/
123/123.txt
123/2345.txt
[root@chy002 chyuanliu]# tar -cvf 1quanb.tar 123/ 123.txt 12.txt
123/
123/123.txt
123/2345.txt
123.txt
12.txt
[root@chy002 chyuanliu]# tar -tf 1quanb.tar 
123/
123/123.txt
123/2345.txt
123.txt
12.txt
[root@chy002 chyuanliu]# tar -cvf  chu234.tar --exclude 2345.txt 123/
123/
123/123.txt

打包并压缩

gzip压缩文件,不管解压缩还有压缩都有-z选项
tar   -zcvf  123.tar.gz  123
tar   -zxvf  123.tar.gz

bz2压缩文件,不管解压缩还有压缩都有-j选项
tar    -jcvf  123.bz2    123
tar    -jxvf  123.bz2

xz压缩文件,不管解压缩还有压缩都有-J选项
tar    -Jcvf   123.xz      123
tar    -Jxvf   123.xz

-tf选项可以看打包压缩文件列表

  

原文地址:https://www.cnblogs.com/chyuanliu/p/7806041.html