Linux命令gzip、bzip、tar、zip区别:

发布时间 2023-11-12 15:17:44作者: yun^-^

 

转载: 【彷徨 | Linux各个压缩命令(gzip,bzip2,zip,tar)的特点及区别详解】https://mbd.baidu.com/ma/s/3Wi3H7BE

1 : gzip

gzip只能压缩(解压)文件 , 不能压缩目录 ,后缀名为.gz , 而且不保留原文件

 解压使用gzip –d或者 gunzip

[root@localhost test_tar]# #压缩
[root@localhost test_tar]# gzip index.html
[root@localhost test_tar]# ls
1.txt.gz  2.txt  3.txt  index.html.gz  test

[root@localhost test_tar]# gzip 1.txt 2.txt
[root@localhost test_tar]# ls
1.txt.gz  2.txt.gz  3.txt  index.html.gz  test
解压使用gzip –d或者 gunzip [root@localhost test_tar]# #解压缩 [root@localhost test_tar]#
gzip -d 1.txt.gz 2.txt.gz [root@localhost test_tar]# ls 1.txt 2.txt 3.txt index.html.gz test

  [root@localhost test_tar]# gunzip 1.txt.gz 2.txt.gz
  [root@localhost test_tar]# ls
  1.txt 2.txt 3.txt index.html.gz test

2 : bzip2  

命令路径:/usr/bin/bzip2       执行权限:所有用户

作用:压缩(解压)文件,不能压缩目录压缩文件后缀为.bz2

语法:bzip2 [-k] [文件]  

-k:产生压缩文件后保留原文件(压缩比高)

-d 解压缩的参数(decompress)

解压使用bzip2 –d或者 bunzip2

eg:

[root@localhost test_tar]# bzip2 -k 1.txt  2.txt 
[root@localhost test_tar]# ll -h
总用量 78M
-rw-r--r-- 1 root root  41M 11月 11 10:27 1.txt
-rw-r--r-- 1 root root 6.1M 11月 11 10:27 1.txt.bz2
-rw-r--r-- 1 root root 6.5M 11月 11 10:26 2.txt
-rw-r--r-- 1 root root 1.2M 11月 11 10:26 2.txt.bz2

[root@localhost test]# bzip2 -d 1.txt.bz2  2.txt.bz2
[root@localhost test]# ll -h
总用量 47M
-rw-r--r-- 1 root root    0 11月 11 09:49 10.txt
-rw-r--r-- 1 root root  41M 11月 11 10:27 1.txt
-rw-r--r-- 1 root root 6.5M 11月 11 10:26 2.txt

  

3 : zip   

命令路径:/usr/bin/zip           执行权限:所有用户

作用:压缩(解压)文件(夹),压缩文件后缀为.zip

语法:zip 选项[-r]  [压缩后文件名称] [文件或目录]   

 -r  压缩目录   , 如果不加-r选项,压缩后的文件没有数据。

解压使用unzip  ,unzip   -d 解压到指定目录

eg:

 #压缩多份文件
[root@localhost test_tar]# zip total.zip  1.txt 2.txt 
[root@localhost test_tar]# zip -r test.zip test/
[root@localhost test_tar]# ll -h
总用量 60M
-rw-r--r-- 1 root root  41M 11月 11 10:27 1.txt
-rw-r--r-- 1 root root 6.5M 11月 11 10:26 2.txt
drwxr-xr-x 2 root root  145 11月 11 10:36 test
-rw-r--r-- 1 root root 1.7K 11月 11 10:40 test.zip
-rw-r--r-- 1 root root  14M 11月 11 10:43 total.zip

#解压
[root@localhost test]# unzip test.zip    
[root@localhost test]# unzip total.zip             
[root@localhost test]# ll -h
总用量 60M
-rw-r--r-- 1 root root  41M 11月 11 10:27 1.txt
-rw-r--r-- 1 root root 6.5M 11月 11 10:26 2.txt
drwxr-xr-x 2 root root  145 11月 11 10:36 test
-rw-r--r-- 1 root root 1.7K 11月 11 10:40 test.zip
-rw-r--r-- 1 root root  14M 11月 11 10:43 total.zip
[root@localhost test_tar]# unzip 1.zip -d ./test #解压到指定目录

 

4 : tar 

命令路径:/bin/tar           执行权限:所有用户

作用:文件、目录打(解)包,后缀是.tar

语法:tar [-zcf] 压缩后文件名  文件或目录

-c    建立一个压缩文件的参数指令(create),---打包

-x    解开一个压缩文件的参数指令(extract),---解包

-z    以gzip命令压缩/解压缩  , 文件格式.tar.gz(.tgz)

- j    以bzip2命令压缩/解压缩 ,文件格式.tar.bz2

-v    压缩的过程中显示文件(verbose)

-f     file 指定包名,必选项,最后一位参数

-t     查看打包文件的内容,不解压,查看文件内容

-C    指定解压路径

最常用压缩方式:   tar + gzip   ,  文件格式.tar.gz(.tgz)

解压     tar -zxvf   包名   文件名     

压缩的   tar -zcvf  包名.tar.gz   文件名     

  1. 单独的打包 ,解包   tar –cf   tar –xf
  2. 打包之后,进行压缩  gzip   bzip2
  3. 一步到位  tar –zcvf       tar -zxvf   或者 tar –jcvf       tar -jxvf 

 

     eg:tar -zcvf dir1.tar.gz  dir1   使用gzip将目录dir1压缩成一个打包并压缩文件dir1.tar.gz

     eg:tar -zxvf  dir1.tar.gz -C(大写的)  /root     解压到指定的/root目录

     eg: tar -cvf bak.tar  .    将当前目录的文件打包为bak.tar

     eg: tar -xvf bak.tar   解压

     eg: tar -zcvf bak.tar.gz  bak.tar   或 gzip  bak.tar    使用gzip将打包文件bak.tar压缩为bak.tar.gz

     eg: tar -jcvf bak.tar.bz2  bak.tar  或 bzip2 bak.tar   使用bzip2将打包文件bak.tar压缩为bak.tar.bz2

     eg: tar -rvf bak.tar /etc/password     将/etc/password追加文件到bak.tar中(r)

     eg:tar -cjvf test.tar.bz2  test  生成test.tar.bz2的压缩文件

     eg:tar -xjf test.tar.bz2        解压

  eg:   tar -tf   total.tar.gz   查看包total.tar.gz的内容

最常用:   tar + gzip

tar –zcvf 压缩   

tar –zxvf  解压

补充:

1,文件路径, 压缩包带文件路径

2,源文件是保留的,不会被删除