• 压缩和打包


    张贺,多年互联网工作经验,担任过网络工程师、系统集成工程师、LINUX系统运维工程师,负责过大规模集群架构自动化运维管理工作,擅长Web集群架构与自动化运维,曾负责国内某电商运维工作。
    笔者微信:zhanghe15069028807

    压缩和打包

    LINUX的压缩通常称之为打包压缩,所谓的打包就是将多个文件放在一起,那放在一起,文件有没有变小呀?没有,就是一个打包嘛!需要对包进行压缩之后包才会缩小。

    总述

    常见的压缩格式:gz、bz2、xz、zip

    压缩工具 压缩后的格式 压缩语法 解压缩语法 不解压查看
    gzip .gz gzip FILE gunzip FILE zcat FILE
    bzip2 .bz2 bzip2 FILE bunzip FILE bzcat FILE
    xz .xz xz FILE unxz FILE xzcat FILE
    zip .zip zip file.zip <FILE/DIR> unzip file.zip

    gzip

    gzip FILE 压缩文件

    gzip –d FILE 解压缩

    gzip -# FILE 指定压缩比1-9的范围,默认是6

    zcat FILE 查看压缩后的内容,但并不解压缩

    [root@zhanghe ~]# ll -h  /etc/man.config 
    -rw-r--r--. 1 0 root 4.9K 2月  22 2013 /etc/man.config   #文件原本大小为4.9K
    [root@zhanghe ~]# rm -rf /tmp/*  
    [root@zhanghe ~]# cd /tmp 
    [root@zhanghe tmp]# cp /etc/man.config .
    [root@zhanghe tmp]# gzip man.config     #压缩 
    [root@zhanghe tmp]# ls man.config.gz          #压缩后自动指定一个.gz的后缀
    [root@zhanghe tmp]# ll -h man.config.gz      #压缩后2.2K 
     -rw-r--r--. 1 0 root 2.2K 8月  17 03:59 man.config.gz
    [root@zhanghe tmp]# gzip -d man.config.gz   #解压缩 
    [root@zhanghe tmp]# ls -lh man.config 
    -rw-r--r--. 1 0 root 4.9K 8月  17 03:59 man.config  #解压缩之后自动去掉后缀 
    [root@zhanghe tmp]# gzip -9 man.config   #指定压缩比
    [root@zhanghe tmp]# zcat man.config    在不解压缩情况下查看被压缩的内容 
    

    bzip2

    bzip2 FILE 压缩文件

    –d FILE 解压缩

    -# 指定压缩比1-9

    -k 保留源文件

    bzcat 查看压缩后的内容,但并不解压缩

    [root@zhanghe tmp]# bzip2 -9 man.config   压缩并指定压缩比
    [root@zhanghe tmp]# ls -lh man.config.bz2 
    -rw-r--r--. 1 0 root 2.2K 8月  17 03:59 man.config.bz2
    [root@zhanghe tmp]# bzip2 -d man.config.bz2 解压缩 
    [root@zhanghe tmp]# bzcat man.config.bz2    在不解压缩情况下查看被压缩的内容 
    [root@zhanghe tmp]# bzip2 -k man.config 使用-K选项保留源文件
    [root@zhanghe tmp]# ls man.config  man.config.bz2 
    

    xz

    xz –d FILE 或者unxz FILE 解压缩

    -# 指定压缩比1-9

    -k保留源文件

    xzcat查看压缩后的内容,但并不解压缩

    [root@zhanghe tmp]# xz man.config 
    [root@zhanghe tmp]# ls man.config.xz 
    [root@zhanghe tmp]# xz -d man.config.xz 
    [root@zhanghe tmp]# ls man.config 
    [root@zhanghe tmp]# xz -k man.config 
    [root@zhanghe tmp]# ls man.config  man.config.xz 
    [root@zhanghe tmp]# xzcat man.config.xz 
    

    zip

    zip <压缩后的文件名.zip> 要压缩的目录或文件

    不删除源文件,即打包又压缩

    unzip解压缩

    [root@zhanghe tmp]# zip man.zip man.config  
    [root@zhanghe tmp]# ls man.config  man.zip 
    [root@zhanghe tmp]# ll -h ./ 
    -rw-r--r--. 1 0 root 4.9K 8月  17 03:59 man.config 
    -rw-r--r--. 1 0 root 2.3K 8月  17 04:21 man.zip  
    [root@zhanghe tmp]# zip dev.zip dev/   #可以对目录使用
    [root@zhanghe tmp]# ls dev  dev.zip 
    [root@zhanghe tmp]# ll -dh ./* 
    drwxr-xr-x. 18 0 root 5.0K 8月  17 01:31 ./dev 
    -rw-r--r--.  1 0 root  158 8月  17 04:25 ./dev.zip 
    [root@zhanghe tmp]# unzip dev.zip    #解压 
    

    tar

    只打包不压缩,不删除源文件,可使用通配符

    -f FILE.tar 指定压缩后的文件名
    -c 创建归档文件
    --cattrs 归档时保留扩展属性

    [root@zhanghe tmp]# tar -cf dev.tar dev/   #一般cf连用,先指定归档后的名字,后指定文件 
    [root@zhanghe tmp]# ls dev  dev.tar     
    [root@zhanghe tmp]# tar -xf  dev.tar     #一般与xf连用 
    [root@zhanghe tmp]# ls dev  dev.tar 
    [root@zhanghe tmp]# tar -tf dev.tar  #-tf不展开归档,查看归档了哪些文件
    
    

    归档压缩

    tar –zcf 归档并调用gzip压缩,z指的是gzip

    tar –zxf解归档并调用gzip解压缩

    tar –jcf 归档并调用bzip2压缩,z指的是bzip2

    tar –jxf解归档并调用bzip2解压缩

    tar –Jcf 归档并调用XZ压缩,z指的是XZ

    tar –Jxf解归档并调用XZ解压缩

    其实只要要压缩的时候指明白压缩后的格式,解压缩的时候就不用指定用什么方式解压缩,如下:

    [root@zhanghe tmp]# tar -zcf dev.tar.gz  dev/
    [root@zhanghe tmp]# ls
    dev.tar.gz
    [root@zhanghe tmp]# tar -xf dev.tar.gz      
    [root@zhanghe tmp]# ls
    dev  dev.tar.gz
    

    总结

    gzip,bzip2,xz,仅压缩,使用方式都是一样的,非常的简单,并且都是不保留源文件,解压缩都是使用-d的选项。

    bzip2和``xz可以通过-k的选择来明确表示需要保存源文件,而且它们在压缩之后还可以通过cat命令进行查看.gzipzcat,而bzip2bzcat,而xzxzcat`。

    zip,不仅压缩,还可打包,可对目录使用,无论是压缩还是解压缩都保留源文件,zip比较落后,需要我们手工指定压缩后的文件名字,解压缩使用unzip,不用指定-d的选项。

    tar,仅打包不压缩,可在tar命令当中调用压缩的命令

    tar的基本语法与zip相似,并且也保留源文件.

  • 相关阅读:
    JS面试题(一)
    cookie、locakstorage、sessionstorage的区别
    BOM操作
    DOM表单(复选框)
    DOM表格操作
    Javascript的组成——EMACScript、DOM、BOM
    scrollto 到指定位置
    编写一个javscript函数 fn,该函数有一个参数 n(数字类型),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。
    使用bluebird解决promise兼容性问题
    Mac 更改/usr/bin 目录权限失败
  • 原文地址:https://www.cnblogs.com/yizhangheka/p/12323146.html
Copyright © 2020-2023  润新知