zoukankan      html  css  js  c++  java
  • linux 压缩与打包

    一、文件管理--压缩打包

    1.压缩

    #压缩的好处主要有:
    	节省磁盘空间占用率
    	节省网络传输带宽消耗
    	网络传输更加快捷
    #压缩的坏处主要有:
    	压缩过程中很消耗服务器资源
    	压缩后文件仍然存在,占用空间
    

    2.常见的压缩类型

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

    3.gzip压缩命令

    #安装压缩命令
    [root@Centos7 ~]# yum install -y gzip
    
    #语法:
    Usage: gzip [OPTION]... [FILE]...
    
    #选项:
    -d:	解压
    
    #使用:
    1.压缩单个文件
    [root@Centos7 ~]# gzip services
    
    2.压缩多个文件
    [root@Centos7 ~]# gzip 1.txt 2.txt host.sh
    
    3.解压
    [root@Centos7 ~]# gzip -d 3.txt.gz
    
    4.查看包的内容
    [root@Centos7 ~]# zcat 2.txt.gz
    
    #总结:
    1.gzip压缩文件之后,源文件会删除
    2.使用zcat可以查看压缩包里面的内容
    3.gzip解压文件之后,压缩包文件会删除
    4.gzip压缩多个文件时,会分别将他们压缩,而不是压缩到一个文件
    5.gzip只能压缩文件,不能压缩文件夹
    	[root@Centos7 ~]# gzip dir
    	gzip: dir is a directory -- ignored
    6.打包文件在哪个目录,压缩后的包就在哪个目录下
    

    4.zip压缩命令

    #安装压缩命令
    [root@Centos7 ~]# yum install -y zip unzip
    
    #语法
    zip [options] [zipname] [file]...
    #搬家          行李箱    衣物
    
    # 压缩zip格式的文件
    #选项
    -r:	递归打包
    -q:	不输出打包过程
    
    # 解压zip格式的压缩包
    选项:  
    	-l		# 查看压缩包中的列表信息 
    	-q		# 静默输出,不显示解压的过程
    	-d		# 指定解压的目录
    
    
    #使用:
    1.压缩单个文件
    [root@Centos7 ~]# zip file.zip 3.txt 
      adding: 3.txt (deflated 80%)
      
    2.压缩多个文件
    [root@Centos7 ~]# zip file1.zip 3.txt 2.txt 1.txt 
      adding: 3.txt (deflated 80%)
      adding: 2.txt (deflated 80%)
      adding: 1.txt (deflated 80%)
     
    3.解压
    [root@Centos7 ~]# unzip file1.zip
    
    4.打包目录
    [root@Centos7 ~]# zip -r dir.zip dir/
      adding: dir/ (stored 0%)
      adding: dir/zengdao (stored 0%)
      adding: dir/qiudao (stored 0%)
      
    #总结:
    1.zip压缩文件之后,源文件仍然存在
    2.unzip解压后,如果目录下存在相同名字的文件,会询问是否重写、替换
    3.zip压缩多个文件时,会将所有内容打包成一个文件
    4.zip在压缩目录时,一定要加上-r参数,递归压缩,否则只压缩目录,不会带着目录下的文件
    5.zip打包,会将包打到当前所在目录下,或者打包到指定目录下
    	[root@Centos7 tmp]# zip /root/file.zip /root/1.txt /root/2.txt /root/3.txt
    6.zip打包时,如果是绝对路径打包,那么会删除/,然后打包
    

    5.tar归档

    #语法
    Usage: tar [OPTION...] [FILE]...
    
    #常用选项
    c   //创建新的归档文件
    f   //指定包文件名,多参数f写最后
    x   //对归档文件解包
    t   //列出归档文件里的文件列表
    v   //输出命令的归档或解包的过程
    C   //指定解压目录位置
    z   //使用gzip压缩归档后的文件(.tar.gz)
    j   //使用bzip2压缩归档后的文件(.tar.bz2)
    J   //使用xz压缩归档后的文件(tar.xz)
    X   //排除多个文件(写入需要排除的文件名称)
    h   //打包软链接
    P   //连带绝对路径打包
    --exclude   //在打包的时候写入需要排除文件或目录
    --exclude-from=		# 指定排除文件的列表
    tf		#指定要查看的压缩包名称 
    xf		#解压指定的压缩包	
    #用法:
    1.归档:
    [root@Centos7 ~]# tar cf file.tar 1.txt
    [root@Centos7 ~]# tar cf file2.tar 1.txt 2.txt 3.txt
    
    2.解压:
    [root@Centos7 ~]# tar xf file.tar
    
    3.查看包内文件
    [root@Centos7 ~]# tar tf file2.tar
    1.txt
    2.txt
    3.txt
    
    4.显示过程
    [root@Centos7 ~]# tar cvf file2.tar 1.txt 2.txt 3.txt
    1.txt
    2.txt
    3.txt
    [root@Centos7 ~]# tar xvf file2.tar
    1.txt
    2.txt
    3.txt
    
    5.指定目录位置解压
    [root@Centos7 ~]# tar xf file2.tar -C /tmp/
    
    6.归档并打包
    [root@Centos7 ~]# tar zcf file.tar.gz 3.txt
    
    7.排除文件打包
    [root@Centos7 ~]# tar zcf file.tar.gz ./* -X a.txt
    [root@Centos7 ~]# cat a.txt 
    1new.txt
    3.txt			#不打包的文件名字
    
    8.打包软连接(打包时将软连接变成了文件,将指向的文件内容写到了该文件中)
    [root@Centos7 ~]# ll
    total 135380
    lrwxrwxrwx 1 root root        5 Jun 24 16:49 1 -> 1.txt
    [root@Centos7 ~]# tar zchf ln.tar.gz 1
    [root@Centos7 ~]# tar tf ln.tar.gz 
    1
    [root@Centos7 ~]# tar xf ln.tar.gz -C /tmp/
    [root@Centos7 ~]# ll /tmp/
    total 1964
    -rw-r--r-- 1 root root 2009719 Jun 24 15:06 1
    
    
    #总结:
    1.tar压缩文件之后,源文件会保留
    2.使用tar tf可以查看压缩包里面的文件
    3.tar解压文件之后,压缩包文件不会删除
    4.tar压缩多个文件时,会将所有内容压缩到一个文件
    5.打包会将包打到当前所在目录下,或者打包到指定目录下
    6.打包时尽量到相对路径下打包,减少绝对路径的使用,打包带着绝对路径,解压时很危险
    
    #常用压缩组合
    tar czf     //打包tar.gz格式
    tar cjf     //打包tar.bz格式
    tar cJf     //打包tar.xz格式
    
    #常用解压组合
    tar zxf     //解压tar.gz格式
    tar jxf     //解压tar.bz格式
    tar xf      //自动选择解压模式
    tar tf      //查看压缩包内容
    

    6.练习题

    ------作业答案地址------------

    1.如何使用gzip命令对文件进行压缩、解压
    2.如何用zip命令对文件以及目录进行压缩、解压
    3.创建一个自己名字的文件至/opt目录
    4.打包opt整个目录,并命名test_opt.tar.gz
    5.查看打包好的test_opt.tar.gz里的文件
    6.将打包好的test_opt.tar.gz内容指定解压至/tmp目录
    7.打包etc目录下的所有文件,不要目录只要文件
    8.打包etc目录下的所有文件,排除passwd,shadow
    9.打包etc目录下的所有以p开头的文件
    10.打包etc目录下所有大于1M的文件
    

    tar命令与find结合使用

    [root@qls ~]# tar  czf  log1.tar.gz   `find  /var/log/  -type f  -name "*.log"`
    tar: Removing leading `/' from member names
    [root@qls ~]# tar  czf  log2.tar.gz   $(find  /var/log/  -type f  -name "*.log")
    tar: Removing leading `/' from member names
    
    # 此方法不行有坑 前面的结果一个一个交给后面命令会覆盖文件
    # 导致最后打包压缩的文件里面只有最后一次的数据
    [root@qls ~]# find  /var/log/  -type f  -name "*.log"  -exec  tar czf  log3.tar.gz  {}  ;
    
    
    
    [root@qls ~]# find  /var/log/  -type f  -name "*.log"  | xargs  tar czf   log4.tar.gz  
    tar: Removing leading `/' from member names
    
    
    
    # 当使用-exec时,  把find查找的文件是进行一个个赋值给{}的  
    #  xargs          把find命令查找出来的文件统一的赋值给了后面 
    
    
  • 相关阅读:
    开源的免费的对比工具
    win10 git bash 配置
    Java SSH 不使用终端也能调用环境变量中的指令
    MySQL WITH ROLLUP
    docker安装postgres
    开源的应用容器引擎
    清除浮动有哪几种方法
    js中的yield
    git的速学了解
    string/stringBuffer/StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/xiaolang666/p/13354948.html
Copyright © 2011-2022 走看看