Linux gzip压缩/解压 *.gz文件详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZYC88888/article/details/87099661

gzip 是linux中常见的压缩/解压工具,最常见的使用对象是*.gz格式的文件,这里简单介绍下它最常见的用法,

GZIP(1) General Commands Manual GZIP(1)

NAME
     gzip, gunzip, zcat - compress or expand files

SYNOPSIS
     gzip [ -acdfhklLnNrtvV19 ] [--rsyncable] [-S suffix] [ name ... ]
     gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ... ]
     zcat [ -fhLV ] [ name ... ]

OPTIONS
     -c --stdout --to-stdout 结果写到标准输出,原文件保持不变
     -d --decompress --uncompress 解压
     -k --keep 压缩或者解压过程中,保留原文件
     -r --recursive
     -t --test 检查压缩文件的完整性
     -v --verbose 显示每个文件的名子和压缩率
     -# --fast --best 取值从-1(最快)到-9(最好),默认是-6

示例1,压缩文件
原文件名为file1.txt,压缩后原文件消失,压缩后文件名为file1.txt.gz
root@ubuntu:/tmp# ls -l file1.*
-rw-r--r-- 1 root root 12383865 Aug 21 08:08 file1.txt
root@ubuntu:/tmp# gzip file1.txt
root@ubuntu:/tmp# ls -l file1.*
-rw-r--r-- 1 root root 134416 Aug 21 08:08 file1.txt.gz

示例2,解压文件
root@ubuntu:/tmp# gzip -d file1.txt.gz
root@ubuntu:/tmp# ls -lh file1.*
-rw-r--r-- 1 root root 12M Aug 21 08:08 file1.txt

示例3,压缩的时候,显示压缩率
root@ubuntu:/tmp# gzip -v file1.txt
file1.txt: 98.9% -- replaced with file1.txt.gz

示例4,一条命令压缩多个文件,压缩之后,是各自分开的:
root@ubuntu:/tmp# gzip file1.txt file2.txt
root@ubuntu:/tmp# ls -l
total 1348
-rw-r--r-- 1 root root 134416 Aug 21 08:08 file1.txt.gz
-rw-r--r-- 1 root root 392 Aug 21 08:15 file2.txt.gz

示例5,压缩过程中,保留原文件
root@ubuntu:/tmp# gzip -k file1.txt
root@ubuntu:/tmp# ls file1.*
file1.txt file1.txt.gz

示例6,压缩到标准输出中
可以连接两个文件
root@ubuntu:/tmp# cat file1.txt file2.txt | gzip > foo.gz
或者
root@ubuntu:/tmp# gzip -c file1.txt file2.txt > foo.gz
======================================

gzip
压缩后的格式为:*.gz

这种压缩方式不能保存原文件;且不能压缩目录

命令举例:
#压缩
[root@localhost tmp]# gzip buodo
[root@localhost tmp]# ls
buodo.gz
#解压
[root@localhost tmp]# gunzip buodo.gz 
[root@localhost tmp]# ls
buodo

tar
命令选项:
    -z(gzip)      用gzip来压缩/解压缩文件
    -j(bzip2)     用bzip2来压缩/解压缩文件
    -v(verbose)   详细报告tar处理的文件信息
    -c(create)    创建新的档案文件
    -x(extract)   解压缩文件或目录
    -f(file)      使用档案文件或设备,这个选项通常是必选的。

命令举例:
#压缩
[root@localhost tmp]# tar -zvcf buodo.tar.gz buodo
[root@localhost tmp]# tar -jvcf buodo.tar.bz2 buodo 

#解压
[root@localhost tmp]# tar -zvxf buodo.tar.gz 
[root@localhost tmp]# tar -jvxf buodo.tar.bz2

zip
与gzip相比:1)可以压缩目录; 2)可以保留原文件;

选项:

    -r(recursive)    递归压缩目录内的所有文件和目录

命令举例:
#压缩和解压文件
[root@localhost tmp]# zip boduo.zip boduo
[root@localhost tmp]# unzip boduo.zip

#压缩和解压目录
[root@localhost tmp]# zip -r Demo.zip Demo
  adding: Demo/ (stored 0%)
  adding: Demo/Test2/ (stored 0%)
  adding: Demo/Test1/ (stored 0%)
  adding: Demo/Test1/test4 (stored 0%)
  adding: Demo/test3 (stored 0%)
[root@localhost tmp]# unzip Demo.zip 
Archive:  Demo.zip
   creating: Demo/
   creating: Demo/Test2/
   creating: Demo/Test1/
 extracting: Demo/Test1/test4        
 extracting: Demo/test3  

bzip2
压缩后的格式:.bz2
参数
    -k    产生压缩文件后保留原文件

命令举例
#压缩
[root@localhost tmp]# bzip2 boduo
[root@localhost tmp]# bzip2 -k boduo

#解压
[root@localhost tmp]# bunzip2 boduo.bz2 
 

猜你喜欢

转载自blog.csdn.net/ZYC88888/article/details/87099661