当我做:
# gzip -c foo > foo1.gz
# gzip < foo > foo2.gz
为什么foo2.gz
最终尺寸会比 更小foo1.gz
?
答案1
因为它会保存文件名和时间戳,以便稍后解压缩后可以尝试恢复它们。由于在第二个示例中foo
给出了gzip
via <stdin>
,因此它无法存储文件名和时间戳信息。
从联机帮助页:
-n --no-name
When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had
to be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the com-
pressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This option is the
default when decompressing.
-N --name
When compressing, always save the original file name and time stamp; this is the default. When decompressing, restore the original
file name and time stamp if present. This option is useful on systems which have a limit on file name length or when the time
stamp has been lost after a file transfer.
我在这里重现了这个问题:
[root@xxx601 ~]# cat /etc/fstab > file.txt
[root@xxx601 ~]# gzip < file.txt > file.txt.gz
[root@xxx601 ~]# gzip -c file.txt > file2.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root 465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root 456 May 17 19:34 file.txt.gz
在我的示例中,file.txt.gz
相当于您的foo2.gz
.使用该-n
选项会禁用此行为,否则会可以访问以下信息:
[root@xxx601 ~]# gzip -nc file.txt > file3.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root 465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root 456 May 17 19:43 file3.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root 456 May 17 19:34 file.txt.gz
file.txt
正如您在上面看到的,和的文件大小file3.txt
匹配,因为它们现在都省略了名称和日期。