• Linux文件压缩与打包


    1.压缩与打包

    压缩:利用更节省空间的记录方式来记录文件数据,让文件占用的容量下降。优点:相同容量能够存储更多数据,传输时数据量降低,从而速度更快。

    打包:将多个文件打包为一个大文件,实际文件占用的容量没有下降。优点:让文件集合有了一个统一的名称。

    2.Linux常见的压缩文件拓展名

    *.gz  gzip压缩的文件

    *.bz2  bzip2压缩的文件

    *.tar  tar程序打包的文件

    *.tar.gz  tar程序打包,gzip压缩的文件

    *.tar.bz2  tar程序打包,bzip2压缩的文件

    3.gizp压缩命令

    使用最广的压缩命令,可以解开compress,zip,gzip压缩过的文件

    gzip  [-cdtv#]  [文件名]

    -c  :将压缩的数据输出到屏幕上,通过数据流重定向来处理

    -d  :解压缩参数

    -t  :用来检验压缩文件的一致性,看看文件是否有错误

    -v  :输出文件压缩比等信息

    -#  :压缩等级0-9,1-9压缩比增大速度变慢,默认为6

    xxx@xxx:~/test$ gzip -9 -v -c test.txt>test.gz
    test.txt:     38.0%
    xxx@xxx:~/test$ ls
    test.gz  test.txt
    xxx@xxx:~/test$ gzip -d test.gz
    xxx@xxx:~/test$ ls
    test  test.txt

    zcat可查看gzip压缩后的文本文件

    4.bzip2压缩命令

    比gzip压缩比更好的压缩命令

    bzip2   [-cdkzv#] 文件名

    -c  :将压缩过程中产生的数据输出到屏幕上

    -d  :解压缩的参数

    -k  :保留原文件

    -z  :压缩的参数

    -v  :可以显示出原文件/压缩文件的压缩比

    -#  :同gzip

    xxx@xxx:~/test$ bzip2 -kvz test.txt
      test.txt:  1.366:1,  5.858 bits/byte, 26.78% saved, 534 in, 391 out.
    xxx@xxx:~/test$ ls
    test.txt  test.txt.bz2

    5.tar打包命令

    多个文件打包成一个大文件,便于压缩(若不打包,压缩则是对每一个文件分别压缩,打包后便时对整体压缩)

    tar [-j|z]  [-cv]  [-f 新建的文件名]  filename   //打包与压缩

    tar    [-j|z]  [-tv]  [-f  新建的文件名]  filename  //查看文件名

    tar    [-j|z] [-xv]  [-f  新建的文件名] [-C 目录]  //解压缩

    参数:

    -c  :新建打包文件

    -t  :查看打包文件的内容中有那些文件名

    -x  :解打包或解压缩

    以上3条不可出现在同一串命令中

    -j  :通过bzip2的支持来压缩/解压缩

    -z  :通过gzip的支持来压缩/解压缩

    -v  :压缩/解压缩过程中显示正在处理的文件名

    -f  :后面接要处理的文件名

    -C  :特定目录解压

    -p  :保留备份数据的 原本权限与属性

    简单记录  

    gzip压缩:tar  -zcvf  filename.tar.gz  要压缩的文件目录或名称

    gzip解压:tar  -zxvf  filename.tar.gz -C 解压的目录

    gzip查看:tar  -zcvf  filename.tar.gz 

    bzip2压缩:tar  -jcvf  filename.tar.bz2  要压缩的文件目录或名称

    bzip2解压:tar  -jxvf  filename.tar.bz2 -C 解压的目录

    bzip2查看:tar  -jcvf  filename.tar.bz2 

    解开某一个文件

    tar -jtvf [打包文件] |grep '部分文件名'  //查看

    tar -jxvf [打包文件] [待解开的文件名]

    xxx@xxx:~$ tar -jcvf /home/lht/test2/test.tar.bz2 test
    test/
    test/world.txt
    test/test111.txt
    test/test333.txt
    test/444.txt
    test/hello.txt
    test/test.txt
    test/.hello.swp
    test/test222.txt
    xxx@xxx:~/test2$ tar -jtvf test.tar.bz2 |grep 'test/test'    //查看
    -rw-r--r-- xxx/xxx           0 2018-01-21 22:14 test/test111.txt
    -rw-r--r-- xxx/xxx           0 2018-01-21 22:14 test/test333.txt
    -rw-r--r-- xxx/xxx         534 2018-01-21 19:54 test/test.txt
    -rw-r--r-- xxx/xxx           0 2018-01-21 22:14 test/test222.txt
    xxx@xxx:~/test2$ tar -jxvf test.tar.bz2 test/test111.txt  //解单个文件
    test/test111.txt
    xxx@xxx:~/test2$ ls
    test  test.tar.bz2
    xxx@xxx:~/test2$ ls test
    test111.txt

    打包某目录,不包含某些文件

    tar -jcvf  [打包文件名]   --exclude=不需要的文件 [需要打包的文件]

    xxx@xxx:~/test2$ tar -jcvf test2.tar.bz2 --exclude=test/test* /home/xxx/test
    tar: 从成员名中删除开头的“//home/xxx/test/
    /home/xxx/test/world.txt
    /home/xxx/test/444.txt
    /home/xxx/test/hello.txt
    /home/xxx/test/.hello.swp    //只打包了不包含 test/test的文件

    仅备份新文件

    tar -jcvf  [打包的文件名]  --newer-mtime='时间'  [需要打包的文件]

    上述命令即可只打包某时间后改变过的文件

  • 相关阅读:
    OC与Swift的区别二(常量、变量、运算符)
    OC与Swift的区别一(文件结构)
    OC对象的归档及解档浅析
    OC单例模式的实现
    oc文件基本读写及操作
    IOS之沙盒(Sandbox)机制
    IOS开发之KVC与KVO简述
    SpringMVC控制器配置文件
    spring常用的连接池属性文件配置
    Struts2文件上传方式与上传失败解决方式
  • 原文地址:https://www.cnblogs.com/lht-record/p/8326016.html
Copyright © 2020-2023  润新知