6.1 压缩打包介绍
6.2 gzip压缩工具
gzip, gunzip, zcat - compress or expand files
# gzip 1.txt
# ls
1.txt.gz
# gzip -f test/
gzip: test/ is a directory -- ignored
-d --decompress --uncompress
Decompress.
# gzip -d 1.txt.gz
-# --fast --best
Regulate the speed of compression using the specified digit #,
where -1 or --fast indicates the fastest
compression method (less compression) and -9 or --best
indicates the slowest compression method (best compression).
The default compression level is -6 (that is, biased towards high compression at expense of speed).
# gzip -9 1.txt
-c --stdout --to-stdout
Write output on standard output; keep original files unchanged.
# gzip -c 1.txt > 1.txt.gz
# ls
1.txt 1.txt.gz
# gzip -cd 1.txt.gz > 2.txt
# ls
1.txt 1.txt.gz 2.txt
# gunzip -c 1.txt.gz > 3.txt
# ls
1.txt 1.txt.gz 2.txt 3.txt
Compressed files can be restored to their original form using gzip -d or gunzip or zcat.
If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it legal.
# gunzip 1.txt.gz
# gzip -9 1.txt
# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jan 6 09:32:23 2018, max compression
# gzip 1.txt
# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jan 6 09:32:23 2018
# gzip -1 1.txt
# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jan 6 09:32:23 2018, max speed
# gzip -3 1.txt
# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jan 6 09:32:23 2018
zcat is identical to gunzip -c.
# zcat 1.txt.gz
6.3 bzip2压缩工具
bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding.
# yum -y install bzip2
# bzip2 -f test/
bzip2: Input file test/ is a directory.
# bzip2 1.txt
# ls
1.txt.bz2
# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
-c --stdout
Compress or decompress to standard output.
# bzip2 -c 1.txt > 1.txt.bz2
# ls
1.txt 1.txt.bz2
# bzip2 -cd 1.txt.bz2 > 2.txt
# ls
1.txt 1.txt.bz2 2.txt
-d --decompress
Force decompression. bzip2, bunzip2 and bzcat are really the same program
# bzip2 -d 1.txt.bz2
# ls
1.txt
-1 (or --fast) to -9 (or --best)
Set the block size to 100 k, 200 k .. 900 k when compressing.
The default compression level is -9.
Has no effect when decompressing.
The --fast and --best aliases are primarily for GNU gzip compatibility.
In particular, --fast doesn't make things significantly faster.
And --best merely selects the default behaviour.
# bzip2 -9 2.txt
# file 2.txt.bz2
2.txt.bz2: bzip2 compressed data, block size = 900k
# bzip2 -1 1.txt
# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 100k
# bzip2 1.txt
# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
-z --compress
The complement to -d: forces compression, regardless of the invocation name.
# bunzip2 -z 1.txt
# ls
1.txt.bz2 2.txt test
bzcat - decompresses files to stdout
6.4 xz压缩工具
xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files
unxz is equivalent to xz --decompress.
xzcat is equivalent to xz --decompress --stdout.
# xz -f test/
xz: test/: Is a directory, skipping
# xz 1.txt
# ls
1.txt.xz
-z, --compress
Compress.
This is the default operation mode when no operation mode option is specified
# unxz -z 1.txt
[root@aminglinux-01 d6z]# ls
1.txt.xz
-d, --decompress, --uncompress
Decompress.
# xz -d 1.txt.xz
# ls
1.txt
-k, --keep
Don't delete the input files.
-c, --stdout, --to-stdout
Write the compressed or decompressed data to standard output instead of a file.
This implies --keep.
# xz -c 1.txt > 1.txt.xz
# ls
1.txt 1.txt.xz
# xz -cd 1.txt.xz > 3.txt
# ls
1.txt 1.txt.xz 2.txt 3.txt
-0 ... -9
Select a compression preset level.
The default is -6.
If multiple preset levels are specified, the last one takes effect.
6.5 zip压缩工具
yum -y install zip
zip - package and compress (archive) files
zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS.
It is analogous to a combination of the Unix commands tar(1) and compress(1) and is compatible with PKZIP (Phil Katz's ZIP for MSDOS systems).
# ls
1.txt
# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 75%)
# ls
1.txt 1.txt.zip
-r
--recurse-paths
Travel the directory structure recursively
# zip test.zip test/ 1.txt 1.txt.zip
adding: test/ (stored 0%)
adding: 1.txt (deflated 75%)
adding: 1.txt.zip (stored 0%)
# zip -r test.zip test/ 1.txt 1.txt.zip
adding: test/ (stored 0%)
adding: test/123/ (stored 0%)
adding: 1.txt (deflated 75%)
adding: 1.txt.zip (stored 0%)
yum -y install unzip
unzip - list, test and extract compressed files in a ZIP archive
# unzip test.zip
Archive: test.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:
[-d exdir]
An optional directory to which to extract files.
By default, all files and subdirectories are recreated in the current directory;
# unzip test.zip -d 456/
Archive: test.zip
creating: 456/test/
creating: 456/test/123/
inflating: 456/1.txt
extracting: 456/1.txt.zip
-l list archive files (short format).
# unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 01-07-2018 02:12 test/
0 01-07-2018 02:12 test/123/
2156850 01-06-2018 10:59 1.txt
541324 01-07-2018 02:05 1.txt.zip
--------- -------
2698174 4 files
6.6 tar打包
GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.
tar打包或解包时,会直接覆盖已有文件或目录,没有任何提示。
SYNOPSIS
tar [OPTION...] [FILE]...
EXAMPLES
tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.
tar -tvf archive.tar
# List all files in archive.tar verbosely.
# tar -tf xxx.tar
2.txt
1.txt
456/
# tar -tvf xxx.tar
-rw-r--r-- root/root 4 2018-01-08 10:47 2.txt
-rw-r--r-- root/root 2156850 2018-01-06 10:59 1.txt
drwxr-xr-x root/root 0 2018-01-07 02:24 456/
tar -xf archive.tar
# Extract all files from archive.tar.
Main operation mode:
-A, --catenate, --concatenate
append tar files to an archive
-c, --create
create a new archive
-d, --diff, --compare
find differences between archive and file system
--delete
delete from the archive (not on mag tapes!)
-r, --append
append files to the end of an archive
-t, --list
list the contents of an archive
-u, --update
only append files newer than copy in archive
-x, --extract, --get
extract files from an archive
Common options:
-C, --directory=DIR
change to directory DIR
-v, --verbose
verbosely list files processed
-f, --file=ARCHIVE
use archive file or device ARCHIVE
Local file selection:
--exclude=PATTERN
exclude files, given as a PATTERN
# tar -cvf xxx.tar --exclude 1.txt --exclude 2.txt 1.txt 2.txt test.zip
# tar -cvf xxx.tar --exclude "*.txt" 1.txt 2.txt test.zip
6.7 打包并压缩
-j, --bzip2
filter the archive through bzip2
-J, --xz
filter the archive through xz
-z, --gzip
filter the archive through gzip
tar -zcvf 123.tar.gz 123
tar -zxvf 123.tar.gz
tar -jcvf 123.bz2 123
tar -jxvf 123.bz2
tar -Jcvf 123.xz 123
tar -Jxvf 123.xz
tar -tf 123.bz2 / tar -tf 123.gz / tar -tf 123.xz