tar、giz、bgiz2命令简介

gzip简介

  gzip是一个被广泛使用的压缩程序,使用方式很简单:gzip [option]...[file]...,压缩过程中可以指定压缩率等参数,压缩后默认文件名会多出".gz"后缀。
特点:gzip只能压缩文件 , 不能压缩目录 ,在原文件上进行压缩,压缩后原文件名称默认会添加.gz后缀 , 可以理解为它不保留原文件。
gzip的

*:压缩当前目录下所有文件<忽略目录>,且不会重复压缩
-f:强制压缩,可压缩任意文件,即使压缩过了还是会再次压缩
[root@localhost opt]# gzip *
gzip: gzip_dir is a directory -- ignored
gzip: wget-log.gz already has .gz suffix -- unchanged
gzip: zookeeper-3.4.10.tar.gz already has .gz suffix -- unchanged

-l:查看文件列表
[root@localhost gzipdir]# gzip -l gt1.txt.gz
         compressed        uncompressed  ratio uncompressed_name
                 41                  13 -15.4% gt1.txt

-d:解压文件
-v:打印指令执行过程
[root@localhost opt]# gzip -dv wget-log.gz
wget-log.gz:     90.8% -- replaced with wget-log

-1至-9:指定压缩比率,比率越小压缩速度越快,比率越大压缩速度越慢,默认为6。
-1或–fast表示最快压缩方法,压缩10%
-9或–best表示最慢压缩方法,压缩90%
[root@localhost gzipdir]# gzip -4 gt2.txt

-r:递归,以递归的方式同时压缩或解压所有子目录中的文件
[root@localhost gzipdir]# gzip -r *
[root@localhost gzipdir]# gzip -dv -r *


-q:不输出警告信息
[root@localhost gzipdir]# gzip *
gzip: gt1.txt.gz already has .gz suffix -- unchanged
gzip: gt2.txt.gz already has .gz suffix -- unchanged
[root@localhost gzipdir]# gzip -q *
[root@localhost gzipdir]#

[root@localhost ~]# gzip -h
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.
[root@localhost ~]# 

bgiz2命令

  bzip2同样是一个压缩程序,与gzip有很多相似之处,比如不能压缩目录、可设置压缩效果等等。与gzip的主要区别在于,bzip2采用新的压缩算法,压缩率更高,但对应的压缩速度要慢些。此外bgzip2不支持递归压缩、压缩后生成的后缀是.bz2、可以保留原文件等等。
-z:压缩;(默认为带-z)
-d:解压; 
-k:压缩或解压后都保留原文件
-f:解压后,如果与某文件冲突,则覆盖。

[root@localhost gzipdir]# bzip2 -z gt1.txt
[root@localhost gzipdir]# bzip2 -dv -f -k gt1.txt.bz2
  gt1.txt.bz2: done

[root@localhost gzipdir]# bzip2 -h 
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
   -z --compress       force compression
   -k --keep           keep (don't delete) input files
   -f --force          overwrite existing output files
   -t --test           test compressed file integrity
   -c --stdout         output to standard out
   -q --quiet          suppress noncritical error messages
   -v --verbose        be verbose (a 2nd -v gives more)
   -L --license        display software version & license
   -V --version        display software version & license
   -s --small          use less memory (at most 2500k)
   -1 .. -9            set block size to 100k .. 900k
   --fast              alias for -1
   --best              alias for -9

   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

   If no file names are given, bzip2 compresses or decompresses
   from standard input to standard output.  You can combine
   short flags, so `-v -4' means the same as -v4 or -4v, &c.

tar
  前面说的gzip、bzip2都是压缩程序,而且不支持归档操作,无法把多个文件归档成一个文件。而tar是Linux中一个打包程序,实现了文件的归档, 用tar程序打出来的包通常都以 .tar 结尾,在打包的同时还可以调用gzip或者bzip2等进行压缩,打包之后生成的tar包也可以用它们来解压或压缩。tar的选项比较多,但常用的也就那么几个

  • -c:生成压缩文档
  • -C:指定文档目录(目录必须存在,默认为当前目录)
  • -f: 指定文档名称(只能位于所有参数的最后,后面紧跟文件名称,一般以.tar结尾
  • -t:查看文档内容
  • -u:更新压缩文档中的文件
  • -r:向压缩文档末尾追加文件(只能向.tar文档中添加,无法往用gzip或bzip2等方式生成的压缩文档中添加)
  • -x:解压文档
  • -z:以gzip命令压缩/解压缩文档
  • - j:以bzip2命令压缩/解压缩文档
  • -k:解压时不覆盖已有文件
  • -v: 显示指令执行过程

#将gt1 gt2两个文件归档成一个tar文件,并采用gzip和bzip2方式压缩,存放至tardir目录下
[root@localhost gzipdir]# tar -czvf ./tardir/t.tar.gz gt1.txt gt2.txt
[root@localhost gzipdir]# tar -cjvf ./tardir/t.tar.bz2 gt1.txt gt2.txt 

#将当前目录下所有文件归档成一个tar文件  
[root@localhost gzipdir]# tar -cvf t.tar * 

#往t.tar文件中添加文件
[root@localhost gzipdir]# tar -rf t.tar gt3.txt

#查看t.tar.gz文档内容
[root@localhost gzipdir]# tar -tf t.tar.gz

#解压t.tar.gz中的gt2.txt,并指定解压目录(默认会覆盖目录中已有文件,使用-k即可指定不覆盖)
[root@localhost gzipdir]# tar -xzvf ./tardir/t.tar.gz -C ./untardir gt1.txt

总结

1、*.gz :用 gzip -d或者gunzip 解压

2、*.bz2 :用 bzip2 -d或者用bunzip2 解压

3、*.tar :用 tar –xvf 解压

4、*.tar.gz:和*.tgz 用 tar –xzf 解压

5、*.tar.bz2:用tar –xjf 解压
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值