zoukankan      html  css  js  c++  java
  • Linux学习笔记(7)Linux常用命令之压缩解压命令

    (1)gzip

    gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为:

    gzip [文件]

    压缩后的文件格式为.gz。

    例:将/etc目录下的services文件拷贝至/tmp目录,并采用gzip进行压缩。

    [root@localhost tmp]# cp /etc/services services
    
    [root@localhost tmp]# ls
    
    services
    
    [root@localhost tmp]# gzip services
    
    [root@localhost tmp]# ls
    
    services.gz

    注:gzip只能压缩文件,无法压缩目录;gzip命令不保留源文件,上例就可以看出。

    (2)gunzip

    gunzip命令用于解压缩文件,英文原意为GNU unzip,所在路径为/bin/gunzip,其语法格式为:

    gunzip [压缩文件]

    例:解压缩新创建的压缩文件

    [root@localhost tmp]# gunzip services.gz
    
    [root@localhost tmp]# ls
    
    services

    注:也可以使用gzip -d解压缩文件;gunzip执行后不保留源文件

    (3)tar

    tar命令用于打包目录,所在路径为/bin/tar,其语法格式为:

    tar 选项[-zcf] [压缩后的文件名] [目录]

    其中,-c选项表示打包;-v选项表示显示详细信息,-f选项指定文件名,-z选项表示打包的同时进行压缩,压缩后的文件格式为.tar.gz

    例:在/tmp目录下创建test/test1和test/test2目录,然后对test目录进行打包。

    [root@localhost tmp]# mkdir -p test/test1 test/test2
    
    [root@localhost tmp]# tar -czvf test.tar.gz test
    
    test/
    
    test/test2/
    
    test/test1/
    
    [root@localhost tmp]# ls -l
    
    total 636
    
    -rw-r--r--. 1 root root 641020 Jan  1 22:17 services
    
    drwxr-xr-x. 4 root root   4096 Jan  1 22:25 test
    
    -rw-r--r--. 1 root root    134 Jan  1 22:26 test.tar.gz

    tar解压缩的语法格式为:

    tar -xzvf [压缩文件]

    其中,-x选项表示解包,-v选项表示显示详细信息,-f选项表示指定解压文件,-z选项表示解压缩

    例:删除创建的test目录,然后解压缩test.tar.gz

    [root@localhost tmp]# rm -rf test
    
    [root@localhost tmp]# ls
    
    services  test.tar.gz
    
    [root@localhost tmp]# tar -xzvf test.tar.gz
    
    test/
    
    test/test2/
    
    test/test1/
    
    [root@localhost tmp]# ls
    
    services  test  test.tar.gz

    (4)zip

    zip命令用于压缩文件或目录,所在路径为/usr/bin/zip,其语法格式为:

    zip 选项[-r] [压缩后的文件名] [文件或目录]

    其中,-r选项表示压缩目录,压缩后的文件格式为.zip

    例:将services文件进行zip压缩:

    [root@localhost tmp]# zip services.zip services
    
      adding: services (deflated 80%)
    
    [root@localhost tmp]# ls
    
    services  services.zip  test  test.tar.gz

    注:zip格式在Linux和Windows中均存在;压缩比并不是很高

    (5)unzip

    unzip命令用于解压.zip格式的文件,所在路径为/usr/bin/unzip,其语法格式为:

    unzip [压缩文件]

    例:解压services.zip文件

    [root@localhost tmp]# unzip services.zip
    
    Archive:  services.zip
    
    replace services? [y]es, [n]o, [A]ll, [N]one, [r]ename:

    源文件存在,解压时询问是否替换。

    (6)bzip2

    bzip2是gzip的升级版本,也用于压缩文件,所在路径为/usr/bin/bzip2,其语法格式为:

    bzip2 选项[-k] [文件]

    其中-k选项产生压缩文件后保留源文件。压缩后的文件格式为.bz2

    使用tar命令可以将目录保存为.bz2文件,其格式为:

    tar 选项[-cjvf] [压缩后文件名] [目录]

    其中-j选项即表示压缩为.tar.bz2文件

    例:使用bzip2命令对services文件进行压缩,随后将test目录也进行压缩

    [root@localhost tmp]# bzip2 -k services
    
    [root@localhost tmp]# ls
    
    services  services.bz2  services.zip  test  test.tar.gz
    
    [root@localhost tmp]# tar -cjvf test.tar.bz2 test
    
    test/
    
    test/test2/
    
    test/test1/
    
    [root@localhost tmp]# ls
    
    services  services.bz2  services.zip  test  test.tar.bz2  test.tar.gz

    (7)bunzip2

    bunzip2命令用于解压缩.bz2文件,所在路径为/usr/bin/bunzip2,其语法格式为:

    bunzip2 选项[-k] [压缩文件]

    其中-k表示解压缩后保留源文件

    使用tar命令也可以解压缩.bz2的文件,其格式为:

    tar 选项[-xjvf] [压缩文件]

    例:删除services文件和test文件夹,然后分别解压缩:

    [root@localhost tmp]# rm -rf services test
    
    [root@localhost tmp]# ls
    
    services.bz2  services.zip  test.bz2  test.tar.gz
    
    [root@localhost tmp]# bunzip2 services.bz2
    
    [root@localhost tmp]# tar -xjvf test.tar.bz2
    
    test/
    
    test/test2/
    
    test/test1/
    
    [root@localhost tmp]# ls
    
    services  services.zip  test  test.tar.bz2  test.tar.gz
  • 相关阅读:
    eclipse使用maven时 java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
    js闭包泄漏-replaceThing问题(A surprising JavaScript memory leak found at Meteor)
    lua package path 设置方法
    Linux 网络相关命令
    is not in the sudoers file 解决(转)
    MySQL中的datetime与timestamp比较
    select random item with weight 根据权重随机选出
    [linux] 系统管理常用命令
    [python] 字符串与列表、字典的转换
    [python]pep8编码规范
  • 原文地址:https://www.cnblogs.com/mengrennwpu/p/4234859.html
Copyright © 2011-2022 走看看