美文网首页
linux笔记-压缩,解压缩和打包

linux笔记-压缩,解压缩和打包

作者: RaoZC | 来源:发表于2019-01-08 09:38 被阅读0次

    1. 压缩用途与技术

    两种压缩技术:

    1.1 将空的空间丢出来

    计算机系统使用byte单位来计算的,1byte=8bits,如果我们记录数字1,会占据一个bit,而剩下的7个bits会自动填上0,其实这些0是空的,压缩的时候,可以将这些0丢出来。但这些压缩后的文件,计算机并不能直接识别,因此需要使用解压缩技术,恢复到压缩前,也就是1,0,0,0,0,0,0,0,计算机才能识别。

    1.2 精简记录

    如果你的数据是11111111111111111111,共20个“1”,压缩的时候记录成“20个1”。

    2. linux系统主要的压缩和解压缩命令

    2.1 压缩文件主要的扩展名
    *.Z        compress程序压缩的文件
    *.zip      zip程序压缩的文件
    *.gz       gzip程序压缩的文件
    *.bz2      bzip2程序压缩的文件
    *.xz       xz程序压缩的文件
    
    2.2 压缩相关命令

    现在linux系统上主流的命令是gzip,bzip2和最新的zx。compress已经被淘汰,被gzip所取代。

    2.2.1 压缩和解压缩gzip,bzip2和xz

    gzip应用最广,可以解开compress,zip和gzip等软件所压缩的文件;
    bzip2就是为了取代gzip,获得更高的压缩比。用法和gzip基本一样。
    xz的压缩比较bzip2更高。
    语法如下:

    gzip -xx filename
    bzip2 -xx filename
    xz -xx filename
    
    main option:
    -c: output the data to the screen, keep original file unchanged
    -d: decompress
    -t: test the consistence of the compress file, error checking
    -k: keep input files (do not delete) during compression or decompression.
    -v(lower case): verbose. Display percentage reduction
    -number: Regulate the speed of compression. -1 or --fast indicates the fastest compressed method (but less compression), and -9 or --best indicated reverse. Default is -6
    

    压缩后原文件消失(可以用-k避免),如果要保留原文件,且

    2.2.2 读取压缩文件

    我们可以用zcat/zmore/zless来读取纯文本档压缩后的压缩文件,用bzcat/bzmore/bzless来读取.bz2文件,用xzcat/xzmore/xzless来读取.xz文件。

    zcat services.gz
    bzcat services.bz2
    xzcat services.xz
    
    2.2.3 搜索压缩文件中的数据
    zgrep -n 'http' services.gz
    bzgrep -n 'http' services.bz2
    xzgrep -n 'http' services.xz
    
    -n: show the number of line
    
    2.3 打包命令

    前面的几个命令只是压缩,并未将多个文件包成一个大文件。

    tar [ -z | -j | -J ] [cv] [-f 待建立的新档名] filename      #打包与压缩
    tar [ -z | -j | -J ] [cv] [-f 已有的档名]       #查看档名
    tar [ -z | -j | -J ] [cv] [-f 已有的档名] [-C目录]        #解压缩
    
    -z: gzip进行压缩解压缩
    -j: bzip2进行压缩解压缩
    -J: xz进行压缩解压缩
    
    -c: 打包
    -t: 查看档名
    -x: 解压缩,解打包
    
    -v: 显示正在处理的文件
    -f filename: 处理的文件
    -C: 释放的目的目录
    

    tar不会主动产生建立的档名,要自定义。推荐.tar.gz,.tar.bz2,*.tar.xz。

    打包某个目录,但不打包某些文件

    tar -jcv -f /ststem.tar.bz2 --exclude=/root/etc*
    

    以上就是linux系统中常用的压缩和打包的方法。

    相关文章

      网友评论

          本文标题:linux笔记-压缩,解压缩和打包

          本文链接:https://www.haomeiwen.com/subject/rtbyrqtx.html