1.10 Linux基础(十) 压缩与解压缩
1.10.1 compress
[[email?protected] data]# compress m ##不保留源文件 [[email?protected] data]# ll total 4840 -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 994739 Aug 8 19:11 m.Z [[email?protected] data]# compress -d m.Z [[email?protected] data]# ll total 7724 -rw-r--r--. 1 root root 3944964 Aug 8 19:11 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak [[email?protected] data]# compress -c m > m.Z ## 保留源文件 [[email?protected] data]# ll total 8688 -rw-r--r--. 1 root root 3944964 Aug 8 19:11 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 994739 Aug 8 19:20 m.Z drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# compress -d m.Z m already exists. Do you wish to overwrite m (y or n)? y 1.10.2 gzip/gunzipgzip [OPTION]... FILE ...
[[email?protected] data]# gzip m [[email?protected] data]# ll total 4408 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# gunzip m ##和gzip -d一样 [[email?protected] data]# ll total 7716 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-r--r--. 1 root root 3944964 Aug 8 19:22 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# zcat m.gz > m [[email?protected] data]# ll total 8264 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-r--r--. 1 root root 3944964 Aug 8 19:45 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts 1.10.3 bzip2bzip2/bunzip2/bzcat
[[email?protected] data]# bzip2 m [[email?protected] data]# ll total 8528 -rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2 -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# bzcat m.bz2 > f1 [[email?protected] data]# ll total 12384 -rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-r--r--. 1 root root 3944964 Aug 8 19:45 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2 -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# bzip2 -d m.bz2 [[email?protected] data]# ll total 12120 -rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-r--r--. 1 root root 3944964 Aug 8 19:45 m -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts [[email?protected] data]# bzip2 -k f1 ##保留源文件 [[email?protected] data]# ll total 8792 -rw-r--r--. 1 root root 3944964 Aug 8 19:49 f1 -rw-r--r--. 1 root root 267452 Aug 8 19:49 f1.bz2 -rw-r--r--. 1 root root 0 Aug 8 19:41 file -rw-------. 1 root root 3944964 Aug 8 19:10 m.bak -rw-r--r--. 1 root root 267452 Aug 8 19:45 m.bz2 -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:17 scripts 1.10.4 xz/unxz/xzcat
1.10.5 zip/unzip:
可以压缩文件夹 打包压缩
解包解压缩
1、把文件解压到当前目录下 unzip test.zip 2、如果要把文件解压到指定的目录下,需要用到-d参数。 unzip -d /temp test.zip 3、解压的时候,有时候不想覆盖已经存在的文件,那么可以加上-n参数 unzip -n test.zip unzip -n -d /temp test.zip 4、只看一下zip压缩包中包含哪些文件,不进行解压缩 unzip -l test.zip 5、查看显示的文件列表还包含压缩比率 unzip -v test.zip 6、检查zip文件是否损坏 unzip -t test.zip 7、将压缩文件test.zip在指定目录tmp下解压缩,如果已有相同的文件存在,要求unzip命令覆盖原先的文件 unzip -o test.zip -d /tmp/ [[email?protected] data]# zip -r tertdir.zip testdir/ [[email?protected] data]# ll total 8796 -rw-r--r--. 1 root root 978 Aug 8 19:59 tertdir.zip drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir [[email?protected] data]# zip -r sysconfig.zip /etc/sysconfig -rw-r--r--. 1 root root 95104 Aug 8 20:02 sysconfig.zip [[email?protected] data]# cat /var/log/messages |zip message.zip - ##将cat 命令的执行结果放到文件名为-中,然后压缩成message.zip adding: - (deflated 98%) [[email?protected] data]# ll -rw-r--r--. 1 root root 61943 Aug 8 20:13 message.zip -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:56 scripts drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir -rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip [[email?protected] data]# unzip message.zip Archive: message.zip inflating: - [[email?protected] data]# ll total 11712 -rw-------. 1 root root 2851599 Aug 8 20:13 - ##解压后文件- message.zip -rw-r--r--. 1 root root 558552 Aug 8 19:22 m.gz drwxr-xr-x. 4 root root 4096 Aug 8 19:56 scripts drwxr-xr-x. 3 root root 17 Aug 8 19:58 testdir -rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip [[email?protected] data]# unzip -p testdir.zip > test ##压缩后更改文件名为test [[email?protected] data]# ll total 11712 -rw-r--r--. 1 root root 0 Aug 8 20:39 test -rw-r--r--. 1 root root 978 Aug 8 19:59 testdir.zip 1.10.6 tar工具tar(Tape ARchive,磁带归档的缩写)
(1) 创建归档
(2) 追加文件至归档: 注:不支持对压缩文件追加
(3) 查看归档文件中的文件列表
(4) 展开归档
(5) 结合压缩工具实现:归档并压缩
[[email?protected] data]# tar -zcvf testdir.tar.gz testdir testdir/ testdir/di1/ testdir/di1/dir2/ testdir/di1/dir2/3/ testdir/di1/dir2/4/ testdir/di1/dir2/5/ [[email?protected] data]# ll testdir testdir.tar.gz [[email?protected] data]# tar -jcvf testdir.tar.bz2 testdir [[email?protected] data]# ll testdir.tar.bz2 [[email?protected] data]# tar -Jcvf testdir.tar.bz2 testdir testdir/ testdir/di1/ testdir/di1/dir2/ testdir/di1/dir2/3/ testdir/di1/dir2/4/ testdir/di1/dir2/5/ [[email?protected] data]# ll testdir.tar.xz [[email?protected] data]# tar -zvxf testdir.tar.gz testdir/ testdir/di1/ testdir/di1/dir2/ testdir/di1/dir2/3/ testdir/di1/dir2/4/ testdir/di1/dir2/5/ [[email?protected] data]# ll testdir -T选项指定输入文件,-X选项指定包含要排除的文件列表 tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist 分割大的 tar 文件为多份小文件: split –b Size –d tar-file-name prefix-name split -b 1M –d mybackup.tgz mybackup-parts split -b 1M mybackup.tgz mybackup-parts 合并: cat mybackup-parts* > mybackup.tar.gz 1.10.7 cpio功能:复制文件从或到归档 cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以 解压以“.cpio” 或者“.tar” 结尾的文件 cpio [选项] > 文件名或者设备名 cpio [选项] < 文件名或者设备名 选项
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |