您的位置:首页 > 运维架构 > Linux

linux压缩与归档工具

2015-10-20 11:52 423 查看
压缩即使用特定的算法来减小计算机文件大小的机制。
linux常用的压缩和解压缩工具有gzip/gunzipbzip2/bunzip2xz/unxzzip/unzip,另外还有一个重要的归档工具tar和备份工具cpio

一、压缩和解压缩工具
gzip:压缩包后缀为.gz,压缩对象不能是目录,压缩后默认会删除原文件
用法:gzip [option] somefile
选项:
-#:压缩比,范围为0-9,默认为6
-d:相当于gunzip
-c:将压缩后的结果输出至标准输出,可实现保留原文件。例如 gzip -c /path/to/somefile > /path/to/somefile.gz
-r:递归处理
不解压查看gzip:zcat,zless,zmore
解压缩:gunzip somefile.gz
[root@localhost tesla]# gzip test
gzip: test is a directory -- ignored   # gzip不能压缩目录
[root@localhost tesla]# gzip -r coffee   # 对目录coffee进行递归压缩处理,coffee中除目录之外的所有文件都被压缩成gzip格式,见下方
[root@localhost tesla]# ls -R coffee
coffee:
test1  test2

coffee/test1:
group.gz  passwd.gz

coffee/test2:
inittab.gz  profile.gz
[root@localhost tesla]# cd test
[root@localhost test]# ls
group  inittab  passwd
[root@localhost test]# gzip -8 group   # 指定压缩比为8
[root@localhost test]# ls   # 可见压缩后源文件被删除
group.gz  inittab  passwd
[root@localhost test]# gunzip group.gz   # 解压缩
[root@localhost test]# ls   # 解压缩后压缩包默认也被删除,同样可在gunzip命令中指定-c选项保留
group  inittab  passwd
[root@localhost test]# gzip -c inittab > /tmp/test/initys.gz   # 这种方式可保留源文件
[root@localhost test]# ls /tmp/test
initys.gz
[root@localhost test]# zcat /tmp/test/initys.gz   # 不解压查看
# inittab is only used by upstart for the default runlevel.
...
bzip2:压缩包后缀为.bz2,其用法类似于gzip,但压缩率比gzip好
用法:bzip2 [option] somefile
选项:
-#:压缩比
-d:相当于bunzip2
-f:bz2压缩包在解压时,若输出文件与现有文件同名,默认不覆盖,指定该选项可强制覆盖
-k:保留原文件
-v:显示命令执行过程
不解压查看:bzcat,bzless,bzmore
解压缩:bunzip2 somefile.bz2
[tesla@localhost ~]$ ls test1
group  passwd
[tesla@localhost ~]$ bzip2 -k test1/group   # 压缩group,并保留原文件
[tesla@localhost ~]$ ls test1
group  group.bz2  passwd
[tesla@localhost ~]$ bzcat test1/group.bz2   # 不解压查看
root:x:0:
bin:x:1:bin,daemon
...
[tesla@localhost ~]$ bunzip2 test1/group.bz2    # 解压缩
bunzip2: Output file test1/group already exists.
[tesla@localhost ~]$ bunzip2 -f test1/group.bz2   # 指定-f选项强行覆盖
xz:压缩包后缀为.xz压缩比很高,但速度较慢
用法:xz [option] somefile
选项:
-#:压缩比
-d:相当于unxz
-k:保留原文件
不解压查看:xzcat,xzless,xzmore
解压缩:unxz somefile.xz
[tesla@localhost ~]$ ls test2
inittab  profile
[tesla@localhost ~]$ xz -v test2/inittab   # 可以看到,压缩比确实很高
test2/inittab (1/1)
100.0 %                       556 B / 884 B = 0.629
[tesla@localhost ~]$ ls test2
inittab.xz  profile
zip:压缩单个文件或将多个文件作归档压缩,不能压缩目录(准确的说,压缩对象可以指定为目录,但对目录本身的压缩率为0%),压缩后不会删除原文件;需指定压缩包文件名,压缩包后缀为.zip,其保存于当前目录;
用法:zip ZIP_FILE.zip src_file...
选项:
-d:从压缩包内删除指定文件
-r:递归处理

