tar/gzip/zip文件打包、压缩命令

一、tar打包备份工具

1.命令功能

tar 将多个文件或目录打包在一起,可用通过调用gzip或zip实现压缩、解压的命令;tar不仅可以多多个文件进行打包,还可以对多个文件打包后进行压缩。

2.语法格式

tar  option  file

tar   选项   文件或目录

选项说明

选项选项

选项选项说明

c

创建新的tar包 *

v

显示详细tar执行过程*

f

指定压缩的文件名字*

t

不解压查看tar包的内容*

p

保持文件的原有属性*

j

通过bzip2命令压缩或解压*

z

通过gzip压缩或解压*

x

解压tar包*

C

指定解压的目录路径*

 

3.使用范例

范例1 备份/etc目录

[root@cxf chu]# tar -zcvf etc.gz  /etc/
......
[root@cxf chu]# tar -zcvf network.gz /etc/sysconfig/

tar: 从成员名中删除开头的“/”
/etc/sysconfig/
/etc/sysconfig/clock
/etc/sysconfig/keyboard
[root@cxf chu]# ls -l
总用量 9420
-rw-r--r-- 1 root root 9589948 5月   8 02:53 etc.gz
-rw-r--r-- 1 root root   49488 5月   8 03:01 network.gz

说明:tar –zcvf etc.gz  /etc/ 

z:gzip压缩文件;c:创建tar包;v:显示tar执行过程;f:指定压缩文件名

etc.gz:压缩文件名;/etc/ 打包源文件

范例2 查看压缩包的内容

[root@cxf chu]# tar -ztvf network.gz 
drwxr-xr-x root/root         0 2018-05-07 01:28 etc/sysconfig/
-rw-r--r-- root/root        21 2017-07-31 18:53 etc/sysconfig/clock
-rw-r--r-- root/root        63 2017-07-31 18:53 etc/sysconfig/keyboard

说明:tar –ztvf network.gz

z:gzip解压或压缩文件,此处解压文件;t:不解压查看tar包文件;v:显示tar执行过程;f:指定压缩的文件名。

范例3 解开压缩包

[root@cxf chu]# tar -zxf network.gz 
[root@cxf chu]# ls   
etc  etc.gz  network.gz
[root@cxf chu]# ls -l etc
总用量 4
drwxr-xr-x 7 root root 4096 5月   7 01:28 sysconfig

说明:解压后有一个etc目录,etc目录下才是sysconfig目录;故打包目录时,一般先cd到打包。目录的上一级目录中后在进行打包。

示例4 打包/etc/目录下的所有的普通文件

[root@cxf /]# tar zcvf etc.tar.gz `find etc/ -type f`
[root@cxf /]# ls -l etc.tar.gz 
-rw-r--r-- 1 root root 9578175 5月   8 03:26 etc.tar.gz

二、 gzip 压缩或解压文件

1.命令功能

gzip命令的作用是将一个大的文件通过压缩算法,变成一个小的文件,gzip文件不能直接压缩目录,如果要压缩目录,需要先将目录打包成一个文件,在压缩。

2.语法格式

gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ...  ]

或gzip  option  file

选项

选项说明

-c

将内容输出到标准输出,不改变原始文件 ****

-d

解压文件

-v

显示执行过程

-r

对目录下的所有文件递归进行压缩操作

-数字(1-9)

指定压缩率,默认是6,值越大压缩率越高

-t

测试,检查压缩文件是否完整。

-l

列出压缩文件的内容信息

3.使用范例

[root@cxf test]# touch {1..6}.txt
[root@cxf test]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt

示例1 把test目录下的每个文件都单独压缩成.gz文件

[root@cxf test]# gzip *.txt
[root@cxf test]# ls
1.txt.gz  2.txt.gz  3.txt.gz  4.txt.gz  5.txt.gz  6.txt.gz

示例2 不解压,显示每个压缩文件的信息。

