压缩、解压缩和归档的工具主要有:
file-roller、compress/uncompress: .Z、gzip/gunzip: .gz、bzip2/bunzip2: .bz2、xz/unxz: .xz、zip/unzip、tar、cpio等等
compress/uncompress
关于compress的用法:
Unknown flag: '-'; Usage: compress [-dfvcVr] [-b maxbits] [file ...]
-d If given, decompression is done instead.
-c Write output on stdout, don't remove original.
-b Parameter limits the max number of bits/code.
-f Forces output file to be generated, even if one already.
exists, and even if no space is saved by compressing.
If -f is not used, the user will be prompted if stdin is.
a tty, otherwise, the output file will not be overwritten.
-v Write compression statistics.
-V Output vesion and compile options.
-r Recursive. If a filename is a directory, descend
into it and compress everything in it.
常用选项:
- -d: 解压缩,相当于uncompress
- -c: 结果输出至标准输出,不删除原文件
- -v: 显示详情
- uncompress 解压缩
- zcat file.Z >file 将file.Z文件解压缩,并显示到屏幕上,可以重定向到其他文件中
特性:压缩完成后,不会保留原文件。
[root@Centos7 app]#compress m
[root@Centos7 app]#ll
total 3608
-rw-------. 1 root root 2975289 Aug 6 19:40 messages-20180806
-rw-------. 1 root root 716051 Aug 6 19:41 m.Z
[root@Centos7 app]#compress -d m.Z
[root@Centos7 app]#ll
total 5816
-rw-------. 1 root root 2975289 Aug 6 19:41 m
-rw-------. 1 root root 2975289 Aug 6 19:40 messages-20180806
[root@Centos7 app]#compress -c m > m1.Z
[root@Centos7 app]#ll
total 6516
-rw-------. 1 root root 2975289 Aug 6 19:41 m
-rw-r--r--. 1 root root 716051 Aug 6 20:04 m1.Z
-rw-------. 1 root root 2975289 Aug 6 19:40 messages-20180806
[root@Centos7 app]#zcat m1.Z > m2
[root@Centos7 app]#ll
total 12332
-rw-------. 1 root root 2975289 Aug 6 19:41 m
-rw-r--r--. 1 root root 716051 Aug 6 20:04 m1.Z
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
gzip/gunzip
关于gzip用法说明:
[root@Centos7 app]#gzip --help
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
常用选项:
- -d: 解压缩,相当于gunzip
- -c: 将压缩或解压缩的结果输出至标准输出
- -#: 1-9,指定压缩比,值越大压缩比越大
特性:压缩完成后,不保留原始文件。压缩率大于compress。
[root@Centos7 app]#gzip m
[root@Centos7 app]#ll
total 6204
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
-rw-------. 1 root root 2975289 Aug 6 19:40 messages-20180806
-rw-------. 1 root root 394603 Aug 6 19:41 m.gz
[root@Centos7 app]#gzip -d m
[root@Centos7 app]#ll
total 8724
-rw-------. 1 root root 2975289 Aug 6 19:41 m
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
[root@Centos7 app]#gzip -9 m
[root@Centos7 app]#ll
total 6196
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
-rw-------. 1 root root 2975289 Aug 6 19:40 messages-20180806
-rw-------. 1 root root 388190 Aug 6 19:41 m.gz
bzip2/bunzip2/bzcat
关于bzip2的用法说明:
[root@Centos7 app]#bzip2 --help
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.
常用选项:
- -k: keep, 保留原文件
- -d:解压缩
- -#: 1-9,压缩比,默认为9
bzcat:不显式解压缩的前提下查看文本文件内容
特性:使用-k选项可以实现复制后保留原文件,压缩率:bzip2>gzip>compress
[root@Centos7 app]#bzip2 -k m2
[root@Centos7 app]#ll
total 6388
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
-rw-r--r--. 1 root root 193018 Aug 6 20:08 m2.bz2
[root@Centos7 app]#bunzip2 m2.bz2
[root@Centos7 app]#ll
total 6196
-rw-r--r--. 1 root root 2975289 Aug 6 20:08 m2
xz/unxz/xzcat
关于xz的用法说明:
Usage: xz [OPTION]... [FILE]...
Compress or decompress FILEs in the .xz format.
-z, --compress force compression
-d, --decompress, --uncompress
force decompression
-t, --test test compressed file integrity
-l, --list list information about .xz files
-k, --keep keep (don't delete) input files
-f, --force force overwrite of output file and (de)compress links
-c, --stdout, --to-stdout
write to standard output and don't delete input files
-0 ... -9 compression preset; default is 6; take compressor *and*
decompressor memory usage into account before using 7-9!
-e, --extreme try to improve compression ratio by using more CPU time;
does not affect decompressor memory requirements
-T, --threads=NUM use at most NUM threads; the default is 1; set to 0
to use as many threads as there are processor cores
-q, --quiet suppress warnings; specify twice to suppress errors too
-v, --verbose be verbose; specify twice for even more verbose
-h, --help display this short help and exit
-H, --long-help display the long help (lists also the advanced options)
-V, --version display the version number and exit
常用选项:
- -k: keep, 保留原文件
- -d:解压缩
- -#: 1-9,压缩比,默认为6
xzcat: 不显式解压缩的前提下查看文本文件内容
特性:使用-k选项,压缩完成后可以保留原文件。压缩率:xz>bzip2>gzip>compress
[root@Centos7 app]#xz -k m
[root@Centos7 app]#ll
total 9032
-rw-------. 1 root root 2975289 Aug 6 19:41 m
-rw-------. 1 root root 116792 Aug 6 19:41 m.xz
zip/unzip
关于zip用法说明:
[root@Centos7 app]#zip --help
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help
打包压缩
zip –r /testdir/sysconfig /etc/sysconfig/
特性:可以针对文件夹进行压缩打包 。压缩率不是很高。
[root@Centos7 app]#zip test /etc/sysconfig/
adding: etc/sysconfig/ (stored 0%)
[root@Centos7 app]#ll
-rw-r--r--. 1 root root 178 Aug 7 09:05 test.zip
tar
tar( Tape ARchive,磁带归档的缩写)
tar [OPTION]...
(1) 创建归档
tar -cpvf /PATH/TO/SOMEFILE.tar FILE...
[root@Centos7 app]#tar -cpvf etc.tar etc
[root@Centos7 app]#ll
total 9700
drwxr-xr-x. 6 root root 4096 Aug 7 09:49 etc
-rw-r--r--. 1 root root 276480 Aug 7 09:49 etc.tar
(2) 追加文件至归档: 注:不支持对压缩文件追加
tar -r -f /PATH/TO/SOMEFILE.tar FILE...
[root@Centos7 app]#tar rf test1.tar /etc/issue
tar: Removing leading `/' from member names
[root@Centos7 app]#tar -tvf test1.tar
[root@Centos7 app]#tar -tvf test1.tar
drwxrwx--- root/root 0 2018-08-07 10:19 etc/
-rw------- root/root 2393 2018-08-07 10:19 etc/ip6tables-config
-rw------- root/root 2374 2018-08-07 10:19 etc/iptables-config
drwxr-xr-x root/root 0 2018-08-07 10:19 etc/cbq/
-rw-r--r-- root/root 23 2018-04-29 00:35 etc/issue
(3) 查看归档文件中的文件列表
tar -t -f /PATH/TO/SOMEFILE.tar
(4) 展开归档
tar -x -f /PATH/TO/SOMEFILE.tar
tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/
(5) 结合压缩工具实现:归档并压缩
-j: bzip2, -z: gzip, -J: xz
[root@Centos7 app]#tar jcvf etc.tar.bz2 etc
[root@Centos7 app]#tar Jcvf etc.tar.xz etc
[root@Centos7 app]#tar zcvf etc.tar.gz etc
[root@Centos7 app]#ll
total 9848
drwxrwx---+ 6 root root 4096 Aug 7 10:19 etc
-rw-r--r--. 1 root root 45634 Aug 7 10:38 etc.tar.bz2
-rw-r--r--. 1 root root 56577 Aug 7 10:40 etc.tar.gz
-rw-r--r--. 1 root root 44748 Aug 7 10:39 etc.tar.xz
(6)-T选项指定输入文件,-X选项指定包含要排除的文件列表
tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist
(7)分割大的 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
(8)合并:
cat mybackup-parts* > mybackup.tar.gz
cpio
功能:pio命令可以复制文件到归档包中,或者从归档包中复制文件。
cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio” 或者“.tar” 结尾的文件
cpio [选项] > 文件名或者设备名
cpio [选项] < 文件名或者设备名
选项
-o 将文件拷贝打包成文件或者将文件输出到设备上
-i 解包,将打包文件解压或将设备上的备份还原到系统
-t 预览,查看文件内容或者输出到设备上的文件内容
-v 显示打包过程中的文件名称。
-d 解包生成目录,在cpio还原时,自动的建立目录
-c 一种较新的存储方式
示例
将etc目录备份:
find ./etc -print |cpio -ov >etc.cpio
内容预览
cpio –tv < etc.cpio
解包文件
cpio –idv < etc.cpio