谢烟客---------Linux之压缩解压缩及归档工具的使用

压缩工具

compress/uncompress:  .Z结尾

gzip/gunzip: .gz结尾

bzip2/bunzip2: .bz2结尾

xz/unxz: .xz后缀

压缩及归档工具: zip/unzip

归档工具: tar,cpio(特殊场景)

gzip/gunzip/zcat命令

1、LZ77压缩算法

2、压缩比不高:快速、高效

3、压缩/解压缩后删除原文件,为了节约空间

4、纯文本压缩,二进制压缩意义不大

5、压缩目录,-r

gzip [OPTIONS...] FILE
    -c --stdout  ## 压缩后的结果输出到标准输出。(目的不自动删除原文件)
    -d           ## 调用gunzip实现解压缩
    -r,--recursive  ## 递归压缩
    -#  1,9      ## 压缩比,越大,文件小,消耗的cpu时针周期越长

zcat FILE
     不展开文件,查看压缩文件的内容

bzip2/bunzip2/bzcat命令

1、LZ77,LZ78压缩算法,支持比gzip更高级的算法

2、压缩比不高:快速、高效

3、压缩/解压缩后删除原文件,为了节约空间

4、纯文本压缩,二进制压缩意义不大

5、不能压缩目录

bzip2 [OPTIONS....] FILE
    -k,keep   ## 不删除原文件,保持原文件
    -d         ## 调用bunzip2解压文件
    -#,1-9    ## 压缩级别。压缩比,越大,文件小,消耗的cpu时针周期越长
    
    -q,quiet  ## 静默模式,适用于脚本中

xz/unxz/xzcat命令

1、通用、主流的压缩工具

2、支持对传统.lzma格式的支持

3、压缩/解压缩后删除原文件,为了节约空间

4、不支持压缩目录

xz [OPTIONS...] FILE...
    -k
    -c,--stdout
    -d
    -#
    
    -q       ## 静默模式
    -F FORMAT  ## xz,lzma,raw 默认为xz
    
xzcat FILE  不展开压缩文件,查看文件解压的内容

zip/unzip命令,归档和压缩工具

1、通用的工具: VMS,Unix,MSDOS,OS/2,Windows(Windows 原生支持的压缩),Window 9x/NT/XP,Minix

2、tar和压缩工具共同的作用

3、要归档的文件名同解压后的名字。

4、压缩/解压缩后不会删除原文件

命令语法

zip 归档后的名字 要归档的文件名
    -d, --delete 不展开归档,删除归档中的文件
    -r , 递归归档
    -q, 静默模式

tar GNUtar归档工具

1、归档后,文件可能变大。(书放在箱子里,箱子的重量)

2、压缩/解压缩后不会删除原文件

3、压缩时,带调用压缩的选项。展开时,可以不带选项,自动识别。 -Jcf tar.xz

命令语法

tar [OPTIONS...]  FILE...
    -cvf FILE.tar 创建归档
    -xvf FILE.tar 展开归档
    -tvf FILE.tar 不展开归档,查看归档的文件
        -f FILE.tar 归档后的文件名
        -v ,verbose详细信息
    -C DIR   展开至指定目录 
    -j ,归档后,调用bzip2工具对文件压缩
    -J , 归档后,调用xz
    -z , 调用gzip
**命令可省略 - ,例如 -cvf 可写为 cvf

gzip/gunzip/zcat命令

使用示例

1、准备文件
[[email protected] ~]# cp /etc/{fstab,init.d/functions} /tmp/
[[email protected] ~]# ls -l /tmp/
-rw-r--r-- 1 root root   358 8月  13 18:08 fstab
-rw-r--r-- 1 root root 15131 8月  13 18:08 functions

2、压缩文件
[[email protected] ~]# cd /tmp
[[email protected] tmp]# ls
fstab  functions
[[email protected] tmp]# gzip fstab 
[[email protected] tmp]# ls
fstab.gz  functions  ## 原文件被删除

3、解压缩文件
[email protected] tmp]# gunzip fstab.gz 
[[email protected] tmp]# ls
fstab  functions     ## 解压的文件被删除

4、保留原文件,压缩
[[email protected] tmp]# gzip -c fstab > fstab.gz
[[email protected] tmp]# ls
fstab  fstab.gz  functions
[[email protected] tmp]# ls -l -h
-rw-r--r-- 1 root root 358 8月  13 18:08 fstab     ## 对比
-rw-r--r-- 1 root root 266 8月  13 18:10 fstab.gz  ## 对比
-rw-r--r-- 1 root root 15K 8月  13 18:08 functions

5、保留原文件,解压
[[email protected] tmp]# gzip -d -c fstab.gz > fstab.1
[[email protected] tmp]# ls -lh
-rw-r--r-- 1 root root 358 8月  13 18:12 fstab    ## 对比
-rw-r--r-- 1 root root 358 8月  13 18:12 fstab.1  ## 对比
-rw-r--r-- 1 root root 266 8月  13 18:10 fstab.gz
-rw-r--r-- 1 root root 15K 8月  13 18:08 functions

6、低压缩比和高压缩比解压的区别
1)低压缩:速度快,效率高
[[email protected] tmp]# gzip -c -1 fstab > fstab.1.gz
2)高压缩,速度慢,浪费cpu时钟周期,文件压缩后的文件小
[[email protected] tmp]# gzip -c -9 fstab > fstab.2.gz
[[email protected] tmp]# ls -lh fstab.1.gz fstab.2.gz 
-rw-r--r-- 1 root root 267 8月  13 18:14 fstab.1.gz
-rw-r--r-- 1 root root 266 8月  13 18:14 fstab.2.gz

7、压缩目录
[[email protected] tmp]# cp -r /etc/pam.d .
[[email protected] tmp]# gzip -r pam.d
[[email protected] tmp]# ls pam.d/
atd.gz            fingerprint-auth-ac.gz ....

8、解压目录
[[email protected] tmp]# gunzip pam.d/*
[[email protected] tmp]# ls pam.d/
atd          fingerprint-auth 

9、不展开文件查看文件的内容
[[email protected] tmp]# zcat fstab.gz 

#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:58:22 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=07151862-c2b9-45dc-bf7a-af8d2a6fa6c1 /                       ext3    defaults        1 1
/www/swap    swap    swap    defaults    0 0
[[email protected] tmp]#

bzip2/bunzip2/bzcat命令

使用示例

1、准备文件
[[email protected] tmp]# cp /var/log/messages /tmp
[[email protected] tmp]# ls /tmp/messages 
/tmp/messages

2、压缩文件
[[email protected] ~]# cd /tmp
[[email protected] tmp]# bzip2 messages 
[[email protected] tmp]# ls messages*
messages.bz2

3、解压缩文件
[[email protected] tmp]# bunzip2 messages.bz2 
[[email protected] tmp]# ls messages*
messages

4、保留原文件,压缩
[[email protected] tmp]# bzip2 -k messages 
[[email protected] tmp]# ls messages*
messages  messages.bz2

5、保留原文件,解压
[[email protected] tmp]# rm messages
rm:是否删除普通文件 "messages"?y
[[email protected] tmp]# ls messages*
messages.bz2
[[email protected] tmp]# bzip2 -d -k messages.bz2 
[[email protected] tmp]# ls messages*
messages  messages.bz2

6、低压缩比和高压缩比解压的区别
[[email protected] tmp]# bzip2 -1 -k messages 
[[email protected] tmp]# bzip2 -9 messages.new 
[[email protected] tmp]# ls -lh  messages*
-rw------- 1 root root 469K 8月  13 18:26 messages
-rw------- 1 root root  21K 8月  13 18:26 messages.bz2       ## 对比
-rw------- 1 root root  18K 8月  13 18:31 messages.new.bz2   ## 对比

7、不展开文件查看文件的内容
[[email protected] tmp]# bzcat messages.bz2 
Aug 12 10:00:29 izpo45bh60h6bsz pure-ftpd[5446]: ([email protected]) [INFO] New connection from 140.205.225.185
Aug 12 10:00:29 izpo45bh60h6bsz pure-ftpd[5446]: ([email protected]) [ERROR] Unable to read the indexed puredb file (or old format detected) - Try pure-pw mkdb

xz/unxz/xzcat命令

使用示例

1、准备文件
[[email protected] tmp]# rm /tmp/* -rf
[[email protected] tmp]# cp /etc/fstab /etc/issue .

2、压缩文件
[[email protected] tmp]# xz issue 
[[email protected] tmp]# ls
fstab  issue.xz

3、解压缩文件
[[email protected] tmp]# xz -d issue.xz 
[[email protected] tmp]# ls
fstab  issue

4、保留原文件,压缩
[[email protected] tmp]# xz -k issue 
[[email protected] tmp]# ls
fstab  issue  issue.xz
[[email protected] tmp]# xz -c fstab > fstab.xz
[[email protected] tmp]# ls
fstab  fstab.xz  issue  issue.xz