[root@cxf test]# gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
                 38                  12  50.0% 1.txt
                 40                  14  42.9% 2.txt
                 44                  30  60.0% 3.txt
                 26                   0   0.0% 4.txt
                 26                   0   0.0% 5.txt
                 26                   0   0.0% 6.txt
                200                  56 -200.0% (totals)

示例3 解压文件,显示解压过程。

[root@cxf test]# gzip -dv *.gz
1.txt.gz:        50.0% -- replaced with 1.txt
2.txt.gz:        42.9% -- replaced with 2.txt
3.txt.gz:        60.0% -- replaced with 3.txt
4.txt.gz:         0.0% -- replaced with 4.txt
5.txt.gz:         0.0% -- replaced with 5.txt
6.txt.gz:         0.0% -- replaced with 6.txt

三、 zip 打包和压缩文件

1.命令功能

zip是linux和windows等多平台通用的压缩格式。zip比gzip更强的是zip命令压缩文件不会删除源文件,还可以压缩目录。

2.语法格式

zip  option  file

选项

选项说明

-r

将指定目录下的所有文件和子目录一并压缩

-x

压缩文件时排查某个文件

-q

不显示压缩信息

3.使用范例

示例1 压缩文件

[root@cxf test]# cp /etc/services .
[root@cxf test]# ls
services
[root@cxf test]# zip services.zip ./services 
  adding: services (deflated 80%)
[root@cxf test]# ls -l
总用量 756
-rw-r--r-- 1 root root 641020 5月  12 01:59 services
-rw-r--r-- 1 root root 127362 5月  12 01:59 services.zip

示例2 压缩目录

[root@cxf test]# cd /
[root@cxf /]# zip tmp.zip ./tmp/
  adding: tmp/ (stored 0%)
[root@cxf /]# zip -r tmp.zip ./tmp/
updating: tmp/ (stored 0%)
  adding: tmp/yum_save_tx-2018-04-24-15-13M35BLW.yumtx (deflated 93%)
  adding: tmp/yum_save_tx-2017-09-04-18-02kOlm9G.yumtx (deflated 26%)
  adding: tmp/yum_save_tx-2018-05-03-18-09pp_ew1.yumtx (deflated 57%)
  adding: tmp/.ICE-unix/ (stored 0%)
  adding: tmp/oldboy.txt (deflated 27%)
  adding: tmp/yum_save_tx-2018-04-24-16-35GSVJdg.yumtx (deflated 68%)
  adding: tmp/yum_save_tx-2017-09-04-18-01uVYqM6.yumtx (deflated 27%)
  adding: tmp/yum_save_tx-2017-09-15-02-36xHd5QO.yumtx (deflated 94%)
  adding: tmp/yum_save_tx-2018-04-24-16-34EZkcRv.yumtx (deflated 28%)
  adding: tmp/yum.log (stored 0%)

示例3 排查压缩

[root@cxf /]# zip -r tmp1.zip ./tmp/ -x tmp/services.zip
  adding: tmp/ (stored 0%)
  adding: tmp/yum_save_tx-2018-04-24-15-13M35BLW.yumtx (deflated 93%)
  adding: tmp/yum_save_tx-2017-09-04-18-02kOlm9G.yumtx (deflated 26%)
  adding: tmp/yum_save_tx-2018-05-03-18-09pp_ew1.yumtx (deflated 57%)
  adding: tmp/.ICE-unix/ (stored 0%)
  adding: tmp/oldboy.txt (deflated 27%)
  adding: tmp/yum_save_tx-2018-04-24-16-35GSVJdg.yumtx (deflated 68%)
  adding: tmp/yum_save_tx-2017-09-04-18-01uVYqM6.yumtx (deflated 27%)
  adding: tmp/yum_save_tx-2017-09-15-02-36xHd5QO.yumtx (deflated 94%)
  adding: tmp/yum_save_tx-2018-04-24-16-34EZkcRv.yumtx (deflated 28%)
  adding: tmp/yum.log (stored 0%)

猜你喜欢

转载自www.cnblogs.com/joechu/p/9034178.html