zoukankan      html  css  js  c++  java
  • 07.Linux 压缩打包

    08.Linux压缩打包

    1. 文件打包与压缩概述

    1.1 什么是文件压缩

    • 将多个文件或目录合并成为一个特殊的文件【压缩
      包】。
    • 比如: 搬家...脑补画面 images

    1.2 为什么需要文件压缩

    • 当我们在传输大量的文件时,通常都会选择将该文件
      进行压缩,然后在进行传输。
      • 首先:压缩后的文件会比压缩前的文件小。一个
        28G 的文件夹压缩后能达到 6G
      • 其次:多个文件传输很慢,但单个文件传输会很
        快,同时还能节省网络的消耗。
      • (比如:搬家时,单行李往外拿和打包后往外拿?
        你懂我意思?)

    1.3 不同系统的压缩格式互通

    • Windows 的压缩包与 Linux 的压缩包能否互通?
      • 在 windows 系统下,我们接触最多的压缩格式是
        rar 或 zip
      • 在 Linux 系统上使用最多的压缩格式是 zip 和
        tar.gz
      • 在 Linux 上的压缩格式放在 windows 系统下都是
        可以正常打开的
    • 所以一般 Windows 和 Linux 互通通常选择 zip 格
      式;
    • 值得注意的是 Linux 不支持 Windows 下的 RAR 格式
      的压缩文件;
    • windows linux macos 支持 zip
    • tar.gz 属于linux系统特有一种格式;windows任然可
      以识别并解压;

    1.4 linux下常见的压缩包类型

    格式 压缩工具
    .zip zip压缩工具
    .gz gzip压缩工具,只能压缩文件.会删除原文件(通常配合tar使用)
    .bz bzip2压缩工具,只能压缩文件,会删除原文件(通常配合tar使用)
    .tar.gz 先使用tar命令归档打包.然后在使用gzip压缩
    .tar.bz2 先使用tar命令归档打包,在使用bzip压缩

    2.文件打包与压缩-gzip

    • gzip 仅能打包文件,并且打包后会删除源文件

    2.1 gzip压缩文件

    [root@xuliangwei ~]# yum install gzip -y
    [root@xuliangwei ~]# gzip file     # 对
    文件进行压缩
    [root@xuliangwei ~]# zcat file.gz    # 查
    看gz压缩后的文件
    [root@xuliangwei ~]# gzip -d file.gz  # 解
    压gzip的压缩包
    # bzip2
    [root@xuliangwei ~]# yum install bzip2 -y
    [root@xuliangwei ~]# bzip2 file     #
    对文件进行压缩
    [root@xuliangwei ~]# bzcat file.bz2    #
    查看gz压缩后的文件
    [root@xuliangwei ~]# bzip2 -d file.bz2  #
    解压gzip的压缩包
    

    2.2 gzip应用场景

    • 使用场景:当需要让某个配置文件不生效时,且又不想删除
    [root@xuliangwei ~]# gzip CentOS-Vault.repo
      # --> CentOS-Vault.repo.gz
    [root@xuliangwei ~]# zcat CentOS-
    Vault.repo.gz # --> 查看不想解压的压缩包文件内
    容
    

    3. 文件打包与压缩-zip

    • 使用 zip 命令可以对文件进行压缩打包,解压则需要
      使用 unzip 命令

    3.1 zip压缩文件

    1.默认最小化安装的操作系统,没有zip和unzip工具,所有需要安装

    [root@xuliangwei ~]# yum install zip unzip -y
    

    2.使用zip压缩文件

    [root@xuliangwei ~]# zip filename.zip
    filename
    
    1. 使用zip 压缩目录
    [root@xuliangwei ~]# zip -r dir.zip dir/
    

    3.2 unzip解压文件

    1.解压 zip 文件包, 默认解压至当前目录

    [root@xuliangwei ~]# unzip filename.zip
    

    2.解压zip包内容至 /opt 目录

    [root@xuliangwei ~]# unzip filename.zip -d /opt/
    
    [root@node ~]# wget https://ftp.yz.yamagata-u.ac.jp/pub/network/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.zip
    [root@node ~]# mkdir /app
    [root@node ~]# unzip apache-tomcat-9.0.50.zip -d /app/
    

    3.不解压压缩包,查看压缩包中的内容

    [root@xuliangwei ~]# unzip -l filename.zip
    ####
    
    

    4. 文件打包与压缩-tar

    • tar 命令是 linux 下最常用的压缩与解压缩,支持
      文件和目录的压缩归档;
    • tar 语法 : tar [-zjxcvfpP] filename
    选项 含义 选项 含义
    c 创建新的归档文件 z 使用gzip压缩归档后的文件(.tar.gz)
    x 对归档文件解包 j 使用bzip2压缩归档后的文件(.tar.bz2)
    t 列出归档文件里的文件列表 J 使用xz压缩归档后的文件(.tar.xz)
    v 输出命令的归档或解包的过程 C 指定解压目录位置
    f 指定包文件名,多参数f写最后 X 排除多个文件
    --exclude 排除文件或目录
    • 常用打包与压缩组合命令
    组合命令 组合含义 组合命令 组合含义
    czf 打包文件为tar.gz zxf 解压tar.gz格式文件
    cjf 打包文件格式为tar.bz格式 jxf 解压tar.bz格式文件
    cJf 打包文件为tar.xz格式 xf 智能解压文件
    tf 查看压缩包文件内容

    4.1 使用tar压缩文件

      1. 将文件或目录进行打包压缩
    #1.以gzip归档方式打包并压缩
    [root@xuliangwei ~]# tar czf test.tar.gz
    test/ test2/
    #2.打包/tmp下所有文件
    [root@xuliangwei /]# find /tmp -type f |
    xargs tar czf tmp.tar.gz
    [root@xuliangwei /]# tar czf tmp.tar.gz
    $(find /tmp -type f)
    # 将前者执行的命令作为参数传递后者tar这个命令使用;
    
    [root@node ~]# find ./ -maxdepth 1 -type f
    ! -name "*.gz" -a ! -name "*.zip" -a ! -
    name "*.bz2" | xargs tar czf
    root_all.tar.gz
    # bzip2格式
    [root@node ~]# tar cjf etc.tar.bz2 /etc/
    [root@node ~]# tar xf etc.tar.bz2 -C /tmp/
    [root@node ~]# ls /tmp/
    etc
    
    • 2.排除文件.并打包压缩

      #1.排除单个文件
      [root@xuliangwei /]# tar czf etc.tar.gz --
      exclude=etc/services etc/
      #2.排除多个文件
      [root@xuliangwei /]# tar czf etc.tar.gz --
      exclude=etc/services --exclude=etc/rc.local
      etc/
      #3.将需要排除的文件写入文件中
      [root@xuliangwei /]# cat paichu.list
      etc/services
      etc/rc.local
      etc/rc.d/rc.local
      #指定需要排除的文件列表, 最后进行打包压缩
      [root@xuliangwei /]# tar czfX etc.tar.gz
      paichu.list etc/
      
      
      #示例1----------------------
      [root@node ~]# tar tf etc.tar.gz | grep -E"hostname$|hosts$"
      etc/hosts
      etc/sysconfig/network-scripts/hostname
      etc/hostname
      [root@node ~]# tar czf etc2.tar.gz --
      exclude=etc/hosts --exclude=etc/hostname 
      /etc/
      [root@node ~]# tar tf etc2.tar.gz | grep -E "hostname$|hosts$"
      etc/sysconfig/network-scripts/hostname
      
      
      #示例1----------------------
      [root@node ~]# cat pai.txt
      etc/hostname
      etc/hosts
      etc/passwd
      etc/shadow
      [root@node ~]# tar czfX etc5.tar.gz pai.txt
      /etc/
      [root@node ~]# tar tf etc5.tar.gz |grep"hostname$"
      etc/sysconfig/network-scripts/hostname
      

    4.2 使用tar列出文件

    • 查看压缩包内容,但不解压
    [root@xuliangwei /]# tar tf test.tar.gz
    

    4.3 使用tar解压文件

    • 1.默认解压文件至当前目录
    # 1.解压至当前目录
    [root@xuliangwei /]# tar xf test.tar.gz
    
    • 2.指定解压内容存放至 /opt 目录
    [root@student ~]# tar xf /etc/local.tar.gz -C /tmp
    

    本文来自博客园,作者:GaoBeier,转载请注明原文链接:https://www.cnblogs.com/gao0722/p/15026549.html

  • 相关阅读:
    每日一练leetcode
    java 中 int与string的相互转化
    每日一练leetcode
    每日一题leetcode
    每日一练leetcode
    每日一练leetcode
    每日一题leetcode
    Three20在IOS6中不能正常使用 迎客
    苹果提供的支付功能接口 迎客
    ios随记 迎客
  • 原文地址:https://www.cnblogs.com/gao0722/p/15026549.html
Copyright © 2011-2022 走看看