zoukankan      html  css  js  c++  java
  • Linux下的文件打包与压缩

    1. Linux下常见的压缩包类型

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

    2,文件打包与压缩-gzip

    • gzip只能打包文件,并且打包后会删除原文件

    2.1gzip压缩文件

    [root@Linux.net: ~]#yum install gzip -y 
    [root@Linux.net: ~]#gzip  NewFile  #对目标文件压缩   压缩后缀是.gz 
    [root@Linux.net: ~]#zcat NewFile.gz  #查看gzip压缩后的文件
    [root@Linux.net: ~]#gzip -d NewFile.gz  #解压gzip的压缩包
    

    2.2,bzip2

    [root@Linux.net: ~]#yum install bzip2 -y
    [root@Linux.net: ~]#bzip2 NewFile  #对文件压缩   压缩后的文件后缀。bz2
    [root@Linux.net: ~]#bzcat NewFile.bz2  #查看bzip2压缩后的文件
    [root@Linux.net: ~]#bzip2 -d NewFile.bz2  #解压压缩包
    

    当需要让某个配置文件不生效时,且又不想删除。 先gip该文件,然后zat查看即可

    3,文件打包与压缩-zip

    • 使用zip命令可以对文件与目录进行压缩解包,会保留原文件
      3.1,zip压缩与解压
    #zip压缩
    [root@Linux.net: ~]#yum install zip unzip -y 
    [root@Linux.net: ~]#zip NewFile.zip NewFile   #将NewFile压缩成NewFile.zip  NewFile文件会保留。
    [root@Linux.net: ~]#zip -r dir.zip dir/       #压缩目录 
    
    #unzip解压
    [root@Linux.net: ~]#unzip NewFile.zip    #解压  原压缩文件还会存在。默认解压到当前目录。
    [root@Linux.net: ~]#unzip NewFile.zip -d /opt/ #指定路径 将解压包解压到/opt/目录下。
    
    [root@Linux.net: ~]#unzip -l NewFile.zip    #不解压压缩包,查看压缩包的内容。
    

    4,文件打包与压缩-tar

    • tar 支持文件和目录的压缩归档。
    • 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.将文件或目录进行打包压缩
    
    [root@Linux.net: ~]#tar czf file.tar.gz file/ NewFile/  #将file和NewFile文件压缩成file.tar.gz,原文件会保留
    [root@Linux.net: ~]#tar tf file.tar.gz #查看压缩包文件
    
    [root@Linux.net: ~]#find /tmp/ -type f |xargs tar czf tmp.tar.gz  #将/tmp下的所有文件压缩打包tmp.tar.gz。 
    root@Linux.net: ~]#tar czf tmp.tar.gz $(find /tmp  -type f)   #将/tmp下的所有文件压缩打包为tmp.tar.gz
    #将前者执行的命令作为参数传递给后者tar这个命令使用。
    [root@Linux.net: ~]#find ./ -maxdepth 1 -type f ! -name "*.gz" -a ! -name "*.zip" -a ! -name "*.bz2"  | xargs tar zf root_all2.tar.gz
    
    #bizp2格式
    [root@Linux.net: ~]#tar cjf etc.tar.bz2 /etc/  #将etc/目录及其所有内容压缩到当前目录,压缩后的名字为etc.tar.bz2。
    [root@Linux.net: ~]#tar xf etc.tar.bz2 -C /tmp/  #将etc.tar.bz2解压到/tmp/下
    
    • 2.排除文件,并打包压缩
    #1,排除单个文件
    [root@Linux.net: ~]#tar czf etc1.tar.gz --exclude=etc/services etc/  
    #2,排除多个文件
    [root@Linux.net: ~]#tar czf etc2.tar.gz --exclude=etc/services  --exclude=etc/rc.local etc/
    #3,将需要排除的文件写入文件中
    [root@Linux.net: ~]#cat paichu.list
    etc/services
    etc/rc.local
    etc/rc.d/rc.local
    [root@Linux.net: ~]#tar czfx etc3.tar.gz paichu.list etc/
    #示例:
    [root@Linux.net: ~]#tar tf etc.tar.bz2 |grep -E "sudors|kernel" #过滤tc.tar.bz2
    

    4.2,使用tar列出文件

    • 查看压缩包内容,但不解压
    [root@Linux.net: ~]#tar tf etc.tar.gz
    

    4.3使用tar解压文件

    • 1,默认解压文件至当前目录
    [root@Linux.net: ~]#tar xf test.tar.gz
    
    • 2,指定解压内容存储至 /opt 目录
    [root@Linux.net: ~]#tar xf /root/etc.tar.bz2 -C /opt/
    
  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/qinghuani/p/15022859.html
Copyright © 2011-2022 走看看