解压:unzip somefile.zip
-d:指定压缩包解压后要存放的目录
-n:解压时不覆盖原有文件
-o:unzip执行后覆盖原文件,不询问用户
-v:查看压缩包目录,但不解压
[tesla@localhost ~]$ ls
coffee  test
[tesla@localhost ~]$ ls test
test1  test2
[tesla@localhost ~]$ ls test/test{1,2}
test/test1:
group  passwd

test/test2:
inittab  profile
[tesla@localhost ~]$ zip rose.zip test/test1/*   # 对test1目录中的文件归档压缩
adding: test/test1/group (deflated 47%)
adding: test/test1/passwd (deflated 59%)
[tesla@localhost ~]$ zip -r beast.zip test   # 如果想对各子目录下的文件一并压缩,指定-r选项(递归处理)是个不错的选择
adding: test/ (stored 0%)
adding: test/test1/ (stored 0%)
adding: test/test1/group (deflated 47%)
adding: test/test1/passwd (deflated 59%)
adding: test/test2/ (stored 0%)
adding: test/test2/profile (deflated 52%)
adding: test/test2/inittab (deflated 47%)
[tesla@localhost ~]$ ls
beast.zip  coffee  rose.zip  test
[tesla@localhost ~]$ zip beast.zip -d test/test2/inittab   # 删除压缩包beast.zip中的一个文件
deleting: test/test2/inittab
[tesla@localhost ~]$ unzip -v beast.zip   # 不解压的情况下查看压缩包目录,可以看到,指定的文件确实已被删除
Archive:  beast.zip
Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
0  Stored        0   0% 10-19-2015 09:25 00000000  test/
0  Stored        0   0% 10-19-2015 09:25 00000000  test/test1/
796  Defl:N      422  47% 10-19-2015 07:38 d8930aa4  test/test1/group
1590  Defl:N      644  60% 10-19-2015 07:38 f5fa6e0b  test/test1/passwd
0  Stored        0   0% 10-19-2015 08:47 00000000  test/test2/
1796  Defl:N      855  52% 10-19-2015 09:25 8a28f20c  test/test2/profile
--------          -------  ---                            -------
4182             1921  54%                            6 files
[tesla@localhost ~]$ unzip rose.zip -d coffee/   # 将rose.zip解压至coffee目录下
Archive:  rose.zip
inflating: coffee/test/test1/group
inflating: coffee/test/test1/passwd
[tesla@localhost ~]$ ls -R coffee
coffee:
test

coffee/test:
test1

coffee/test/test1:
group  passwd


二、归档工具:tar
tar可以将多个文件或目录打包成单个文件,同时还可以通过gzip/bzip2/xz的支持,将该归档文件同时进行压缩
用法:tar [option]... -f tarfile.tar [src_file...]
-f:指定归档文件
注意:多个选项合并指定时,-f选项要放在末尾,因为其后是接归档文件的,例如如果这样指点:-fcv httpd.tar,那么tar会将归档文件名误认为是cv
①创建归档:
-c:create
例 tar -cf httpd.tar httpd
②展开归档:

-x:extract,展开
例 tar -xf httpd.tar
③查看归档后的文件中包含哪些原文件:
-t:列出归档文件清单
例 tar -tf httpd.tar
④tar可直接通过选项调用压缩工具执行压缩或解压
-z:调用gzip
-j:调用bzip2
-J:调用xz
例如 tar -zcf httpd.tar.gz httpd # 把目录httpd归档并压缩成httpd.tar.gz
tar -zxf httpd.tar.gz [-C DIR] # 解压(默认至当前目录),可省去-z,tar会根据后缀自动判断该调用哪种工具;
⑤其它选项
-v:显示执行过程
-r:追加文件至归档
-C DIR(或--directory=DIR):展开或解压到指定目录,如不指定,默认为当前目录
-N, --newer=DATE-OR-FILE:only store files newer than DATE-OR-FILE,只归档比指定时间或文件更新的文件。时间格式如"2015-10-19 00:00:00",以时间戳中的ctime作比较
--exclude=PATTERN:归档时排除符合指定范式的文件

[root@localhost tesla]# ls test
group  inittab  passwd  profile
[root@localhost tesla]# tar -cvf /data/test.tar test  # 如果源文件或目录指定的是绝对路径,打包时会将前面的根路径去掉,否则的话总是会解包到原来的地方而将原文件覆盖
test/
test/profile
test/group
test/passwd
test/inittab
[root@localhost tesla]# ls /data
test.tar
[root@localhost tesla]# cp /etc/grub.conf test/
[root@localhost tesla]# tar -rf /data/test.tar test/grub.conf   # 向包中添加文件
[root@localhost tesla]# tar -tf /data/test.tar   # 不解包查看包中清单
test/
test/profile
test/group
test/passwd
test/inittab
test/grub.conf
[root@localhost tesla]# tar -xf /data/test.tar -C /tmp   # 解包至/tmp目录下
[root@localhost tesla]# ls /tmp/test
group  grub.conf  inittab  passwd  profile
[root@localhost tesla]# cd
[root@localhost ~]# tar -xf /data/test.tar test/group   # 可解开包中的单一文件
[root@localhost ~]# ls /root/test
group
[root@localhost ~]# cd /home/tesla
[root@localhost tesla]# tar -zcf test.tar.gz test   # 打包对调用gzip压缩
[root@localhost tesla]# ls
test  test.tar.gz
[root@localhost tesla]# ll /data/test.tar test.tar.gz
-rw-r--r-- 1 root root 20480 Oct 19 12:37 /data/test.tar
-rw-r--r-- 1 root root  2947 Oct 19 12:40 test.tar.gz
[root@localhost tesla]# tar -xf test.tar.gz -C /var   # 解压
[root@localhost tesla]# ls /var/test
group  grub.conf  inittab  passwd  profile
[root@localhost test]# tar -cf /var/pioneer.tar /home/tesla   # 如果源文件或目录指定的是绝对路径,打包时会自动将根路径去掉,否则总是会解包到原来的地方而将源文件覆盖
tar: Removing leading `/' from member names
[root@localhost test]# tar -tf /var/pioneer.tar
home/tesla/
home/tesla/.bash_logout
home/tesla/.bash_profile
...
[root@localhost tesla]# stat test/passwd
...
Access: 2015-10-19 13:08:23.146558224 +0800
Modify: 2015-10-19 12:59:58.607703441 +0800
Change: 2015-10-19 12:59:58.607703441 +0800
[root@localhost tesla]# tar --newer="2015-10-19 13:05:00" -cvf time.tar test
test/
tar: test/profile: file is unchanged; not dumped
test/group
test/grub.conf
tar: test/passwd: file is unchanged; not dumped   # passwd文件的ctime比指定时间旧,故没有被归档
tar: test/inittab: file is unchanged; not dumped


三、cpio:备份工具。
除了之前讲到的dd具有备份功能之外,cpio也是一个强大的备份工具。cpio的几个特点:
cpio并不会自己去寻找要备份的资料,而是通常需要通过其它的文件查找工具(如find)找到后传送给它。
②与tar工具不同,当我们使用绝对路径指定要备份的资料时,cpio并不会去掉前面的根路径,由此在还原时会覆盖原文件。故一般使用相对路径来指定

备份:通常其它工具找到的结果 | cpio -o [option]... > file/device
常用选项:
-o:让资料输出至文件或设备中
-B:把I/O块大小设置成 5120字节,默认为512字节

-v:显示执行过程
find boot | cpio -oB > /var/boot.backup
find / | cpio -oB > /dev/sdb1 # 将整个文件系统备份到磁盘sdb上
还原:cpio -i [option]... < file/device
常用选项:
-i:还原备份档
-u:无条件覆盖所有文件
-c:use the new portable format
cpio -i < /var/boot.backup
[root@localhost ~]# cd /
[root@localhost /]# find boot | cpio -ovB > data/boot.backup  # 备份boot分区
boot
boot/grub
boot/grub/grub.conf
boot/grub/reiserfs_stage1_5
...
4867 blocks
[root@localhost /]# ls data
boot.backup
[root@localhost /]# ll data/boot.backup
-rw-r--r-- 1 root root 24919040 Oct 19 16:08 data/boot.backup
[root@localhost /]# cd data
[root@localhost data]# cpio -i < boot.backup   # 还原
48670 blocks
[root@localhost data]# ls
boot  boot.backup
[root@localhost data]# ls boot
...
initramfs-2.6.32-431.el6.x86_64.img
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  备份 归档 tar 压缩 gzip