5、保留原文件,解压
[[email protected] tmp]# ls
fstab.xz  issue.xz
[[email protected] tmp]# xz -d -k fstab.xz issue.xz 
[[email protected] tmp]# ls
fstab  fstab.xz  issue  issue.xz

6、低压缩比和高压缩比解压的区别
-rw-r--r-- 1 root root 358 8月  13 18:40 fstab.bak

[[email protected] tmp]# xz -k -1 fstab 
[[email protected] tmp]# mv fstab fstab.bak
[[email protected] tmp]# xz -k -9 fstab.bak 
-rw-r--r-- 1 root root 324 8月  13 18:40 fstab.bak.xz
-rw-r--r-- 1 root root 332 8月  13 18:40 fstab.xz

7、lzma格式压缩
[[email protected] tmp]# ls
fstab 
[[email protected] tmp]# xz -F lzma fstab
[[email protected] tmp]# ls
fstab.bak.xz  fstab.lzma  issue  pam.d

8、不展开文件查看文件的内容
[[email protected] tmp]# xzcat fstab.xz 

#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:58:22 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=07151862-c2b9-45dc-bf7a-af8d2a6fa6c1 /                       ext3    defaults        1 1
/www/swap    swap    swap    defaults    0 0

9、静默模式,错误输出流不输出至标准输出
[[email protected] tmp]# xz fstab.xz
xz: fstab.xz: File already has `.xz‘ suffix, skipping
[[email protected] tmp]# xz -q fstab.xz

zip/unzip命令

使用示例

1、归档
[[email protected] tmp]# zip hello  fstab.bak.xz fstab.xz issue.xz 
[[email protected] tmp]# ls -lh
总用量 20K
-rw-r--r-- 1 root root  324 8月  13 18:40 fstab.bak.xz
-rw-r--r-- 1 root root  324 8月  13 18:40 fstab.xz
-rw-r--r-- 1 root root 1.2K 8月  13 18:54 hello.zip
-rw-r--r-- 1 root root   80 8月  13 18:39 issue.xz

2、展开
[[email protected] tmp]# ls
hello.zip  pam.d
[[email protected] tmp]# unzip hello.zip 
Archive:  hello.zip
 extracting: fstab.bak.xz            
 extracting: fstab.xz                
 extracting: issue.xz                
[[email protected] tmp]# ls
fstab.bak.xz  fstab.xz  hello.zip  issue.xz  pam.d

3、归档目录
[[email protected] tmp]# find . -maxdepth 1 -type f -delete
[[email protected] tmp]# ls
pam.d

[[email protected] tmp]# zip pam.d.zip pam.d

[[email protected] tmp]# ll
总用量 8
drwxr-xr-x 2 root root 4096 8月  13 18:43 pam.d
-rw-r--r-- 1 root root  162 8月  13 18:58 pam.d.zip

4、展开归档
[[email protected] tmp]# mv pam.d pam.d.bak
[[email protected] tmp]# unzip pam.d.zip 
Archive:  pam.d.zip
   creating: pam.d/
[[email protected] tmp]# ls
pam.d  pam.d.bak  pam.d.zip
[[email protected] tmp]# ls pam.d

5、归档目录
[[email protected] tmp]# zip pam.d.1.zip pam.d.bak/*
[[email protected] tmp]# zip -r pam.d.zip pam.d

6、展开归档
[[email protected] tmp]# unzip pam.d.zip 

7、静默模式:展开时/归档时不显示多余的信息
[[email protected] tmp]# unzip -q pam.d.zip

tar命令

使用示例

1、创建归档
[[email protected] tmp]# tar cvf hello.tar ./* 

2、展开归档
[[email protected] tmp]# tar xvf hello.tar

3、不展开,查看内容
[email protected] tmp]# tar tf hello.tar

4、展开至指定目录
[[email protected] tmp]# mkdir test
[[email protected] tmp]# tar xvf hello.tar -C test/
[[email protected] tmp]# ls test/
hello  pam.d  pam.d.1.zip  pam.d.bak  pam.d.zip

5、归档后以bzip2工具压缩
[[email protected] tmp]# tar jcf total.tar.bz2 ./*
total.tar.bz2

6、归档后以xz工具压缩
[[email protected] tmp]# tar Jcf total.tar.xz ./*
total.tar.xz

7、归档后以gzip工具压缩
[[email protected] tmp]# tar zcf total.tar.gz ./*
total.tar.gz
时间: 2024-08-01 10:45:37

谢烟客---------Linux之压缩解压缩及归档工具的使用的相关文章

linux之压缩解压缩及归档工具

压缩.解压缩及归档工具 1.压缩文件:XXXXXzip    filename 2.解压文件 Xunzip   filename.Xzip 3.查看压缩文件: Xcat   filename.Xzip 4.关于压缩工具 工具 文件扩展名 描述 Bzip2 .bz2 使用Burrows-Wheeler块排序文本压缩算法和Huffman编码 Compress .z 原Unix文件压缩工具,现在以你个很少使用 Gzip .gz Gun项目的压缩工具:使用Lempel-Ziv编码 Zip .zip Un

linux压缩、解压缩及归档工具

压缩.解压缩及归档工具 compress/uncompress: .Z    gzip/gunzip: .gz    bzip2/bunzip2: .bz2    xz/unxz: .xz    zip/unzip    tar, cpio 1.gzip/gunzip gzip [OPTION]... FILE ...            -d: 解压缩,相当于gunzip            -c: 将结果输出至标准输出:            -#:1-9,指定压缩比: zcat:不显式

压缩、解压缩及归档工具

压缩.解压缩及归档工具 压缩工具 compress /Z gzip /gz bzip2 /bz2 xz /xz   目前主流的压缩工具一般使用bzip2和gzip,xz是比较新的压缩工具,每一种压缩工具使用的压缩算法都存在差异,越新的压缩工具可能压缩的效果更好,但压缩时间可能比较长,会消耗大量的CPU计算能力.实际生产中应当具体情况而定. 打包压缩工具 zip 打包压缩   zip -r /backup/sysconfig /etc/sysconfig 解包解压缩   unzip sysconf

【linux_笔记】Linux_文件管理命令—压缩解压缩及归档基本工具

学习资源来自:www.magedu.com 学习记录过程中难免出现错误,如有发现,还望大神们指出. 示例操作部分有的与历史操作有关,如果先前的示例操作没有执行过的话,可能会有部分示例的操作无法执行.示例仅供参考.(示例见附件) 文件管理命令--压缩解压缩及归档基本工具 压缩.解压缩命令 压缩格式:gz, bz2, xz, zip, Z 压缩算法:算法不同,压缩比也会不同: 早期    压缩:        compress(压缩比很小): FILENAME.Z -- 压缩后的文件名    解压:

2016-8-28 压缩解压缩及归档 while脚本

文件管理命令――压缩解压缩及归档基本工具 压缩.解压缩命令 压缩格式:gz, bz2, xz, zip, Z 压缩算法:算法不同,压缩比也会不同: 早期    压缩:        compress(压缩比很小): FILENAME.Z ―― 压缩后的文件名    解压:        uncompress gzip.bzip2.xz只能压缩文件,并且默认压缩完成后删除源文件,zip可以压缩目录 gzip: .gz    gzip /PATH/TO/SOMEFILE:压缩完成后会删除原文件   

Linux的压缩/解压缩文件处理 zip & unzip

Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服务器没有安装zip包执行不了zip命令,但基本上都可以用tar命令的,实例如下: tar -zcvf /home/zdzlibs.tar.gz /home/zdz/java/zdzlibs/ 原文地址:https://www.cnblogs.com/huzixia/p/10393289.html

Linux 之 压缩解压缩

Linux中常见的压缩格式 .zip            .gz             .bz2           .tar.gz      tar.bz2 zip zip格式的压缩文件和windows通用,可以在跨平台压缩/解压缩 压缩:zip 压缩文件名  源文件 zip -r 压缩文件名 源目录 解压缩:unzip 压缩文件 gz 压缩:gzip 源文件         {压缩为gz格式的的压缩文件,压缩成功之后,源文件会消失} gzip -c 源文件 > 压缩文件       {压

linux打包压缩解压缩命令大全

linux打包压缩和解压缩命令大全 linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 和 .tgz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.tar.gz DirName 压缩多个

文件压缩、解压缩以及归档工具详解

一.简介 早期的有compress和uncompress,其对应的是.Z结尾的压缩格式文件:现在使用较多的有: gzip/gunzip,其对应的是.gz结尾的压缩格式文件: bzip2/bunzip2其对应的是.bz2结尾的压缩格式文件: xz/unxz其对应的是.xz结尾的压缩格式文件: zip/unzip其对应的是.zip结尾的压缩格式文件: 二.compress/uncompress 语法:compress [-dfvcVr] [-b maxbits] [file ...] OPTION: