zoukankan      html  css  js  c++  java
  • linux中压缩解压缩命令

    目录

    • gzip
    • gunzip
    • tar(打包压缩)
    • tar(解包解压)
    • zip
    • unzip
    • bzip2
    • bunzip2

    gzip

    解释

    命令名称:gzip
    命令英文原意:GUN zip
    命令所在路径:/bin/gzip
    执行权限:所有用户
    功能描述:压缩文件
    

    语法

    # 压缩后文件格式.gz
    gzip [文件]
    

    示例

    # 压缩文件
    gzip /tmp/services
    
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls /tmp
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>  php-cgi.sock
    hsperfdata_root                                     services
    issue.hard                                          systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    issue.soft                                          test
    Japan                                               test.txt
    mysql.sock
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# gzip /tmp/services
    [root@izm5e2q95pbpe1hh0kkwoiz ~]#  ls -lh /etc/services
    -rw-r--r--. 1 root root 655K Jun  7  2013 /etc/services
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -lh /tmp/services.gz
    -rw-r--r-- 1 root root 133K Nov 29 17:45 /tmp/services.gz
    
    # 压缩前655k,压缩后133k
    

    gzip只能压缩文件,而且压缩后,原文件不在了

    gunzip

    解释

    命令名称:gunzip
    命令英文原意:GUN unzip
    命令所在路径:/bin/gunzip
    执行权限:所有用户
    功能描述:解压缩.gz文件
    

    语法

    # 解压缩.gz文件
    gunzip [文件]
    

    示例

    # 解压缩services.gz文件
    gunzip services.gz
    

    tar

    解释

    命令名称:tar
    命令所在路径:/bin/tar
    执行权限:所有用户
    功能描述:打包目录
    

    语法

    tar 选项[-zcf] [压缩后文件名] [被打包的目录]
        -c 打包
        -v 显示详细信息
        -f 指定文件名
        -z 打包同时压缩
    

    示例

    # 打包并压缩文件
    tar -czf Japan.tar.gz  /tmp/Japan
    tar -zcf Japan.tar.gz  /tmp/Japan
    
    # -f必须处于最后面,否则报错
    tar -zfc Japan.tar.gz  /tmp/Japan
    
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# tar -zfc Japan.tar.gz  /tmp/Japan
    tar: You must specify one of the `-Acdtrux' or `--test-label'  options
    Try `tar --help' or `tar --usage' for more information.
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# tar -zcf Japan.tar.gz  /tmp/Japan
    tar: Removing leading `/' from member names
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls
    boduo  Japan.tar.gz  lnmp-install.log  test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# rm Japan.tar.gz
    rm: remove regular file ‘Japan.tar.gz’? y
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# tar -czf Japan.tar.gz  /tmp/Japan
    tar: Removing leading `/' from member names
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls
    boduo  Japan.tar.gz  lnmp-install.log  test.txt
    
    

    tar

    解释

    命令名称:tar
    命令所在路径:/bin/tar
    执行权限:所有用户
    功能描述:解包解压缩目录
    

    语法

    tar 选项[-zcf] [压缩后文件名] [被打包的目录]
        -x 解包
        -v 显示详细信息
        -f 指定解压文件
        -z 解压缩
    

    示例

    # 解压缩目录(解压到当前目录)
    tar -zxvf Japan.tar.gz
    
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls
    boduo  Japan.tar.gz  lnmp-install.log  test.txt
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# tar -zxvf Japan.tar.gz
    tmp/Japan/
    tmp/Japan/cangjing/
    tmp/Japan/boduo/
    tmp/Japan/longze/
    [root@izm5e2q95pbpe1hh0kkwoiz ~]# ls
    

    zip

    解释

    命令名称:zip
    命令所在路径:/usr/bin/zip
    执行权限:所有用户
    功能描述:压缩文件或目录
    

    语法

    # 压缩后的文件格式为zip
    zip 选项[-r] [压缩后文件名] [被压缩的文件或目录]
        -r 压缩目录
    

    zip命令找不到

    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# zip aaa.zip test
    -bash: zip: command not found
    
    解决方法:
    yum -y install zip
    

    示例

    # 压缩/tmp目录下的test文件为aaa.zip
    zip aaa.zip test
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# zip aaa.zip test
      adding: test/ (stored 0%)
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls
    aaa.zip
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
    hsperfdata_root
    issue.hard
    issue.soft
    Japan
    mysql.sock
    php-cgi.sock
    services
    systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    test
    test.txt
    
    
    
    # 压缩/tmp下的Japan目录
    zip -r Japan.zip Japan
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# zip -r Japan.zip Japan
      adding: Japan/ (stored 0%)
      adding: Japan/cangjing/ (stored 0%)
      adding: Japan/boduo/ (stored 0%)
      adding: Japan/longze/ (stored 0%)
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls
    aaa.zip
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
    hsperfdata_root
    issue.hard
    issue.soft
    Japan
    Japan.zip
    mysql.sock
    php-cgi.sock
    services
    systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    test
    test.txt
    
    

    相对比gzip来说zip可以保留原文件,可以压缩目录

    unzip

    解释

    命令名称:unzip
    命令所在路径:/usr/bin/unzip
    执行权限:所有用户
    功能描述:解压.zip的压缩文件
    

    语法

    unzip [压缩文件]
    

    示例

    # 解压aaa.zip文件
    unzip aaa.zip
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# unzip aaa.zip
    Archive:  aaa.zip
       creating: test/
    

    bzip2

    解释

    命令名称:bzip2
    命令所在路径:/usr/bin/bzip2
    执行权限:所有用户
    功能描述:压缩文件
    

    语法

    # 压缩后的文件格式为.bz2
    bzip2 选项[-k] [文件]
        -k 差生压缩文件后保留原文件
    

    示例

    # 压缩文件
    bzip2 -k boduo
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch boduo
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# bzip2 -k boduo
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls
    aaa.zip
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
    boduo
    boduo.bz2
    hsperfdata_root
    issue.hard
    issue.soft
    Japan
    Japan.zip
    mysql.sock
    php-cgi.sock
    services
    systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    test
    test.txt
    
    
    
    
    # 将目录压缩为.bz2
    tar -cjf Japan.tar.bz2 Japan
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# tar -cjf Japan.tar.bz2 Japan
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls
    aaa.zip
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
    boduo
    boduo.bz2
    hsperfdata_root
    issue.hard
    issue.soft
    Japan
    Japan.tar.bz2
    Japan.zip
    mysql.sock
    php-cgi.sock
    services
    systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    test
    test.txt
    

    只能压缩文件

    bunzip2

    解释

    命令名称:bunzip2
    命令所在路径:/usr/bin/bunzip2
    执行权限:所有用户
    功能描述:解压缩
    

    语法

    bunzip2 选项[-k] [压缩文件名]
        -k 解压后保留原压缩文件
    

    示例

    # 解压文件
    bunzip2
    
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# bunzip2 boduo.bz2
    bunzip2: Output file boduo already exists.
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# rm boduo
    rm: remove regular empty file ‘boduo’? y
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# bunzip2 boduo.bz2
    [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls
    aaa.zip
    Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
    boduo
    hsperfdata_root
    issue.hard
    issue.soft
    Japan
    Japan.tar.bz2
    Japan.zip
    mysql.sock
    php-cgi.sock
    services
    systemd-private-9255c5ee9ec84f5987c1d9ba485e177e-ntpd.service-eKcnmD
    test
    test.txt
    
    
    
    # 解压目录
    tar -xjf Japan.tar.bz2
    
    # 不会报文件夹已存在的冲突,应该会合并
    
    
  • 相关阅读:
    CSS3自适应布局单位 —— vw,vh
    JS 设计模式四 -- 模块模式
    JS 设计模式三 -- 策略模式
    JS 设计模式
    JS 设计模式二 -- 单例模式
    JS 设计模式一 -- 原型模式
    JS 灵活使用 console 调试
    JS 优化条件语句的5个技巧
    JS 函数节流与防抖
    前端性能优化
  • 原文地址:https://www.cnblogs.com/eternityz/p/12332908.html
Copyright © 2011-2022 走看看