Linux文件归档与压缩命令

一、压缩与解压命令
1、gzip/gunzip/zcat
  gzip [OPTION] FILE
    -d 解压缩,相当于gunzip
    -# 指定压缩比,默认值为6,值越大压缩比越大,可选区间1-9
    -c 保留原文件
     gzip -c file_name > file_name.gz
 压缩:
   gzip FILE_NAME (FILE_NAME是要压缩的文件名)
 解压:
  gunzip FILE_NAME.gz (FILE_NAME.gz是要解压缩的文件名)
  gzip -d FILE_NAME.gz (FILE_NAME.gz是要解压缩的文件名)
 不解压直接查看文件内容:
  zcat FILE_NAME.gz

[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# 
[root@linux4 zip]# gzip yum.log 
[root@linux4 zip]# ls
yum.log.gz
[root@linux4 zip]# gunzip yum.log.gz 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# gzip -9 yum.log 
[root@linux4 zip]# ls
yum.log.gz
[root@linux4 zip]# gzip -d yum.log.gz 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# gzip -c yum.log > yum.log.gz
[root@linux4 zip]# ls
yum.log  yum.log.gz
[root@linux4 zip]# zcat yum.log.gz 
Jul 29 11:03:55 Installed: tree-1.6.0-10.el7.x86_64
Sep 07 10:59:39 Installed: mpfr-3.1.1-4.el7.x86_64
Sep 07 10:59:39 Installed: libmpc-1.0.1-3.el7.x86_64
Sep 07 10:59:39 Installed: apr-1.4.8-7.el7.x86_64
Sep 07 10:59:39 Installed: libquadmath-4.8.5-44.el7.x86_64
Sep 07 10:59:39 Installed: m4-1.4.16-10.el7.x86_64
Sep 07 10:59:39 Installed: autoconf-2.69-11.el7.noarch
Sep 07 10:59:39 Installed: automake-1.13.4-3.el7.noarch
Sep 07 10:59:39 Installed: apr-util-1.5.2-6.el7.x86_64
Sep 07 10:59:39 Installed: zip-3.0-11.el7.x86_64
[root@linux4 zip]# 

2、bzip2/bunzip2/bzcat
  bzip2 [OPTION]…FILE
   -d: 解压缩
   -#: 指定压缩比,默认是6,数字越大压缩比越大(1-9)
   -k: keep,保留原文件
 压缩:
  bzip2 FILE_NAME
 解缩:
  bzip2 -d FILE_NAME.bz2
  bunzip2 FILE_NAME.bz2
 不解缩直接查看文件内容:
  bzcat FILE_NAME.bz2

[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# bzip2 yum.log 
[root@linux4 zip]# ls
yum.log.bz2
[root@linux4 zip]# bunzip2 yum.log.bz2 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# bzip2 -9 yum.log 
[root@linux4 zip]# ls
yum.log.bz2
[root@linux4 zip]# bzip2 -d yum.log.bz2 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# bzip2 -k yum.log 
[root@linux4 zip]# ls
yum.log  yum.log.bz2
[root@linux4 zip]# bzcat yum.log.bz2 
Jul 29 11:03:55 Installed: tree-1.6.0-10.el7.x86_64
Sep 07 10:59:39 Installed: mpfr-3.1.1-4.el7.x86_64
Sep 07 10:59:39 Installed: libmpc-1.0.1-3.el7.x86_64
Sep 07 10:59:39 Installed: apr-1.4.8-7.el7.x86_64
Sep 07 10:59:39 Installed: libquadmath-4.8.5-44.el7.x86_64
Sep 07 10:59:39 Installed: m4-1.4.16-10.el7.x86_64
Sep 07 10:59:39 Installed: autoconf-2.69-11.el7.noarch
Sep 07 10:59:39 Installed: automake-1.13.4-3.el7.noarch
Sep 07 10:59:39 Installed: apr-util-1.5.2-6.el7.x86_64
Sep 07 10:59:39 Installed: zip-3.0-11.el7.x86_64
[root@linux4 zip]# 

3、xz/unxz/xzcat
  lzma/unlzma/lzcat(略)
  xz [OPTION]… FILE
    -d: 解压缩
    -#: 指定压缩比,默认是6,数字越大压缩比越大(1-9)
    -k: keep,保留原文件
 压缩:
  xz FILE_NAME
 解缩:
  xz -d FILE_NAME.xz
  unxz FILE_NAME.xz
 不解缩直接查看文件内容:
  xzcat FILE_NAME.xz

[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# xz yum.log 
[root@linux4 zip]# ls
yum.log.xz
[root@linux4 zip]# unxz yum.log.xz 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# xz -2 yum.log 
[root@linux4 zip]# ls
yum.log.xz
[root@linux4 zip]# xz -d yum.log.xz 
[root@linux4 zip]# ls
yum.log
[root@linux4 zip]# xz -k yum.log 
[root@linux4 zip]# ls
yum.log  yum.log.xz
[root@linux4 zip]# xzcat yum.log.xz 
Jul 29 11:03:55 Installed: tree-1.6.0-10.el7.x86_64
Sep 07 10:59:39 Installed: mpfr-3.1.1-4.el7.x86_64
Sep 07 10:59:39 Installed: libmpc-1.0.1-3.el7.x86_64
Sep 07 10:59:39 Installed: apr-1.4.8-7.el7.x86_64
Sep 07 10:59:39 Installed: libquadmath-4.8.5-44.el7.x86_64
Sep 07 10:59:39 Installed: m4-1.4.16-10.el7.x86_64
Sep 07 10:59:39 Installed: autoconf-2.69-11.el7.noarch
Sep 07 10:59:39 Installed: automake-1.13.4-3.el7.noarch
Sep 07 10:59:39 Installed: apr-util-1.5.2-6.el7.x86_64
Sep 07 10:59:39 Installed: zip-3.0-11.el7.x86_64
[root@linux4 zip]# 

4、zip/unzip (既能压缩又能归档)
  压缩:
   zip file_name.zip FILE_NAME
  解压:
   unzip file_name.zip

[root@linux4 zip]# ls
boot.log  vmware.log  yum.log
[root@linux4 zip]# zip mylog.zip *.log
  adding: boot.log (stored 0%)
  adding: vmware.log (deflated 65%)
  adding: yum.log (deflated 78%)
[root@linux4 zip]# ls
boot.log  mylog.zip  vmware.log  yum.log
[root@linux4 zip]# rm -rf *.log
[root@linux4 zip]# unzip mylog.zip 
Archive:  mylog.zip
 extracting: boot.log                
  inflating: vmware.log              
  inflating: yum.log                 
[root@linux4 zip]# ls
boot.log  mylog.zip  vmware.log  yum.log
[root@linux4 zip]# 

二、归档(打包)命令
1、tar命令(tar命令的选项可以不带-)
 tar [OPTION]…FILE…
  -c 创建归档
   -c -f /PATH/TO/SOMEFILE.tar FILE…
   -cf /PATH/TO/SOMEFILE.tar FILE…
  -x 展开归档
   -xf /PATH/FROM/SOMEFILE.tar
   -xf /PATH/FROM/SOMEFILE.tar -C /PATH/TO/SOMEDIR 指定展开归档的目录
  -t 查看归档文件的文件列表
   -tf /PATH/FROM/SOMEFILE.tar
  -v, --verbose 显示过程,通常和-c/-x一起使用

[root@linux4 tar]# ls
boot.log  vmware.log  yum.log
[root@linux4 tar]# tar -cvf mylog.tar boot.log vmware.log yum.log 
boot.log
vmware.log
yum.log
[root@linux4 tar]# ls
boot.log  mylog.tar  vmware.log  yum.log
[root@linux4 tar]# tar -cf mylog2.tar *.log
[root@linux4 tar]# ls
boot.log  mylog2.tar  mylog.tar  vmware.log  yum.log
[root@linux4 tar]# rm -rf *.log
[root@linux4 tar]# tar -xvf mylog.tar 
boot.log
vmware.log
yum.log
[root@linux4 tar]# ls
boot.log  mylog2.tar  mylog.tar  vmware.log  yum.log
[root@linux4 tar]# rm -rf *.log
[root@linux4 tar]# ls
mylog2.tar  mylog.tar
[root@linux4 tar]# tar -xf mylog2.tar 
[root@linux4 tar]# ls
boot.log  mylog2.tar  mylog.tar  vmware.log  yum.log
[root@linux4 tar]# mkdir mylog2
[root@linux4 tar]# tar -xf mylog2.tar -C mylog2
[root@linux4 tar]# cd mylog2
[root@linux4 mylog2]# ls
boot.log  vmware.log  yum.log
[root@linux4 mylog2]# 

三、归档压缩命令结合使用
  tar -z:gzip
   -zcf /PATH/TO/SOMEFILE.tar.gz FILE… 打包并压缩
   -zxf /PATH/FROM/SOMEFILE.tar.gz 展开归档并解压缩
  tar -j:bzip2
   -jcf /PATH/TO/SOMEFILE.tar.bz2 FILE… 打包并压缩
   -jxf /PATH/TO/SOMEFILE.tar.bz2 展开归档并解压缩
  tar -J:xz
   -Jcf /PATH/TO/SOMEFILE.tar.xz FILE… 打包并压缩
   -Jxf /PATH/TO/SOMEFILE.tar.xz 展开归档并解压缩

#######################  tar -z 示例 ######################### 
[root@linux4 tar]# ls
boot.log  vmware.log  yum.log
[root@linux4 tar]# tar -czf mylog.tar.gz *.log
[root@linux4 tar]# ls
boot.log  mylog.tar.gz  vmware.log  yum.log
[root@linux4 tar]# rm -rf *.log
[root@linux4 tar]# tar -zxf mylog.tar.gz 
[root@linux4 tar]# ls
boot.log  mylog.tar.gz  vmware.log  yum.log

#######################  tar -j 示例 ######################### 
[root@linux4 tar]# ls
boot.log  vmware.log  yum.log
[root@linux4 tar]# tar -jcf mylog.tar.bz2 *.log
[root@linux4 tar]# ls
boot.log  mylog.tar.bz2  vmware.log  yum.log
[root@linux4 tar]# rm -rf *.log
[root@linux4 tar]# tar -jxf mylog.tar.bz2 
[root@linux4 tar]# ls
boot.log  mylog.tar.bz2  vmware.log  yum.log
[root@linux4 tar]# 

#######################  tar -J 示例 ######################### 
[root@linux4 tar]# ls
boot.log  vmware.log  yum.log
[root@linux4 tar]# tar -Jcf mylog.tar.xz *.log
[root@linux4 tar]# ls
boot.log  mylog.tar.xz  vmware.log  yum.log
[root@linux4 tar]# rm -rf *.log
[root@linux4 tar]# tar -Jxf mylog.tar.xz 
[root@linux4 tar]# ls
boot.log  mylog.tar.xz  vmware.log  yum.log
[root@linux4 tar]# 

你可能感兴趣的:(Linux,linux,服务器,运维)