Linux基础学习之文件压缩工具-gzip
先看介绍文件 :/$ man gzip
GZIP(1) General Commands Manual GZIP(1)
NAME
gzip, gunzip, zcat - compress or expand files
SYNOPSIS
gzip [ -acdfhklLnNrtvV19 ] [-S suffix] [ name ... ]
gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ... ]
zcat [ -fhLV ] [ name ... ]
DESCRIPTION
Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file
is replaced by one with the extension .gz, while keeping the same ownership modes, access and modifica‐
tion times. (The default extension is z for MSDOS, OS/2 FAT, Windows NT FAT and Atari.) If no files
are specified, or if a file name is "-", the standard input is compressed to the standard output. Gzip
will only attempt to compress regular files. In particular, it will ignore symbolic links.
If the compressed file name is too long for its file system, gzip truncates it. Gzip attempts to trun‐
cate only the parts of the file name longer than 3 characters. (A part is delimited by dots.) If the
name consists of small parts only, the longest parts are truncated. For example, if file names are lim‐
ited to 14 characters, gzip.msdos.exe is compressed to gzi.msd.exe.gz. Names are not truncated on sys‐
tems which do not have a limit on file name length.
By default, gzip keeps the original file name and timestamp in the compressed file. These are used when
decompressing the file with the -N option. This is useful when the compressed file name was truncated
or when the timestamp was not preserved after a file transfer.
Compressed files can be restored to their original form using gzip -d or gunzip or zcat. If the origi‐
nal 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.
#查看桌面上的文件以及文件夹,其中通过cp 1.txt 2.txt复制得到两个大小内容一致的文件
[wolf@localhost Desktop]$ ls
1 1.txt 2.txt
[wolf@localhost Desktop]$ ls -R
.:
1 1.txt 2.txt
./1:
1.txt 2.txt
[wolf@localhost Desktop]$ ll
total 8
drwxrwxr-x. 2 wolf wolf 32 Nov 3 19:12 1
-rw-rw-r--. 1 wolf wolf 1828 Nov 3 19:09 1.txt
-rw-rw-r--. 1 wolf wolf 1828 Nov 3 19:10 2.txt
首先压缩文件1.txt
[wolf@localhost Desktop]$ gzip 1.txt
[wolf@localhost Desktop]$ ll
total 8
drwxrwxr-x. 2 wolf wolf 32 Nov 3 19:12 1
-rw-rw-r--. 1 wolf wolf 93 Nov 3 19:09 1.txt.gz
-rw-rw-r--. 1 wolf wolf 1828 Nov 3 19:10 2.txt
发现1.txt原始文件找不到了,生成了压缩文件1.txt.gz,大小又1828字节变为93字节,我们查看可以保留原始文件的压缩方法。
wolf@localhost Desktop]$ 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
-k, --keep keep (don't delete) input files
-l, --list list compressed file contents
-L, --license display software license
-n, --no-name do not save or restore the original name and timestamp
-N, --name save or restore the original name and timestamp
-q, --quiet suppress all warnings
-r, --recursive operate recursively on directories
--rsyncable make rsync-friendly archive
-S, --suffix=SUF use suffix SUF on compressed files
--synchronous synchronous output (safer if system crashes, but slower)
-t, --test test compressed file integrity
-v, --verbose verbose mode
-V, --version display version number
-1, --fast compress faster
-9, --best compress better
With no FILE, or when FILE is -, read standard input.
Report bugs to <bug-gzip@gnu.org>.
可以看到-k参数可以保留原始文件
[wolf@localhost Desktop]$ ls
1 1.txt.gz 2.txt
[wolf@localhost Desktop]$ gzip -k 2.txt
[wolf@localhost Desktop]$ ls
1 1.txt.gz 2.txt 2.txt.gz
解压文件 -d选项
[wolf@localhost Desktop]$ ls
1 1.txt.gz 2.txt
[wolf@localhost Desktop]$ gzip -k 2.txt
[wolf@localhost Desktop]$ ls
1 1.txt.gz 2.txt 2.txt.gz
[wolf@localhost Desktop]$ gzip -d 1.txt.gz
[wolf@localhost Desktop]$ ls
1 1.txt 2.txt 2.txt.gz
[wolf@localhost Desktop]$ gzip -d 2.txt.gz
gzip: 2.txt already exists; do you wish to overwrite (y or n)? n
not overwritten
[wolf@localhost Desktop]$ ls
1 1.txt 2.txt 2.txt.gz
[wolf@localhost Desktop]$ gzip -d 2.txt.gz
gzip: 2.txt already exists; do you wish to overwrite (y or n)? y
[wolf@localhost Desktop]$ ls
1 1.txt 2.txt
最后,我们看一下,能不能讲文件夹进行一个压缩
[wolf@localhost Desktop]$ ls -R
.:
1 1.txt 2.txt
./1:
1.txt 2.txt
[wolf@localhost Desktop]$ gzip -k 1.txt 2.txt
[wolf@localhost Desktop]$ ls
1 1.txt 1.txt.gz 2.txt 2.txt.gz
[wolf@localhost Desktop]$ ll
total 16
drwxrwxr-x. 2 wolf wolf 32 Nov 3 19:12 1
-rw-rw-r--. 1 wolf wolf 1828 Nov 3 19:09 1.txt
-rw-rw-r--. 1 wolf wolf 93 Nov 3 19:09 1.txt.gz
-rw-rw-r--. 1 wolf wolf 1828 Nov 3 19:10 2.txt
-rw-rw-r--. 1 wolf wolf 93 Nov 3 19:10 2.txt.gz
结果显示,这个是目录文件,直接给忽略,可以看出来不行。同样对两个文件进行压缩的话,也是生成两个压缩文件。
最后在解释文档里有讲到,压缩文件的时间戳与原始文件是一样的,保持不变,通过ll命令可以看到。
下一节进行归档tar命令的介绍。